title: How do I upgrade a reference to a mutable reference?
body: <p>I have a program that revolves around one shared data structure, proposing changes to the data, and then applying these changes at a later stage. These proposed changes hold references to the core object.</p>
  
  <p>In C++ or another language, I would simply make the reference non-const, then mutate it when I need to. But Rust doesn't play well with this approach. (I asked about this in IRC earlier today, but sadly I'm still stuck.)</p>
  
  <p>To help, I made a distilled example for booking tickets in a theatre, where theatre is the data structure, the Bookings are proposed changes, and the <code>run</code> method would be applying them if I could figure out how to get it to work!</p>
  
  <p>Firstly, defining some data structures. A theatre has many rows, which have many seats each:</p>
  
  <pre><code>use std::sync::{Arc, RwLock};
  use std::thread;
  
  struct Theatre { rows: Vec&lt;Row&gt; }
  struct Row { seats: Vec&lt;Seat&gt; }
  
  struct Seat {
      number: i32,
      booked: bool,
  }
  
  impl Seat {
      fn new(number: i32) -&gt; Seat {
          Seat { number: number, booked: false }
      }
  
      fn book(&amp;mut self) {
          self.booked = true;
      }
  }
  </code></pre>
  
  <p>Here, the <code>get_booking</code> method searches for a seat, returning a <code>Booking</code> with a reference to the seat it finds.</p>
  
  <pre><code>impl Theatre {
      fn get_booking&lt;'t&gt;(&amp;'t self, number: i32) -&gt; Option&lt;Booking&lt;'t&gt;&gt; {
          for row in self.rows.iter() {
              for seat in row.seats.iter() {
                  if seat.number == number &amp;&amp; seat.booked == false {
                      return Some(Booking { seats: vec![ seat ] })
                  }
              }
          }
  
          None
      }
  }
  </code></pre>
  
  <p>But this is where I get stuck. The <code>run</code> method has mutable access to the overall theatre (from its parameter), and it knows which seat to mutate (<code>self</code>). But since <code>self</code> isn't mutable, even though the theatre that contains it is, it can't be mutated.</p>
  
  <pre><code>struct Booking&lt;'t&gt; {
      seats: Vec&lt;&amp;'t Seat&gt;
  }
  
  impl&lt;'t&gt; Booking&lt;'t&gt; {
      fn describe(&amp;self) {
          let seats: Vec&lt;_&gt; = self.seats.iter().map(|s| s.number).collect();
          println!("You want to book seats: {:?}", seats);
      }
  
      fn run(&amp;self, _theatre: &amp;mut Theatre) {
          let mut seat = ??????;
          seat.book();
      }
  }
  </code></pre>
  
  <p>Finally, a main method that would use it if it worked.</p>
  
  <pre><code>fn main() {
      // Build a theatre (with only one seat... small theatre)
      let theatre = Theatre { rows: vec![ Row { seats: vec![ Seat::new(7) ] } ] };
      let wrapper = Arc::new(RwLock::new(theatre));
  
      // Try to book a seat in another thread
      let thread = thread::spawn(move || {
          let desired_seat_number = 7;
  
          let t = wrapper.read().unwrap();
          let booking = t.get_booking(desired_seat_number).expect("No such seat!");
  
          booking.describe();
  
          let mut tt = wrapper.write().unwrap();
          booking.run(&amp;mut tt);  // this is never actually reached because we still have the read lock
      });
  
      thread.join().unwrap();
  }
  </code></pre>
  
  <p>What's annoying is that I know exactly why my current code doesn't work - I just can't figure out how Rust wants my program formatted instead. There are some things I <em>don't</em> want to do:</p>
  
  <ul>
  <li>The simplest solution is to have <code>Booking</code> hold an index to its seat, instead of a reference: in this case, with <code>row</code> and <code>seat</code> <code>usize</code> fields. However, although my theatre uses O(1) vectors, I'd also like to reference a value in the middle of a large tree, where having to iterate to find the value would be much more expensive. This would also mean that you couldn't, say, get the seat number (in the <code>describe</code> function) without having to pass in the entire Theatre.</li>
  <li>It would also be solved by having a <code>Booking</code> hold a mutable reference to the seat, which I could just then mutate as normal. However, this would mean I could only have one proposed change at a time: I couldn't, for example, have a list of bookings and apply them all at once, or have two bookings and only apply one.</li>
  </ul>
  
  <p>I feel like I'm very close to having something that Rust will accept, but don't quite know how to structure my program to accommodate it. So, any pointers? (pun intended)</p>