pub trait Error: Debug + Display {
    fn source(&self) -> Option<&(dyn Error + 'static)> { ... }
}
Expand description

Error is a trait representing the basic expectations for error values, i.e., values of type E in Result<T, E>. Errors must describe themselves through the Display and Debug traits, and may provide cause chain information:

Error::source() is generally used when errors cross “abstraction boundaries”. If one module must report an error that is caused by an error from a lower-level module, it can allow accessing that error via Error::source(). This makes it possible for the high-level module to provide its own errors while also revealing some of the implementation for debugging via source chains.

Provided methods

The lower-level source of this error, if any.

Examples
use std::error::Error;
use std::fmt;

#[derive(Debug)]
struct SuperError {
    side: SuperErrorSideKick,
}

impl fmt::Display for SuperError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SuperError is here!")
    }
}

impl Error for SuperError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        Some(&self.side)
    }
}

#[derive(Debug)]
struct SuperErrorSideKick;

impl fmt::Display for SuperErrorSideKick {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SuperErrorSideKick is here!")
    }
}

impl Error for SuperErrorSideKick {}

fn get_super_error() -> Result<(), SuperError> {
    Err(SuperError { side: SuperErrorSideKick })
}

fn main() {
    match get_super_error() {
        Err(e) => {
            println!("Error: {}", e);
            println!("Caused by: {}", e.source().unwrap());
        }
        _ => println!("No error"),
    }
}

Implementations

Returns true if the boxed type is the same as T

Returns some reference to the boxed value if it is of type T, or None if it isn’t.

Returns some mutable reference to the boxed value if it is of type T, or None if it isn’t.

Forwards to the method defined on the type dyn Error.

Forwards to the method defined on the type dyn Error.

Forwards to the method defined on the type dyn Error.

Forwards to the method defined on the type dyn Error.

Forwards to the method defined on the type dyn Error.

Forwards to the method defined on the type dyn Error.

Attempts to downcast the box to a concrete type.

Attempts to downcast the box to a concrete type.

Attempts to downcast the box to a concrete type.

Implementations on Foreign Types

Implementors