2

Compiling the following snippet:

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)
}

I get the error:

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

What is the reason of that mistake? What is the solution?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Rudolf Schmidt
  • 2,345
  • 5
  • 21
  • 28
  • I believe your question is already answered by [Result of Option::map does not live long enough](https://stackoverflow.com/q/35249309/155423). If you disagree, please [edit] your question to explain the differences. Otherwise, we can mark this as already answered. – Shepmaster May 10 '18 at 02:07
  • It is also answered by [Lifetime of Option::map's argument](https://stackoverflow.com/q/26056210/155423). – Shepmaster May 10 '18 at 02:24
  • 1
    The duplicate's information [applied to your situation](https://play.rust-lang.org/?gist=a8e0b0d861cc8e46fb0f006aaef175fa&version=stable&mode=debug). – Shepmaster May 10 '18 at 02:43
  • Possible duplicate of [Result of Option::map does not live long enough](https://stackoverflow.com/questions/35249309/result-of-optionmap-does-not-live-long-enough) – Dan Hulme May 15 '18 at 07:52

0 Answers0