title: Cannot borrow as mutable more than once at a time in one code - but can in another very similar
body: <p>I've this snippet that doesn't pass the borrow checker:</p>
  
  <pre><code>use std::collections::HashMap;
  
  enum Error {
      FunctionNotFound,
  }
  
  #[derive(Copy, Clone)]
  struct Function&lt;'a&gt; {
      name: &amp;'a str,
      code: &amp;'a [u32],
  }
  
  struct Context&lt;'a&gt; {
      program: HashMap&lt;&amp;'a str, Function&lt;'a&gt;&gt;,
      call_stack: Vec&lt;Function&lt;'a&gt;&gt;,
  }
  
  impl&lt;'a&gt; Context&lt;'a&gt; {
      fn get_function(&amp;'a mut self, fun_name: &amp;'a str) -&gt; Result&lt;Function&lt;'a&gt;, Error&gt; {
          self.program
              .get(fun_name)
              .map(|f| *f)
              .ok_or(Error::FunctionNotFound)
      }
  
      fn call(&amp;'a mut self, fun_name: &amp;'a str) -&gt; Result&lt;(), Error&gt; {
          let fun = try!(self.get_function(fun_name));
  
          self.call_stack.push(fun);
  
          Ok(())
      }
  }
  
  fn main() {}
  </code></pre>
  
  <pre class="lang-none prettyprint-override"><code>error[E0499]: cannot borrow `self.call_stack` as mutable more than once at a time
    --&gt; src/main.rs:29:9
     |
  27 |         let fun = try!(self.get_function(fun_name));
     |                        ---- first mutable borrow occurs here
  28 | 
  29 |         self.call_stack.push(fun);
     |         ^^^^^^^^^^^^^^^ second mutable borrow occurs here
  ...
  32 |     }
     |     - first borrow ends here
  </code></pre>
  
  <p>My gut feeling is that the problem is tied to the fact that <code>HashMap</code> returns either <code>None</code> or a reference of the value inside the data structure. But I don't want that: my intention is that <code>self.get_function</code> should return either a byte copy of the stored value or an error (that's why I put <code>.map(|f| *f)</code>, and <code>Function</code> is <code>Copy</code>).</p>
  
  <p>Changing <code>&amp;'a mut self</code> to something else doesn't help.</p>
  
  <p>However, the following snippet, somewhat similar in spirit, is accepted:</p>
  
  <pre><code>#[derive(Debug)]
  enum Error {
      StackUnderflow,
  }
  
  struct Context {
      stack: Vec&lt;u32&gt;,
  }
  
  impl Context {
      fn pop(&amp;mut self) -&gt; Result&lt;u32, Error&gt; {
          self.stack.pop().ok_or(Error::StackUnderflow)
      }
  
      fn add(&amp;mut self) -&gt; Result&lt;(), Error&gt; {
          let a = try!(self.pop());
          let b = try!(self.pop());
  
          self.stack.push(a + b);
          Ok(())
      }
  }
  
  fn main() {
      let mut a = Context { stack: vec![1, 2] };
      a.add().unwrap();
      println!("{:?}", a.stack);
  }
  </code></pre>
  
  <p>Now I'm confused. What is the problem with the first snippet? Why doesn't it happen in the second?</p>
  
  <p>The snippets are part of a larger piece of code. In order to provide more context, <a href="https://play.rust-lang.org/?gist=e2c360eb91f7e0e459e7&amp;version=stable" rel="noreferrer">this on the Rust Playground</a> shows a more complete example with the faulty code, and <a href="https://play.rust-lang.org/?gist=f6152f3411c1effed810&amp;version=stable" rel="noreferrer">this shows an earlier version without <code>HashMap</code></a>, which passes the borrow checker and runs normally.</p>