title: Game loop in Rust while satisfying the borrow checker
body: <p>Here's a very simple stab at a game loop in Rust:</p>
  
  <pre><code>trait Entity {
      fn update(&amp;mut self, world: &amp;mut World);
  }
  
  struct World {
      entities: Vec&lt;Box&lt;Entity&gt;&gt;
  }
  impl World {
      fn update(&amp;mut self) {
          for ent in self.entities.iter_mut() {
              ent.update(self);
          }
      }
  }
  </code></pre>
  
  <p>This, however, does not work:</p>
  
  <pre><code>error[E0499]: cannot borrow `*self` as mutable more than once at a time
    --&gt; test.rs:12:16
     |
  11 |                    for ent in self.entities.iter_mut() {
     |                               ------------- first mutable borrow occurs here
  12 |                            ent.update(self);
     |                                ^^^^ second mutable borrow occurs here
  13 |                    }
     |                    - first borrow ends here
  
  error: aborting due to previous error
  </code></pre>
  
  <p>In order to get the entity out of the vector, I need to do a mutable borrow of the <code>World</code> struct, which prevents the entity from using it in any way.</p>
  
  <p>What would be a good way to structure this, so that I can both mutate the entity as well as have the entity reference other objects in the world (for collision checking, etc)?</p>
  
  <p>The only way I can think of is to either have the entity return an updated copy of itself (I don't think all that allocating and copying would be good for performance) or cowboy it with <code>unsafe</code>.</p>