这段代码从stdin读取一行,我想分割它。
use std::io::stdin;
fn example() {
let mut input = String::new();
stdin().read_line(&mut input).expect("Failed to read line");
let mut parts = input.trim().split_whitespace();
let args = parts;
let new_dir = args.peekable().peek().unwrap();
println!("{}", new_dir);
}
编译器说:
error[E0597]: borrowed value does not live long enough
--> src/lib.rs:8:19
|
8 | let new_dir = args.peekable().peek().unwrap();
| ^^^^^^^^^^^^^^^ - temporary value dropped here while still borrowed
| |
| temporary value does not live long enough
9 | println!("{}", new_dir);
10 | }
| - temporary value needs to live until here
|
= note: consider using a `let` binding to increase its lifetime
我了解这意味着args.peekable()
创建了一个临时值,而println!("{}", new_dir);
借用了该值。
我不知道临时值在哪里。我以为借来的值是从args
借来的,这里不会删除。
args.peekable().peek().unwrap();
中到底发生了什么?