What is the idiomatic way to handle/unwrap nested Result types?

大憨熊 提交于 2019-12-29 06:59:09

问题


I read that using unwrap on a Result is not a good practice in Rust and that it's better to use pattern matching so any error that occurred can be handled appropriately.

I get the point, but consider this snippet that reads a directory and prints the accessed time for each entry:

use std::fs;
use std::path::Path;

fn main() {
    let path = Path::new(".");
    match fs::read_dir(&path) {
        Ok(entries) => {
            for entry in entries {
                match entry {
                    Ok(ent) => {
                        match ent.metadata() {
                            Ok(meta) => {
                                match meta.accessed() {
                                    Ok(time) => {
                                        println!("{:?}", time);
                                    },
                                    Err(_) => panic!("will be handled")
                                }
                            },
                            Err(_) => panic!("will be handled")
                        }
                    },
                    Err(_) => panic!("will be handled")
                }
            }
        },
        Err(_) => panic!("will be handled")
    }
}

I want to handle every possible error in the code above (the panic macro is just a placeholder). While the code above works, I think it's ugly. What is the idiomatic way to handle a case like this?


回答1:


I read that using unwrap on a Result is not a good practice in Rust.

It's not that easy. For example, read my answer here to learn a bit more. Now to your main problem:


Reduce right shift by passing Ok value to the outside

One big issue with your code is the right shift: for example, the meta.accessed() call is indented a whole lot. We can avoid this by passing the value we want to work with out of the match:

let entries = match fs::read_dir(&path) {
    Ok(entries) => entries, // "return" from match
    Err(_) => panic!("will be handled"),
};

for entry in entries {  // no indentation! :)
    // ...
}

That's already a very good way to make the code more readable.

Using the ? operator to pass the error to the calling function

Your function could return a Result<_, _> type in order to pass the error to the calling function (yes, even main() can return Result). In this case you can use the ? operator:

use std::{fs, io};

fn main() -> io::Result<()> {
    for entry in fs::read_dir(".")? {
        println!("{:?}", entry?.metadata()?.accessed()?);
    }
    Ok(())
}

Use helper methods of Result

There are also many helper methods, like map() or and_then(), for the Result type. and_then is helpful if you want to do something, if the result is Ok and this something will return a result of the same type. Here is your code with and_then() and manual handling of the error:

fn main() {
    let path = Path::new(".");
    let result = fs::read_dir(&path).and_then(|entries| {
        for entry in entries {
            let time = entry?.metadata()?.accessed()?;
            println!("{:?}", time);
        }
        Ok(())
    });

    if let Err(e) = result {
        panic!("will be handled");
    }
}

There really isn't only one way to do this kind of error handling. You have to get to know all the tools you can use and then need to choose the best for your situation. However, in most situations, the ? operator is the right tool.




回答2:


Result happens to have a lot of convenience methods for these kinds of things:

use std::fs;
use std::path::Path;

fn main() {
    let path = Path::new(".");
    match fs::read_dir(&path) {
        Ok(entries) => {
            for entry in entries {
                match entry.and_then(|e| e.metadata()).map(|m| m.accessed()) {
                    Ok(time) => {
                        println!("{:?}", time);
                    },
                    Err(_) => panic!("will be handled")
                }
            }
        },
        Err(_) => panic!("will be handled")
    }
}

And usually you will not have so much logic in main and will simply be able to use ? or try! in another function:

use std::fs;
use std::path::Path;

fn print_filetimes(path: &Path) -> Result<(), std::io::Error> {
    for entry in fs::read_dir(&path)? {
        let time = entry.and_then(|e| e.metadata()).map(|m| m.accessed())?;
        println!("{:?}", time);
    }

    Ok(())
}

fn main() {
    let path = Path::new(".");
    match print_filetimes(path) {
        Ok(()) => (),
        Err(_) => panic!("will be handled"),
    }
}


来源:https://stackoverflow.com/questions/39805834/what-is-the-idiomatic-way-to-handle-unwrap-nested-result-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!