title: Merge two HashMaps in Rust
body: <p>So I'm a bit stuck, trying to merge two HashMaps.</p>
  
  <p>It's easy to do it inline:</p>
  
  <pre><code>fn inline() {
      let mut first_context = HashMap::new();
      first_context.insert("Hello", "World");
      let mut second_context = HashMap::new();
      second_context.insert("Hey", "There");
  
      let mut new_context = HashMap::new();
      for (key, value) in first_context.iter() {
          new_context.insert(*key, *value);
      }
      for (key, value) in second_context.iter() {
          new_context.insert(*key, *value);
      }
      println!("Inline:\t\t{}", new_context);
      println!("Inline:\t\t{}\t{} [Initial Maps Still Usable]", first_context, second_context);
  }
  </code></pre>
  
  <p>It's easy enough to make a function:</p>
  
  <pre><code>fn abstracted() {
      fn merge&lt;'a&gt;(first_context: &amp;HashMap&lt;&amp;'a str, &amp;'a str&gt;, second_context: &amp;HashMap&lt;&amp;'a str, &amp;'a str&gt;) -&gt; HashMap&lt;&amp;'a str, &amp;'a str&gt; {
          let mut new_context = HashMap::new();
          for (key, value) in first_context.iter() {
              new_context.insert(*key, *value);
          }
          for (key, value) in second_context.iter() {
              new_context.insert(*key, *value);
          }
          new_context
      }
  
      let mut first_context = HashMap::new();
      first_context.insert("Hello", "World");
      let mut second_context = HashMap::new();
      second_context.insert("Hey", "There");
  
      println!("Abstracted:\t{}", merge(&amp;first_context, &amp;second_context));
      println!("Abstracted:\t{}\t{} [Initial Maps Still Usable]", first_context, second_context);
  }
  </code></pre>
  
  <p>However, I can't seem to get the generic version to work:</p>
  
  <pre><code>fn generic() {
      fn merge&lt;'a, K: Hash + Eq, V&gt;(first_context: &amp;HashMap&lt;&amp;'a K, &amp;'a V&gt;, second_context: &amp;HashMap&lt;&amp;'a K, &amp;'a V&gt;) -&gt; HashMap&lt;&amp;'a K, &amp;'a V&gt; {
          let mut new_context = HashMap::new();
          for (key, value) in first_context.iter() {
              new_context.insert(*key, *value);
          }
          for (key, value) in second_context.iter() {
              new_context.insert(*key, *value);
          }
          new_context
      }
  
      let mut first_context = HashMap::new();
      first_context.insert("Hello", "World");
      let mut second_context = HashMap::new();
      second_context.insert("Hey", "There");
  
      println!("Generic:\t{}", merge(&amp;first_context, &amp;second_context));
      println!("Generic:\t{}\t{} [Initial Maps Still Usable]", first_context, second_context);
  }
  </code></pre>
  
  <p><a href="https://play.rust-lang.org/?gist=411066079819b1db825502934e5b585b&amp;version=stable" rel="noreferrer">The above code on play.rust-lang.org</a>.</p>
  
  <p>Compiling it:</p>
  
  <pre><code>error: the trait `core::kinds::Sized` is not implemented for the type `str`
  </code></pre>
  
  <p>I get that the compiler is confused about the size of the generic value, but I'm not sure why "str" doesn't have a strict memory size? I know its a String slice and not a type, but still this should work, no? Is this a bug?</p>
  
  <p>I thought this would be a relatively trivial function. If someone has a good solution, I'd love to learn. Actually ideally, I'd love to see a solution with a <code>trait Mergeable</code> and write a decorator for HashMap&lt;&amp;K, &amp;V>, such that I can call <code>let new_context = first_context.merge(&amp;second_context);</code> but this can be a different question.</p>