title: How to use mutable state in a loop?
body: <p>The loop:</p>
  <pre><code>impl State {    
      pub fn exec(&amp;mut self) -&gt; Vec&lt;(&amp;[u8], SocketAddr)&gt; {
          vec!()
      }
  }
  
  fn main_loop(mut state: State) {
      let mut packets = Vec::&lt;(&amp;[u8], SocketAddr)&gt;::new();
      loop {
          let new = state.exec();
          packets.extend(new);
      }
  }
  </code></pre>
  <p>gives the error:</p>
  <pre><code>error[E0499]: cannot borrow `state` as mutable more than once at a time
    --&gt; src/main.rs:93:14
     |
  93 |             let new = state.exec();
     |                       ^^^^^ `state` was mutably borrowed here in the previous iteration of the loop
  </code></pre>
  <p>Yet if I comment-out the <code>packets.extend(new);</code> line, there is no error.</p>
  <p>I <em>think</em> the problem is that the returned vector is owned by <code>state</code>, as if <code>new</code> is dropped before the loop iterates, it's fine.  I'm happy for the returned vector to be copied, if this would fix it.</p>
  <p>I expected that adding <code>.clone()</code> to either <code>new</code> or <code>state.exec()</code> would have fixed it, but it doesn't.</p>
  <p>Is there a simple way of fixing this?</p>
  <p>Or do I need to structure single-threaded application state in a different way?</p>