title: Why can we have multiple mutable references to the same data in the same scope?
body: <p>From Rust documentations:</p>
  
  <blockquote>
    <p>mutable references have one big restriction: you can have only one mutable reference to a particular piece of data in a particular scope.</p>
  </blockquote>
  
  <p>The example in the docs does support this statement:</p>
  
  <pre><code>let mut s = String::from("hello");
  let r1 = &amp;mut s;
  let r2 = &amp;mut s;
  println!("{}, {}", r1, r2);
  </code></pre>
  
  <p>But this is a bit misleading to me. When print statement in the last line is removed the program compiles and runs fine (although with a couple of warnings). Later in the docs they say this is how rust deals with data races. I haven't reached the point where they explain threads and parallel programming but I guess it's still worthwhile to ask how are we preventing the data race in the following case:</p>
  
  <pre><code>fn main() {
      let mut name: String = String::from("Devashish");
  
      // assume that fun1 was called from a new thread
      fun1(&amp;mut name);
  
      // fun1 and fun2 are being executed in parallel at this point
      fun2(&amp;mut name);
  }
  
  fn fun1(re: &amp;mut String) {
      println!("From fun1: {}", re);
  }
  
  fn fun2(re: &amp;mut String) {
      println!("From fun2: {}", re);
  }
  </code></pre>
  
  <p>How are we preventing data race in the case above?</p>