title: How can one array contain multiple mutable references to the same object?
body: <p>I'm making a tile-based game with structures that span multiple tiles. Every tile under the structure has to have a mutable reference to that structure. For a structure that spans two tiles, the 2D array that represents the map has to simultaneously contain two mutable references to the structure.</p>
  
  <p>I also have a list that mutably owns all my structures (for iteration)</p>
  
  <p>I tried this with <code>&amp;mut</code> and failed:</p>
  
  <pre><code>let mut all_structures: Vec&lt;Box&lt;Structure&gt;&gt; = Vec::new();
  let mut grid: [[Vec&lt;&amp;mut Box&lt;Structure&gt;&gt;; 4]; 4] = Default::default(); // accessed [y][x]
  
  let mut house: Box&lt;Structure&gt; = Box::new(House { });
  grid[1][1].push(&amp;mut house);
  grid[1][2].push(&amp;mut house);
  all_structures.push(house);
  </code></pre>
  
  <p>with error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0499]: cannot borrow `house` as mutable more than once at a time
    --&gt; src/main.rs:21:21
     |
  20 |     grid[1][1].push(&amp;mut house);
     |                     ---------- first mutable borrow occurs here
  21 |     grid[1][2].push(&amp;mut house);
     |                ---- ^^^^^^^^^^ second mutable borrow occurs here
     |                |
     |                first borrow later used by call
  </code></pre>
  
  <p>Right now this is just single threaded so nothing needs to be thread-safe</p>
  
  <p>I put the code on the <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;code=trait%20Structure%20%7B%0A%20%20%20%20%0A%7D%0A%0Astruct%20House%20%7B%0A%20%20%20%20%0A%7D%0Aimpl%20Structure%20for%20House%20%7B%0A%20%20%20%20%0A%7D%0A%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20mut%20all_structures%3A%20Vec%3CBox%3CStructure%3E%3E%20%3D%20Vec%3A%3Anew()%3B%0A%20%20%20%20let%20mut%20grid%3A%20%5B%5BVec%3C%26mut%20Box%3CStructure%3E%3E%3B%204%5D%3B%204%5D%20%3D%20Default%3A%3Adefault()%3B%20%2F%2F%20%5By%5D%5Bx%5D%0A%20%20%20%20%0A%20%20%20%20%0A%20%20%20%20let%20mut%20house%3A%20Box%3CStructure%3E%20%3D%20Box%3A%3Anew(House%20%7B%20%7D)%3B%0A%20%20%20%20grid%5B1%5D%5B1%5D.push(%26mut%20house)%3B%0A%20%20%20%20grid%5B1%5D%5B2%5D.push(%26mut%20house)%3B%0A%20%20%20%20all_structures.push(house)%3B%0A%0A%0A%20%20%20%20println!(%22Hello%2C%20world!%22)%3B%0A%7D%0A" rel="nofollow noreferrer">playground</a></p>