title: How to avoid cloning parts when changing a mutable struct while recursion over that struct
body: <p>I try to go recursively through a rose tree. The following code also works as intended but I still have the problem that I need to clone the value due to issues with the borrow checker. Therefore, it would be nice if there is a way to change from cloning to something better.</p>
  <p>Without the clone() rust complains (rightfully) that I borrow self mutable by looking at the child nodes and the second time in the closure.</p>
  <p>The whole structure and code is more complicated and bigger than shown below but that are the core elements. Do I have to change the data structure or do I miss something obvious? If the data structure is the issue how would you change it?</p>
  <p>Also, the NType enum seems kinda useless here but I have some additional kinds that I have to consider. Here Inner nodes always have children and Outer nodes never.</p>
  <pre><code>enum NType{
      Inner,
      Outer
  }
  
  #[derive(Eq, PartialEq, Clone, Debug)]
  struct Node {
      // isn't a i32 actually. In my real program it's another struct 
      count: i32,
      n_type: NType,
      children: Option&lt;Vec&lt;usize&gt;&gt;
  }
  
  #[derive(Eq, PartialEq, Clone, Debug)]
  struct Tree {
      nodes: Vec&lt;Node&gt;,
  }
  
  impl Tree{
      
      pub fn calc(&amp;mut self, features: &amp;Vec&lt;i32&gt;) -&gt; i32{
          // root is the last node
          self.calc_h(self.nodes.len() - 1, features);
          self.nodes[self.nodes.len() - 1].count.clone()
      }
  
      fn calc_h(&amp;mut self, current: usize, features: &amp;Vec&lt;i32&gt;){
  
          // do some other things to decide where to go into recursion and where not to
          // also use the features
  
          if self.nodes[current].n_type == Inner{
              //cloneing is very expensiv and destroys the performance
              self.nodes[current].children.as_ref().unwrap().clone().iter().for_each(|&amp;n| self.calc_h(n, features));
              self.do_smt(current)
          }
          self.do_smt(current)
      }
  }
  
  </code></pre>
  <p>Edit:</p>
  <ul>
  <li>Lagerbaer suggested to use as_mut but that results into current being a &amp;mut usize and that doesn't really solve the problem.</li>
  <li>changed childs into children</li>
  </ul>