title: Cannot borrow as mutable because it is also borrowed as immutable
body: <p>I am learning Rust and I don't quite get why this is not working. </p>
  
  <pre><code>#[derive(Debug)]
  struct Node {
      value: String,
  }
  
  #[derive(Debug)]
  pub struct Graph {
      nodes: Vec&lt;Box&lt;Node&gt;&gt;,
  }
  
  fn mk_node(value: String) -&gt; Node {
      Node { value }
  }
  
  pub fn mk_graph() -&gt; Graph {
      Graph { nodes: vec![] }
  }
  
  impl Graph {
      fn add_node(&amp;mut self, value: String) {
          if let None = self.nodes.iter().position(|node| node.value == value) {
              let node = Box::new(mk_node(value));
              self.nodes.push(node);
          };
      }
  
      fn get_node_by_value(&amp;self, value: &amp;str) -&gt; Option&lt;&amp;Node&gt; {
          match self.nodes.iter().position(|node| node.value == *value) {
              None =&gt; None,
              Some(idx) =&gt; self.nodes.get(idx).map(|n| &amp;**n),
          }
      }
  }
  
  
  #[cfg(test)]
  mod tests {
      use super::*;
  
      #[test]
      fn some_test() {
          let mut graph = mk_graph();
  
          graph.add_node("source".to_string());
          graph.add_node("destination".to_string());
  
          let source = graph.get_node_by_value("source").unwrap();
          let dest = graph.get_node_by_value("destination").unwrap();
  
          graph.add_node("destination".to_string());
      }
  }
  </code></pre>
  
  <p>(<a href="https://play.rust-lang.org/?gist=6ddb1ff6464c5082a19b3f9e338667d3&amp;version=nightly" rel="noreferrer">playground</a>)</p>
  
  <p>This has the error</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0502]: cannot borrow `graph` as mutable because it is also borrowed as immutable
    --&gt; src/main.rs:50:9
     |
  47 |         let source = graph.get_node_by_value("source").unwrap();
     |                      ----- immutable borrow occurs here
  ...
  50 |         graph.add_node("destination".to_string());
     |         ^^^^^ mutable borrow occurs here
  51 |     }
     |     - immutable borrow ends here
  </code></pre>
  
  <p>This example from <em>Programming Rust</em> is quite similar to what I have but it works:</p>
  
  <pre><code>pub struct Queue {
      older: Vec&lt;char&gt;,   // older elements, eldest last.
      younger: Vec&lt;char&gt;, // younger elements, youngest last.
  }
  
  impl Queue {
      /// Push a character onto the back of a queue.
      pub fn push(&amp;mut self, c: char) {
          self.younger.push(c);
      }
  
      /// Pop a character off the front of a queue. Return `Some(c)` if there /// was a character to pop, or `None` if the queue was empty.
      pub fn pop(&amp;mut self) -&gt; Option&lt;char&gt; {
          if self.older.is_empty() {
              if self.younger.is_empty() {
                  return None;
              }
  
              // Bring the elements in younger over to older, and put them in // the promised order.
              use std::mem::swap;
              swap(&amp;mut self.older, &amp;mut self.younger);
              self.older.reverse();
          }
  
          // Now older is guaranteed to have something. Vec's pop method // already returns an Option, so we're set.
          self.older.pop()
      }
  
      pub fn split(self) -&gt; (Vec&lt;char&gt;, Vec&lt;char&gt;) {
          (self.older, self.younger)
      }
  }
  
  pub fn main() {
      let mut q = Queue {
          older: Vec::new(),
          younger: Vec::new(),
      };
  
      q.push('P');
      q.push('D');
  
      assert_eq!(q.pop(), Some('P'));
      q.push('X');
  
      let (older, younger) = q.split(); // q is now uninitialized.
      assert_eq!(older, vec!['D']);
      assert_eq!(younger, vec!['X']);
  }
  </code></pre>