title: Why does the order of borrowing matter in rust?
body: <pre class="lang-rust prettyprint-override"><code>fn say_hello(s: &amp;str) {
      println!(&quot;Hello {}&quot;, s);
  }
  </code></pre>
  <p>Why does this work</p>
  <pre class="lang-rust prettyprint-override"><code>fn main() {
      let mut name = String::from(&quot;Charlie&quot;);
      let x = &amp;mut name;
      say_hello(x);
      name.push_str(&quot; Brown&quot;);
  }
  </code></pre>
  <p>but this doesn't?</p>
  <pre class="lang-rust prettyprint-override"><code>fn main() {
      let mut name = String::from(&quot;Charlie&quot;);
      let x = &amp;mut name;
      name.push_str(&quot; Brown&quot;);
      say_hello(x);
  }
  </code></pre>
  <p>all I did was switch the order of the two functions but it seems like <code>x</code> has mutably borrowed name and push_str has also mutably borrowed name in both situations, so why does the first example compile?</p>
  <p>If I take out the call to <code>say_hello()</code> why does the order of the two not matter even though there are still two mutable borrows?</p>
  <p>Edit:
  Is this similar?</p>
  <pre class="lang-rust prettyprint-override"><code>fn change_string(s: &amp;mut String) { // s is mutably borrowed but isn't used yet
      println!(&quot;{}&quot;, s.is_empty()); // so the scopes don't overlap even though is_empty is making an immutable borrow?
      s.push_str(&quot; Brown&quot;);
  }
  </code></pre>