Cannot borrow variable when borrower scope ends
<p>I can't understand why a mutable borrowed variable is still borrowed after the scope of the borrower ends. It looks like it is related to trait usage, but I don't see why:</p>

<pre><code>fn main() {
    let mut a = 10;
    test::&lt;FooS&gt;(&amp;mut a);
    println!("out {:?}", a)
}

trait Foo&lt;'a&gt; {
    fn new(data: &amp;'a mut u32) -&gt; Self;
    fn apply(&amp;mut self);
}

struct FooS&lt;'a&gt; {
    data: &amp;'a mut u32,
}

impl&lt;'a&gt; Foo&lt;'a&gt; for FooS&lt;'a&gt; {
    fn new(data: &amp;'a mut u32) -&gt; Self {
        FooS { data: data }
    }

    fn apply(&amp;mut self) {
        *self.data += 10;
    }
}

fn test&lt;'a, F&gt;(data: &amp;'a mut u32)
    where F: Foo&lt;'a&gt;
{
    {
        // let mut foo = FooS {data: data}; // This works fine
        let mut foo: F = Foo::new(data);
        foo.apply();
    } // foo scope ends here
    println!("{:?}", data); // error
} // but borrowed till here
</code></pre>

<p><a href="https://play.rust-lang.org/?gist=2efab9502dcf3d5fd5ffe74972aa31a3&amp;version=stable&amp;backtrace=0" rel="nofollow">try online</a></p>

<pre class="lang-none prettyprint-override"><code>error: cannot borrow `data` as immutable because `*data` is also borrowed as mutable [--explain E0502]
   --&gt; &lt;anon&gt;:34:22
31  |&gt;         let mut foo: F = Foo::new(data);
    |&gt;                                   ---- mutable borrow occurs here
...
34  |&gt;     println!("{:?}", data); // error
    |&gt;                      ^^^^ immutable borrow occurs here
35  |&gt; } // but borrowed till here
    |&gt; - mutable borrow ends here
</code></pre>