我试图通过Programming Rust这本书来理解生命周期,并在这里做了一个自定义示例:
fn simple_fn(point: [&i32; 3]) -> (&i32, &i32) {
(point[0], point[2])
}
fn main() {
let mut point: [&i32; 3] = [&19, &18, &17];
{
let x = 3;
point[1] = &x;
point[1] = &10;
}
let t = simple_fn(point);
println!("{:?}", t);
}
编译器返回错误:
error[E0597]: `x` does not live long enough
--> src/main.rs:10:20
|
9 | let x = 3;
| - binding `x` declared here
10 | point[1] = &x;
| ^^ borrowed value does not live long enough
11 | point[1] = &10;
12 | }
| - `x` dropped here while still borrowed
13 |
14 | let t = simple_fn(point);
| ----- borrow later used here
我不明白,当我把point[1]
重新赋值给&10
,就在赋值给x
之后,为什么point[1]
的生命周期 仍然限于x
的生命周期 ?