【发布时间】:2019-01-06 13:14:11
【问题描述】:
fn main() {
// block1: fails
{
let mut m = 10;
let n = {
let b = &&mut m;
&**b // just returning b fails
};
println!("{:?}", n);
}
// block2: passes
{
let mut m = 10;
let n = {
let b = &&m;
&**b // just returning b fails here too
};
println!("{:?}", n);
}
}
block1 失败并出现以下错误:
error[E0597]: borrowed value does not live long enough
--> src/main.rs:7:22
|
7 | let b = &&mut m;
| ^^^^^^ temporary value does not live long enough
8 | &**b // just returning b fails
9 | };
| - temporary value dropped here while still borrowed
...
12 | }
| - temporary value needs to live until here
我是否正确假设内部不可变引用超出了 block2 范围,而在 block1 中,即使存在外引用呢?
【问题讨论】:
标签: rust mutable dereference borrowing