How to retrieve an io::Result<> error?

I use a function from_path() from escposify package. Here's their souce code:

pub fn from_path<P: AsRef<path::Path> + ToString>(path: P) -> io::Result<File<fs::File>> {
    let fobj = fs::OpenOptions::new()
     Ok(File { fobj })

This is how I use it :

// how to catch if the target_path is not available ?
// I can't use map_error() or ? operator on this since it's not a Result<File, Error>
// any error happens here, it will crash the entire program.
let file = File::<fs::File>::from_path(target_path);

You can, since io::Result<T> is just an alias for Result<T, io::Error>, and everything that works with plain Result will also work with io::Result.


By the way, if ? doesn't immediately work for some reason, you can still match on the result.

1 Like

well.. it turns out to be a bug with the escposify package that I use.

After a version update to 0.5.1, the package's author rewrite the File::from_path() function parameters and return type.

So the ? works now. I just aware of that version just now.

let file = File::<fs::File>::from_path(target_path)?;