How to convert a string to an enum?
<p>My <a href="https://play.rust-lang.org/?gist=08f08974535dcbbab05a870695c0dcc8&amp;version=stable&amp;backtrace=0" rel="noreferrer">original approach</a> was to create method called <code>to_str()</code> which will return a slice, but I am not sure that it is correct approach because this code doesn't compile.</p>

<pre><code>enum WSType {
    ACK,
    REQUEST,
    RESPONSE,
}

impl WSType {
    fn to_str(&amp;self) -&gt; &amp;str {
        match self {
            ACK =&gt; "ACK",
            REQUEST =&gt; "REQUEST",
            RESPONSE =&gt; "RESPONSE",            
        }
    }
}

fn main() {
    let val = "ACK";
    // test
    match val {
        ACK.to_str() =&gt; println!("ack"),
        REQUEST.to_str() =&gt; println!("ack"),
        RESPONSE.to_str() =&gt; println!("ack"),
        _ =&gt; println!("unexpected"),
    }
}
</code></pre>