title: Avoid calling both HashMap::get() and HashMap::get_mut() when borrowing 'self'
body: <p>I'm learning rust and I find myself writing lots and lots of code that disappoints the borrow checker. I've recently ran into this quirk that I can't figure out a way around.</p>
  <p>To set the scene: I would like to (1) fetch a struct from a HashMap, (2) compute some metadata about the fetched struct, then (3) modify the fetched struct. Simple! See a simplified example below:</p>
  <pre><code>struct Entity {
      pub metadata: i64
  }
  struct Engine {
      pub entities: HashMap&lt;i32, Box&lt;Entity&gt;&gt;
  }
  impl Engine {
      fn immutable_calculate_metadata(&amp;self, new_metadata: i64) -&gt; i64 {
          // Compute a bunch of stuff that requires reading &quot;self&quot;
          42 + new_metadata
      }
      pub fn add_entity(&amp;mut self, key: i32, metadata: i64) {
          self.entities.insert(key, Box::new(Entity{metadata}));
          let neighbor_key = &amp;(key - 1);
          if let Some(neighbor) = self.entities.get_mut(neighbor_key) {
              let metadata = self.immutable_calculate_metadata(metadata);
              // I *want* to write this line, but it breaks:
              // neighbor.metadata = metadata;
              // So I have to re-fetch the same key.
              self.entities.get_mut(neighbor_key).unwrap().metadata = metadata;
          }
      }
  }
  </code></pre>
  <p>(rust playground: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2021&amp;gist=cf53e4af2d41add3765578e619c75e5e" rel="nofollow noreferrer">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2021&amp;gist=cf53e4af2d41add3765578e619c75e5e</a> )</p>
  <p>I don't know how I can remove the redundant fetch in the
  <code>self.entities.get_mut(neighbor_key).unwrap().metadata = metadata;</code> line. Any help here would be greatly appreciated. Ideally someone who knows what they're doing (read: you) could enumerate the options I have here to avoid this.</p>
  <p>Of course, if I try to do this intuitively (see the commented out line <code>// neighbor.metadata = metadata;</code>) I get the following error:</p>
  <pre><code>error[E0502]: cannot borrow `*self` as immutable because it is also borrowed as mutable
     |
  18 |         if let Some(neighbor) = self.entities.get_mut(neighbor_key) {
     |                                 ----------------------------------- mutable borrow occurs here
  19 |             let metadata = self.immutable_calculate_metadata(metadata);
     |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ immutable borrow occurs here
  20 |             // I *want* to write this line, but it breaks:
  21 |             neighbor.metadata = metadata;
     |             ---------------------------- mutable borrow later used here
  </code></pre>
  <p>I don't exactly understand why <code>self.entities.get_mut(neighbor_key)</code> is considered a &quot;mutable borrow of <code>*self</code>&quot;. It seems to me we're borrowing an <code>Entity</code> or a <code>HashMap</code> but that's not the focus of this question.</p>