Consider this code :
use std::io::Result;
use std::fs::{read_dir, DirEntry};
fn f() -> Result<Vec<DirEntry>> {
read_dir("some_dir")?
.map(|x| {
match x {
Ok(s) => s,
Err(e) => return Err(e),
}
})
.collect()
}
gives me a compilation error :
error[E0308]: mismatched types
--> src/lib.rs:9:26
|
9 | Ok(s) => s,
| ^ expected enum `std::result::Result`, found struct `std::fs::DirEntry`
|
= note: expected type `std::result::Result<_, std::io::Error>`
found type `std::fs::DirEntry`
This makes sense, since I am returning inside a closure so the return Err(e)
is a return of the closure not the f
function. How do I do this correctly though?
I don't want to use for
loops, as I am trying to do this functionally.
My first attempt was writing something like .map(|x| x?)
but of course that doesn't work for the same reasons.
Please help.