title: Cloning a hashmap using a reference to another hashmap
body: <p>I have a reference to a hashmap (<code>data</code> in the code below) which I want to clone into a new, owned hashmap. Cloning the reference gives me a new reference, which is not what I need.</p>
  
  <p>I also tried doing an iter + map over the <code>data</code> reference and cloning the key and value pairs individually followed by a collect, but that also does not work. Here is a minimal working example:</p>
  
  <pre class="lang-rust prettyprint-override"><code>use core::cell::Cell;
  use std::collections::HashMap;
  use std::collections::HashSet;
  use std::rc::Rc;
  
  struct Dummy&lt;K, V&gt; {
      dirty: Rc&lt;Cell&lt;bool&gt;&gt;,
      data: Cell&lt;Option&lt;HashMap&lt;K, HashSet&lt;V&gt;&gt;&gt;&gt;,
  }
  
  impl&lt;K, V&gt; Dummy&lt;K, V&gt; {
      fn persist(&amp;self, prefix: &amp;str, data: &amp;HashMap&lt;K, HashSet&lt;V&gt;&gt;) {
          self.dirty.set(true);
          self.data.set(Some(data.clone()));
      }
  }
  </code></pre>
  
  <p>which gives the following error:</p>
  
  <pre><code>error[E0308]: mismatched types
    --&gt; src/lib.rs:14:28
     |
  14 |         self.data.set(Some(data.clone()));
     |                            ^^^^^^^^^^^^ expected struct `std::collections::HashMap`, found reference
     |
     = note: expected type `std::collections::HashMap&lt;K, std::collections::HashSet&lt;V&gt;&gt;`
                found type `&amp;std::collections::HashMap&lt;K, std::collections::HashSet&lt;V&gt;&gt;`
  </code></pre>
  
  <p>(<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=01f983a97475341f0525803dc687fc4b" rel="nofollow noreferrer">Permalink to the playground</a>)</p>
  
  <p>The purpose of this code is to observe the contents of the hashmap on through the <code>Dummy</code> struct, which is used for unit testing.</p>
  
  <p>I guess this problem is because given the generic types there is no way to determine how to deeply clone the key and value objects? </p>
  
  <p>Is there a way to create a new hashmap given a reference to an existing hashmap?</p>