title: Embedding a borrowed value into an enum?
body: <p>I'm having difficulty with the following code:</p>
  
  <pre><code>trait HelloPhrase {
      fn hello(&amp;self, to: &amp;'static str);
  }
  
  pub enum GetHelloResult&lt;H: HelloPhrase&gt; {
      Matched(H),
      NoMatch,
  }
  
  struct English;
  
  impl English {
      pub fn new() -&gt; English {
          English
      }
  }
  
  impl HelloPhrase for English {
      fn hello(&amp;self, to: &amp;'static str) {
          println!("Hello {}.", to)
      }
  }
  
  struct Phrases&lt;H: HelloPhrase&gt; {
      hello_phrases: std::collections::HashMap&lt;&amp;'static str, H&gt;,
  }
  
  impl&lt;H: HelloPhrase&gt; Phrases&lt;H&gt; {
      pub fn new() -&gt; Phrases&lt;H&gt; {
          Phrases { hello_phrases: std::collections::HashMap::new() }
      }
  
      pub fn add_hello_phrase(&amp;mut self, lang: &amp;'static str, hello_phrase: H) {
          self.hello_phrases.insert(lang, hello_phrase);
      }
  
      pub fn get_hello(&amp;self, lang: &amp;'static str) -&gt; GetHelloResult&lt;H&gt; {
          match self.hello_phrases.get(lang) {
              Some(hello_phrase) =&gt; return GetHelloResult::Matched(hello_phrase),
              _ =&gt; return GetHelloResult::NoMatch,
          };
      }
  }
  
  fn main() {
      let mut hs = Phrases::new();
      hs.add_hello_phrase("english", English::new());
  
      match hs.get_hello("english") {
          GetHelloResult::Matched(hello_phrase) =&gt; hello_phrase.hello("Tom"),
          _ =&gt; println!("HelloPhrase not found"),
      }
  }
  </code></pre>
  
  <p>(<a href="https://play.rust-lang.org/?gist=8d37aa99377a4eb31ba5d53e7a0cc666&amp;version=nightly&amp;backtrace=0" rel="nofollow">play link</a>)</p>
  
  <p><code>HelloPhrase</code> is a trait for a language to implement, English, Russian, etc. <code>Phrases</code> is a manager struct, which could have many maps of language-to-phrase. This is a contrived example, but you could think of this as an event manager (that is, get the event handlers for X input), or as an HTTP handler and router.</p>
  
  <p>With that said, I'm having difficulty understanding how to borrow ownership of a <code>HelloPhrase</code> to return it to the caller. Running it, returns the following error:</p>
  
  <pre><code>&lt;anon&gt;:40:66: 40:78 error: mismatched types:
   expected `H`,
      found `&amp;H`
  (expected type parameter,
      found &amp;-ptr) [E0308]
  &lt;anon&gt;:40             Some(hello_phrase) =&gt; return GetHelloResult::Matched(hello_phrase),
                                                                             ^~~~~~~~~~~~
  </code></pre>
  
  <p>I've tried adding:</p>
  
  <pre><code>pub fn get_hello(&amp;self, lang: &amp;'static str) -&gt; GetHelloResult&lt;&amp;H&gt; {
  </code></pre>
  
  <p>and</p>
  
  <pre><code>pub enum GetHelloResult&lt;H: HelloPhrase&gt; {
      Matched(&amp;H),
      NoMatch,
  }
  </code></pre>
  
  <p>(<a href="https://play.rust-lang.org/?gist=7c497838328961397583c6b1236a9cc9&amp;version=nightly&amp;backtrace=0" rel="nofollow">play link</a>)</p>
  
  <p>which results in the following error:</p>
  
  <pre><code>&lt;anon&gt;:7:13: 7:15 error: missing lifetime specifier [E0106]
  &lt;anon&gt;:7     Matched(&amp;H),
  </code></pre>
  
  <p>I'm having trouble adding a lifetime to the enum - in theory I want the lifetime of the returned value to be that of the <code>Phrases</code> struct - but the lifetime syntax is quite confusing to me so far. To sum this up into two questions:</p>
  
  <ol>
  <li>How do I add a lifetime to <code>GetHelloResult</code>, to satisfy this error?</li>
  <li>Based on ownership rules with Rust, is what I'm trying to do an anti-pattern with Rust? What might be a better design for something like this?</li>
  </ol>
  
  <p>Based on the documentation, I know how to use a lifetime on a struct, but I do not know how to add a lifetime to an enum (syntax wise). I only mentioned struct lifetime because I assume that's a missing part, but I honestly don't know. Furthermore, if I add a lifetime to the struct and impl and attempt to add it to the <code>hello_phrases</code> map, I get the error </p>
  
  <blockquote>
  <pre><code>the parameter type `H` may not live long enough [E0309]
  </code></pre>
  </blockquote>