title: How to resolve lifetime error for mutable reference in Rust?
body: <p>I am not sure why the following code does not compile.</p>
  
  <pre><code>use std::cmp::Ordering;
  
  struct MyItr&lt;'a&gt; {
      cur: &amp;'a i32,
  }
  
  impl&lt;'a&gt; Ord for MyItr&lt;'a&gt; {
      fn cmp(&amp;self, other: &amp;MyItr) -&gt; Ordering {
          self.cur.cmp(&amp;other.cur)
      }
  }
  
  impl&lt;'a&gt; PartialOrd for MyItr&lt;'a&gt; {
      fn partial_cmp(&amp;self, other: &amp;MyItr&lt;'a&gt;) -&gt; Option&lt;Ordering&gt; {
          Some(self.cmp(other))
      }
  }
  
  impl&lt;'a&gt; PartialEq for MyItr&lt;'a&gt; {
      fn eq(&amp;self, other: &amp;MyItr) -&gt; bool {
          self.cur == other.cur
      }
  }
  
  impl&lt;'a&gt; Eq for MyItr&lt;'a&gt; {}
  
  fn f0&lt;'a&gt;(t0: &amp;'a mut MyItr&lt;'a&gt;, t1: &amp;'a mut MyItr&lt;'a&gt;, i: &amp;'a i32) {
      let t = std::cmp::max(t0, t1);
      t.cur = i;
  }
  
  fn f1() {
      let i0 = 1;
      let i1 = 2;
      let mut z0 = MyItr { cur: &amp;i0 };
      let mut z1 = MyItr { cur: &amp;i1 };
  
      let i2 = 3;
      f0(&amp;mut z0, &amp;mut z1, &amp;i2);
  }
  </code></pre>
  
  <pre class="lang-none prettyprint-override"><code>$ cargo build
     Compiling foo v0.1.0 (file:///private/tmp/foo)
  error: `z1` does not live long enough
    --&gt; lib.rs:40:1
     |
  39 |     f0(&amp;mut z0, &amp;mut z1, &amp;i2);
     |                      -- borrow occurs here
  40 | }
     | ^ `z1` dropped here while still borrowed
     |
     = note: values in a scope are dropped in the opposite order they are created
  </code></pre>
  
  <p>My understanding is the borrowed references to <code>z0</code> and <code>z1</code> are backed once the <code>f0</code> invocation ends. However, The compiler seems to assume the borrowed references are not backed.</p>
  
  <pre class="lang-none prettyprint-override"><code>$ cargo --version
  cargo 0.20.0-nightly (41e490480 2017-05-16)
  </code></pre>