Moved variable still borrowing after calling `drop`?

I can’t give you a definite answer, but I’ll try to explain a few things here. Let’s start with clarifying something:

The compiler knows drop() drops x

This is not true. While there are a few “magic” things in the standard library that the compiler knows about, drop() is not such a lang item. In fact, you could implement drop() yourself and it’s actually the easiest thing to do:

fn drop<T>(_: T) {}

The function just takes something by value (thus, it’s moved into drop()) and since nothing happens inside of drop(), this value is dropped at the end of the scope, like in any other function. So: the compiler doesn’t know x is dropped, it just knows x is moved.


As you might have noticed, the compiler error stays the same regardless of whether or not we add the drop() call. Right now, the compiler will only look at the scope of a variable when it comes to references. From Niko Matsakis’ intro to NLL:

The way that the compiler currently works, assigning a reference into a variable means that its lifetime must be as large as the entire scope of that variable.

And in a later blog post of his:

In particular, today, once a lifetime must extend beyond the boundaries of a single statement […], it must extend all the way till the end of the enclosing block.

This is exactly what happens here, so yes, your problem has to do with all this “lexical borrowing” stuff. From the current compilers perspective, the lifetime of the expression &a needs to be at least as large as the scope of x. But this doesn’t work, since the reference would outlive a, since the scope of x is larger than the scope of a as pointed out by the compiler:

= note: values in a scope are dropped in the opposite order they are created

And I guess you already know all that, but you can fix your example by swapping the lines let mut x ...; and let a ...;.


I’m not sure whether or not this exact problem would be solved by any of the currently proposed solutions. But I hope that we will see soon enough, as all of this is being addressed as part of the Rust 2017 roadmap. A good place to read up on the updates is here (which also contains links to the five relevant blog posts of Niko).

Leave a Comment