title: Why does this fail to compile?
body: <p>I'm trying to get a reference to the tail of a linked list, and here's the code I wrote:</p>
  <pre class="lang-rust prettyprint-override"><code>pub struct ListNode {
      pub next: Option&lt;Box&lt;ListNode&gt;&gt;,
  }
  
  fn tail(mut head: &amp;mut Option&lt;Box&lt;ListNode&gt;&gt;) -&gt; &amp;mut Option&lt;Box&lt;ListNode&gt;&gt; {
      while let Some(x) = head.as_mut() {
          head = &amp;mut x.next;
      }
      head
  }
  </code></pre>
  <p>This fails to compile because it thinks I'm borrowing head as mutable twice. I was able to get to compile by changing it to this:</p>
  <pre class="lang-rust prettyprint-override"><code>fn tail(mut head: &amp;mut Option&lt;Box&lt;ListNode&gt;&gt;) -&gt; &amp;mut Option&lt;Box&lt;ListNode&gt;&gt; {
      while head.is_some() {
          head = &amp;mut head.as_mut().unwrap().next;
      }
      head
  }
  </code></pre>
  <p>It appears to me that these two functions do the exact same thing, but the first one looks a lot cleaner. Why doesn't it compile? Is there a better way of doing this than what I changed it to?</p>