借用值在Option :: map中的活动时间不够长

时间:2018-05-10 00:31:53

标签: rust rust-cargo

编译以下代码段:

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

这个错误的原因是什么?解决方案是什么?

0 个答案:

没有答案