编译以下代码段:
use std::env;
fn main() {
let base = env::home_dir()
.map(|p| p.join(".foo"))
.map(|p| p.join("bar"))
.map(|p| p.display())
.expect("dir not loadable");
println!("Name: {}", base)
}
我收到错误:
error[E0597]: `p` does not live long enough
--> src/main.rs:7:18
|
7 | .map(|p| p.display())
| ^ - `p` dropped here while still borrowed
| |
| borrowed value does not live long enough
...
18 | }
| - borrowed value needs to live until here
这个错误的原因是什么?解决方案是什么?