title: Reborrowing of mutable reference
body: <p>When I wondered how a mutable reference could move into a method, all the questions began.</p>
  <pre><code>let a = &amp;mut x;
  a.somemethod(); // value of a should have moved
  a.anothermethod(); // but it works.
  </code></pre>
  <p>I've googled a lot. (really a lot)
  And I've noticed that a mutable reference passed as a parameter to a function, always undergoes the following transformation. (which is called reborrowing)</p>
  <pre><code>fn test(&amp;mut a) -&gt; ();
  
  let a = &amp;mut x;
  test(a); // what we write in code.
  test(&amp;mut *a); // the actual action in code.
  </code></pre>
  <p>So, I have googled more about &quot;reborrowing&quot;, for its detail.</p>
  <p>And this is what I've got.</p>
  <p>In any of codes, x refers to an arbitrary data. I don't mention it because I don't think the type of it is important for discussion. (However, I used i32 in my own).</p>
  <pre><code>let a = &amp;mut x;
  let b = &amp;mut *a; // a isn't available from now on
  *a = blahblah; // error! no more access allowed for a
  *b = blahblah; // I've wrote this code not to confuse you of the lifetime of b. If not mentioned, it always live till the scope ends.
  </code></pre>
  <pre><code>let a = &amp;mut x;
  {
      let b = &amp;*a;
      // *a = blahblah in this scope will throw an error, just like above case.
  }
  *a = blahblah; // but this works.
  </code></pre>
  <p>So alright. It's quite interesting. It seems like <code>b</code> borrows not only <code>x</code> but also <code>a</code>.</p>
  <p>Perhaps, we can clarify reborrowing like this : <code>&amp;'a *(&amp;'b mut x)</code>.
  It has borrowed <code>x</code> (which has a lifetime 'a in here),
  but also borrowed <code>a</code> (which has a lifetime 'b).</p>
  <p>So I ran the following code to confirm my conjecture.</p>
  <pre><code>let x: i32 = 1; // I wanted to make it clear that x lives in this scope.
  let b;
  {
      let a = &amp;mut x;
      b = &amp;mut *a;
  }
  *b = 0;
  </code></pre>
  <p>But this works!</p>
  <p>What??
  But I just decided to get this.
  <code>&amp;'a mut *&amp;mutx</code>, not <code>&amp;'a mut *&amp;'b mutx</code>.</p>
  <p>I had no idea why <code>mut x</code> is unavailable during the lifetime of <code>&amp;mut *&amp;mutx</code> nor why <code>mut x</code> is re-available after the lifetime of <code>&amp;mut *&amp;mutx</code>, but &quot;okay, let's just say so&quot;.</p>
  <p>But look at this. It's totally out of my mind for a clear and general understanding.</p>
  <pre><code>let x: i32 = 1;
  let b;
  {
      let a = &amp;mut x;
      let b = &amp;**&amp;a;
  } // error!! a should live longer than b!
  </code></pre>
  <p>Wasn't lifetime simply relying on what the real data <code>b</code> is referring to???
  <code>&amp;'a **&amp; &amp;mut x</code>, not <code>&amp;'a **&amp;'b &amp;'c mut x</code>.</p>
  <p>And now what??</p>
  <p><code>&amp;'a **&amp;'b &amp;mut x</code> ??? (which was my guess).</p>
  <p>How should I accept this complicated situation?</p>