title: Can I move from &mut self to &self within the same function?
body: <p>I'm trying to learn a bit of Rust through a toy application, which involves a tree data structure that is filled dynamically by querying an external source. In the beginning, only the root node is present. The tree structure provides a method <code>get_children(id)</code> that returns a <code>[u32]</code> of the IDs of all the node's children — either this data is already known, or the external source is queried and all the nodes are inserted into the tree.</p>
  <p>I'm running into the following problem with the borrow checker that I can't seem to figure out:</p>
  <pre class="lang-rust prettyprint-override"><code>struct Node {
      id: u32,
      value: u64, // in my use case, this type is much larger and should not be copied
      children: Option&lt;Vec&lt;u32&gt;&gt;,
  }
  
  struct Tree {
      nodes: std::collections::HashMap&lt;u32, Node&gt;,
  }
  
  impl Tree {
      fn get_children(&amp;mut self, id: u32) -&gt; Option&lt;&amp;[u32]&gt; {
          // This will perform external queries and add new nodes to the tree
          None
      }
  
      fn first_even_child(&amp;mut self, id: u32) -&gt; Option&lt;u32&gt; {
          let children = self.get_children(id)?;
          let result = children.iter().find(|&amp;id| self.nodes.get(id).unwrap().value % 2 == 0)?;
          Some(*result)
      }
  }
  </code></pre>
  <p>Which results in:</p>
  <pre><code>error[E0502]: cannot borrow `self.nodes` as immutable because it is also borrowed as mutable
    --&gt; src/lib.rs:19:43
     |
  18 |         let children = self.get_children(id)?;
     |                        ---- mutable borrow occurs here
  19 |         let result = children.iter().find(|&amp;id| self.nodes.get(id).unwrap().value % 2 == 0)?;
     |                                      ---- ^^^^^ ---------- second borrow occurs due to use of `self.nodes` in closure
     |                                      |    |
     |                                      |    immutable borrow occurs here
     |                                      mutable borrow later used by call
  </code></pre>
  <p>Since <code>get_children</code> might insert nodes into the tree, we need a <code>&amp;mut self</code> reference. However, the way I see it, after the value of <code>children</code> is known, <code>self</code> no longer needs to be borrowed mutably. Why does this not work, and how would I fix it?</p>
  <h4>EDIT -- my workaround</h4>
  <p>After Chayim Friedman's answer, I decided against returning <code>Self</code>. I mostly ran into the above problem when first calling <code>get_children</code> to get a list of IDs and then using <code>nodes.get()</code> to obtain the corresponding <code>Node</code>. Instead, I refactored to provide the following functions:</p>
  <pre><code>impl Tree {
      fn load_children(&amp;mut self, id: u32) {
          // If not present yet, perform queries to add children to the tree
      }
  
      fn iter_children(&amp;self, id: u32) -&gt; Option&lt;IterChildren&gt; {
          // Provides an iterator over the children of node `id`
      }
  }
  </code></pre>