title: In Rust, what exactly are mutable and immutable borrows?
body: <p>I'm stuck with the Rust concepts of <strong>borrowing</strong> and <strong>mutable</strong>:</p>
  
  <pre><code>#[derive(Debug)]
  struct Rectangle {
      height: u32,
      width: u32,
  }
  
  fn mut_area(rect_mut: &amp;mut Rectangle) -&gt; u32 {
      rect_mut.width /= 2;
      rect_mut.height * rect_mut.width
  }
  
  fn mut_string(s: &amp;mut String) -&gt; &amp;str {
      s.push_str("!");
      let len = s.len();
      &amp;s[0..len / 2]
  }
  
  fn main() {
      let mut rect = Rectangle {
          height: 50,
          width: 40,
      };
      println!("original rect: {:?}", rect);
      let a = mut_area(&amp;mut rect);
      println!("area of rect: {}", a);
      println!("now rect: {:?}", rect);
  
      let mut s = String::from("helloworld");
      println!("original s: {}", s);
      let half = mut_string(&amp;mut s);
      println!("half of the modified string: {}", half);
      println!("modified s: {}", s);
  }
  </code></pre>
  
  <p>When I tried to compile it, the compiler told me:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0502]: cannot borrow `s` as immutable because it is also borrowed as mutable
    --&gt; &lt;anon&gt;:32:32
     |
  30 |     let half = mut_string(&amp;mut s);
     |                                - mutable borrow occurs here
  31 |     println!("half of the modified string: {}", half);
  32 |     println!("modified s: {}", s);
     |                                ^ immutable borrow occurs here
  33 | }
     | - mutable borrow ends here
  </code></pre>
  
  <p>I know there's a rule about mutable reference:</p>
  
  <blockquote>
    <p>you can only have one mutable reference to a particular piece of data in a particular scope.</p>
  </blockquote>
  
  <p>But why can <code>rect</code> be borrowed but <code>s</code> cannot be? And how do I achieve what I want here - to print the modified string after the function call?</p>