title: How can I mutably borrow a vector while iterating over it?
body: <p>I have a vector of <code>Obj</code>s, and I need the <code>do_something</code> method to be able to mutate that vector.</p>
  <pre><code>fn main() {
      let mut vect: Vec&lt;Box&lt;dyn Obj&gt;&gt; = vec![];
      vect.push(Box::new(ExampleObject { my_value: 1 }));
      for e in &amp;mut vect {
          e.do_something(&amp;mut vect);
      }
  }
  struct ExampleObject {
      pub my_value: i32,
  }
  impl Obj for ExampleObject {
      fn do_something(&amp;mut self, state: &amp;mut Vec&lt;Box&lt;dyn Obj&gt;&gt;) {
          state.push(Box::new(ExampleObject { my_value: 1 }));
      }
  }
  trait Obj {
      fn do_something(&amp;mut self, state: &amp;mut Vec&lt;Box&lt;dyn Obj&gt;&gt;);
  }
  </code></pre>
  <p>Doing this gives me this error:</p>
  <pre><code>error[E0499]: cannot borrow `vect` as mutable more than once at a time
    --&gt; core/src/lib.rs:91:24
     |
  90 |     for e in &amp;mut vect {
     |              ---------
     |              |
     |              first mutable borrow occurs here
     |              first borrow later used here
  91 |         e.do_something(&amp;mut vect);
     |                        ^^^^^^^^^ second mutable borrow occurs here
  </code></pre>
  <p>I understand why this invalidates the borrow checker, but I've yet to come up with a workaround that satisfies my requirements of having the implemented trait function be able to mutate the vector.</p>
  <p>I would prefer not to have anything be cloned, but it is possible.</p>
  <p>I know this causes an infinite loop, its just a simplified example of the structure I want to implement.</p>