Skip to content

Instantly share code, notes, and snippets.

@vittorioromeo
Created June 17, 2017 18:12
Show Gist options
  • Save vittorioromeo/f45297143923478d775f16fe05de3eee to your computer and use it in GitHub Desktop.
Save vittorioromeo/f45297143923478d775f16fe05de3eee to your computer and use it in GitHub Desktop.

I'm having some trouble with the borrow checker. I want to do this:

 for entry in WalkDir::new(".")
    .into_iter()
    .filter_map(|e| e.ok())
    .map(|e| e.path().parent().unwrap())
{}

but I get

  --> src/main.rs:32:43
   |
32 |         .map(|e| e.path().parent().unwrap())
   |                  - borrow occurs here     ^ `e` dropped here while still borrowed
33 |     {
34 |     }
   |     - borrowed value needs to live until here

I think that this happens because Path::parent returns an Option<&Path> This is what I'm doing now...

for entry in WalkDir::new(opt.path)
    .into_iter()
    .filter_map(|e| e.ok())
    .map(|e| {
        e.path().parent().and_then(|x| x.to_str()).map(
            |x| x.to_string(),
        )
    })
{}

I can work around this by having

let parent = entry.path().parent().unwrap();

inside the body of the loop, but I would like a "nice" functional chain. Is a ref_map (or whatever it makes sense to call it) missing by design?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment