Where the compiler can choose between moving a borrowed reference and reborrowing, it will choose reborrowing. That's what's happening here: b
reborrows the reference from a
, and attempting to touch a
itself or create any other references to the referrent of a
while b
is in scope will cause that error. Putting b
in a more limited scope allows the rest of that program to compile:
fn main() {
let a : &mut Box<usize> = &mut Box::new(0);
{
let b : &mut Box<usize> = a;
} // b goes out of scope and releases its borrow here
let c : &Box<usize> = a;
let d : &Box<usize> = &a;
}
This thread goes into more detail about reborrowing, and cites some useful sources.