title: How to mutate hash map?
body: <p>I'm solving a task from the Rust Book:</p>
  
  <pre><code>struct Cacher&lt;T&gt;
  where
      T: Fn(i32) -&gt; i32,
  {
      calculation: T,
      hm: HashMap&lt;i32, i32&gt;,
  }
  
  impl&lt;T&gt; Cacher&lt;T&gt;
  where
      T: Fn(i32) -&gt; i32,
  {
      // ...
      fn value(&amp;mut self, arg: i32) -&gt; i32 {
          let v = self.hm.get(&amp;arg); // borrowing returned Option&lt;&amp;V&gt;
          match v {
              // matching owned value
              Some(v) =&gt; *v, //return Copy'ied i32
              None =&gt; {
                  let v2 = (self.calculation)(arg); // get result of closure
                  self.hm.insert(arg, v2); // memoize gotten value
                  v2 // return value
              }
          }
      }
      // ...
  }
  </code></pre>
  
  <p>However, the compiler gives the following:</p>
  
  <pre class="lang-none prettyprint-override"><code>let v = self.hm.get(&amp;arg);
  --- immutable borrow occures here
  </code></pre>
  
  <p>Ok, I understand it, but the next message:</p>
  
  <pre><code>self.hm.insert(arg, v2);
  ^^^^^^^ mutable borrow occures here
  </code></pre>
  
  <p>How does this happens if I don't change the borrowed (<code>v</code>) value in <code>self.hm.insert(arg, v2);</code>?</p>
  
  <p>I did a mutable borrow by changing <code>let v</code> to <code>let mut v</code>, but it didn't help: the compiler reports the same error.</p>
  
  <p>How could I change my code to be able to insert a memoized value into the hash map?</p>
  
  <p>Sorry for vague title, didn't find better description.</p>