title: Who borrowed a variable?
body: <p>I'm fighting with the borrow checker. I have two similar pieces of code, one working as I expect, and the other not.</p>
  
  <p>The one that works as I expect:</p>
  
  <pre><code>mod case1 {
      struct Foo {}
  
      struct Bar1 {
          x: Foo,
      }
  
      impl Bar1 {
          fn f&lt;'a&gt;(&amp;'a mut self) -&gt; &amp;'a Foo {
              &amp;self.x
          }
      }
  
      // only for example
      fn f1() {
          let mut bar = Bar1 { x: Foo {} };
          let y = bar.f(); // (1) 'bar' is borrowed by 'y'
          let z = bar.f();  // error (as expected) : cannot borrow `bar` as mutable more
                             // than once at a time [E0499]
      }
  
      fn f2() {
          let mut bar = Bar1 { x: Foo {} };
          bar.f(); // (2) 'bar' is not borrowed after the call
          let z = bar.f();  // ok (as expected)
      }
  }
  </code></pre>
  
  <p>The one that doesn't:</p>
  
  <pre><code>mod case2 {
      struct Foo {}
  
      struct Bar2&lt;'b&gt; {
          x: &amp;'b Foo,
      }
  
      impl&lt;'b&gt; Bar2&lt;'b&gt; {
          fn f(&amp;'b mut self) -&gt; &amp;'b Foo {
              self.x
          }
      }
  
      fn f4() {
          let foo = Foo {};
          let mut bar2 = Bar2 { x: &amp;foo };
          bar2.f(); // (3) 'bar2' is borrowed as mutable, but who borrowed it?
          let z = bar2.f(); // error: cannot borrow `bar2` as mutable more than once at a time [E0499]
      }
  }
  </code></pre>
  
  <p>I hoped I could call <code>Bar2::f</code> twice without irritating the compiler, as in case 1.</p>
  
  <p>The question is in the comment (3): who borrowed <code>bar2</code>, whereas there is no affectation? </p>
  
  <p>Here's what I understand: </p>
  
  <ol>
  <li><p>In case 1, <code>f2</code> call: the lifetime parameter <code>'a</code> is the one of the receiving <code>&amp;Foo</code> value, so this lifetime is empty when there is no affectation, and <code>bar</code> is not borrowed after the <code>Bar1::f</code> call;</p></li>
  <li><p>In case 2, <code>bar2</code> borrows <code>foo</code> (as immutable), so the lifetime parameter <code>'b</code> in <code>Bar2</code> struct is the <code>foo</code> reference lifetime, which ends at the end of <code>f4</code> body. Calling <code>Bar2::f</code> borrows <code>bar2</code> for that lifetime, namely to the end of <code>f4</code>.</p></li>
  </ol>
  
  <p>But the question is still: who borrowed <code>bar2</code>? Could it be <code>Bar2::f</code>? How <code>Bar2::f</code> would hold the borrowed ownership after the call? What am I missing here?</p>
  
  <p>I'm using Rust 1.14.0-nightly (86affcdf6 2016-09-28) on x86_64-pc-windows-msvc.</p>