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
可以通过在函数签名中不使用相同的生存期(只需离开fn f(_: &mut &u8) {}
)来修复此代码,但是这里到底发生了什么?
错误消息看起来有些奇怪-tmp_mut
尝试的生存时间超过声明它的块,这似乎是违反直觉的。
我希望错误消息会抱怨不可能的寿命要求,而不是唯一性错误。