title: Linking the lifetimes of self and a reference in method
body: <p>I have <a href="https://play.rust-lang.org/?gist=51474ebe468347034d0cbb930b619c9d&amp;version=nightly" rel="noreferrer">this piece of code</a>:</p>
  
  <pre><code>#[derive(Debug)]
  struct Foo&lt;'a&gt; {
      x: &amp;'a i32,
  }
  
  impl&lt;'a&gt; Foo&lt;'a&gt; {
      fn set(&amp;mut self, r: &amp;'a i32) {
          self.x = r;
      }
  }
  
  fn main() {
      let v = 5;
      let w = 7;
      let mut f = Foo { x: &amp;v };
  
      println!("f is {:?}", f);
  
      f.set(&amp;w);
  
      println!("now f is {:?}", f);
  }
  </code></pre>
  
  <p>My understanding is that in the first borrow of the value of <code>v</code>, the generic lifetime parameter <code>'a</code> on the struct declaration is filled in with the lifetime of the value of <code>v</code>. This means that the resulting <code>Foo</code> object must not live longer than this <code>'a</code> lifetime or that the value of <code>v</code> must live at least as long as the <code>Foo</code> object.</p>
  
  <p>In the call to the method <code>set</code>, the lifetime parameter on the <code>impl</code> block is used and the lifetime of the value of <code>w</code> is filled in for <code>'a</code> in the method signature. <code>&amp;mut self</code> is assigned a different lifetime by the compiler, which is the lifetime of <code>f</code> (the <code>Foo</code> object). If I switched the order of the bindings of <code>w</code> and <code>f</code> in the <code>main</code> function, this would result in an error.</p>
  
  <p>I wondered what would happen if I annotated the <code>&amp;mut self</code> reference with the same lifetime parameter <code>'a</code> as <code>r</code> in the <code>set</code> method:</p>
  
  <pre><code>impl&lt;'a&gt; Foo&lt;'a&gt; {
      fn set(&amp;'a mut self, r: &amp;'a i32) {
          self.x = r;
      }
  }
  </code></pre>
  
  <p>Which results in the following error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0502]: cannot borrow `f` as immutable because it is also borrowed as mutable
    --&gt; src/main.rs:21:31
     |
  19 |     f.set(&amp;w);
     |     - mutable borrow occurs here
  20 | 
  21 |     println!("now f is {:?}", f);
     |                               ^ immutable borrow occurs here
  22 | }
     | - mutable borrow ends here
  </code></pre>
  
  <p>In contrast to the example above, <code>f</code> is still considered mutably borrowed by the time the second println! is called, so it cannot be borrowed simultaneously as immutable. </p>
  
  <p>How did this come to be?</p>
  
  <p>By not leaving off the lifetime annotation the compiler filled one in for me for <code>&amp;mut self</code> in the first example. This happens by the rules of lifetime elision. However by explicitly setting it to <code>'a</code> in the second example I linked the lifetimes of the value of <code>f</code> and the value of <code>w</code>.</p>
  
  <p>Is <code>f</code> considered borrowed by itself somehow?</p>
  
  <p>And if so, what is the scope of the borrow? Is it min(lifetime of <code>f</code>, lifetime of <code>w</code>) -> lifetime of <code>f</code>?</p>
  
  <p>I assume I haven't fully understood the <code>&amp;mut self</code> reference in the function call yet. I mean, the function returns, but <code>f</code> is still considered to be borrowed.</p>
  
  <p>I am trying to fully understand lifetimes. I am primarily looking for corrective feedback on my understanding of the concepts. I am grateful for every bit of advice and further clarification.</p>