我的错误是什么以及如何解决?
fn get_m() -> Vec<i8> {
vec![1, 2, 3]
}
fn main() {
let mut vals = get_m().iter().peekable();
println!("Saw a {:?}", vals.peek());
}
(操场)
编译器的错误提示“考虑使用let
绑定”——但我已经是:
error[E0597]: borrowed value does not live long enough
--> src/main.rs:6:45
|
6 | let mut vals = get_m().iter().peekable();
| ------- ^ temporary value dropped here while still borrowed
| |
| temporary value created here
7 | println!("Saw a {:?}", vals.peek());
8 | }
| - temporary value needs to live until here
|
= note: consider using a `let` binding to increase its lifetime
这显然是一个新手问题——尽管我认为此时我已经写了足够多的 Rust,以至于我已经掌握了借用检查器......显然我没有。
这个问题类似于Using a `let` binding to increase value lifetime,但不涉及将表达式分解为多个语句,所以我认为问题并不相同。