[Solved:] Variable binding: moving a &mut or borrowing the referent?


Hi all,

This code fails as expected at let c = a; with compile error "use of moved value: a ":

fn main() {
    let a: &mut i32 = &mut 0;
    let b = a;
    let c = a;

a is moved into b and is no longer available for an assignment to c. So far, so good.

However, if I just annotate b's type and leave everything else alone:

fn main() {
    let a: &mut i32 = &mut 0;
    let b: &mut i32 = a;
    let c = a;

the code fails again at let c = a;

But this time with a very different error message: "cannot move out of a because it is borrowed … borrow of *a occurs here: let b: &mut i32 = a;"

So, if I just annotate b's type: no move of a into b, but instead a “re”-borrow of *a?

What am I missing?


(cross-posted to http://stackoverflow.com/q/30535529/4951150)


The reborrow does no harm and does not pose a safety problem. That’s why nobody seemed to have noticed, I guess. But the docs say it should always be a move (at least what I’ve found).

It would be a problem though if code depended on a actually being moved instead of reborrowed. But I’m too fresh to Rust to come up with an example (or there is none).

vberger opened an issue at https://github.com/rust-lang/rust/issues/25899 .

So I mark this “solved” - kinda.