fn f<'a>(_: &'a mut &'a u8) {}
fn main() {
let mut data = &1;
{
let tmp_mut = &mut data;
f(tmp_mut);
}
println!("{:p}", data);
}
error[E0502]: cannot borrow `data` as immutable because it is also borrowed as mutable
--> src/main.rs:11:22
|
8 | let tmp_mut = &mut data;
| ---- mutable borrow occurs here
...
11 | println!("{:p}", data);
| ^^^^ immutable borrow occurs here
12 | }
| - mutable borrow ends here
This code can be fixed by not using the same lifetime in the function signature (just leave fn f(_: &mut &u8) {}
), but what exactly is going on here?
The error message looks a little bit strange - it seems counterintuitive that tmp_mut
tries to live more than the block where it was declared.
I would expect the error message to grumble about impossible lifetime requirements, not about a uniqueness error.