How to manually return a Result<(), Box>?

后端 未结 4 949
傲寒
傲寒 2021-02-02 16:05

I want to return an error from a function in case a condition is true:

use std::error::Error;

pub fn run() -> Result<(), Box> {
    //         


        
4条回答
  •  [愿得一人]
    2021-02-02 16:51

    Error is a trait and you want to return a trait object (note the dyn keyword), so you need to implement this trait:

    use std::error::Error;
    use std::fmt;
    
    #[derive(Debug)]
    struct MyError(String);
    
    impl fmt::Display for MyError {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(f, "There is an error: {}", self.0)
        }
    }
    
    impl Error for MyError {}
    
    pub fn run() -> Result<(), Box> {
        let condition = true;
    
        if condition {
            return Err(Box::new(MyError("Oops".into())));
        }
    
        Ok(())
    }
    
    fn main() {
        if let Err(e) = run() {
            println!("{}", e); // "There is an error: Oops"
        }
    }
    
    • Create your own error type,
    • Implement Debug, Display, then Error for it,
    • If there is an error, return the Err variant of Result.

    I advise you to use failure that remove all the error boilerplate:

    #[derive(Fail, Debug)]
    #[fail(display = "There is an error: {}.", _0)]
    struct MyError(String);
    

    --

    Note that if you expect an Error, you can return whatever type you want, given that it implements Error. This includes the error types in std.

提交回复
热议问题