title: Rust language : immutable borrow and mutable borrow error (2 cases)
body: <p>I am learning rust and am looking at compiler errors in learning. However, I have one case of mutable and immutable borrow and I am getting no error in the first case, but I get an error in the second, just by uncommenting a println. <strong>I, expected an error in both</strong>.</p>
  <p>case 1 (this runs):</p>
  <pre><code>fn main() {
  let mut s = String::from(&quot;Hello&quot;);
  
  // does xyz below have a immutable reference to s?
  let xyz = replace_one(&amp;mut s);
  
  // if xyz has a immutable reference to s, then how does pol work?
  let pol = &amp;mut s;
  
  pol.replace_range(0.., &quot;changed value&quot;);
  println!(&quot;pol is {}&quot;, pol);
  
  // by uncommenting this in the second case, I get an error, of immutable and mutable borrow.
  // println!(&quot;{}&quot;,xyz); 
  
  }
  
  fn replace_one(data: &amp;mut String) -&gt; &amp;String {
      data.replace_range(0.., &quot;Howdy.&quot;);
      data
  }
  </code></pre>
  <p>I am <strong>assuming</strong> here that the xyz has a immutable reference to s. There is no function or macro consuming xyz, so therefore, pol shouldnt work (?), given that they are in the same scope. Now, in the second case, I uncomment the println statement and it throws an error.</p>
  <p>case 2 (this doesnt):</p>
  <pre><code>fn main() {
      let mut s = String::from(&quot;Hello&quot;);
  
  
  let xyz = replace_one(&amp;mut s);
  
  
  let pol = &amp;mut s;
  
  pol.replace_range(0.., &quot;changed value&quot;);
  println!(&quot;pol is {}&quot;, pol);
  
  //  uncommenting this, throws an error showing that I have a immutable borrow at xyz 
  // and a mutable borrow at pol.
      println!(&quot;{}&quot;,xyz);
  
  }
  
  fn replace_one(data: &amp;mut String) -&gt; &amp;String {
      data.replace_range(0.., &quot;Howdy.&quot;);
      data
  }
  </code></pre>
  <p>I am a newbie to rust. Would be helpful if you can guide me to any resources on this.</p>