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?