Trait core2::error::Error

source ·
pub trait Error: Debug + Display {
    // Provided method
    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§

source

fn source(&self) -> Option<&(dyn Error + 'static)>

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§

source§

impl dyn Error + 'static

source

pub fn is<T: Error + 'static>(&self) -> bool

Returns true if the boxed type is the same as T

source

pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T>

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

source

pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T>

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

source§

impl dyn Error + Send + 'static

source

pub fn is<T: Error + 'static>(&self) -> bool

Forwards to the method defined on the type dyn Error.

source

pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T>

Forwards to the method defined on the type dyn Error.

source

pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T>

Forwards to the method defined on the type dyn Error.

source§

impl dyn Error + Send + Sync + 'static

source

pub fn is<T: Error + 'static>(&self) -> bool

Forwards to the method defined on the type dyn Error.

source

pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T>

Forwards to the method defined on the type dyn Error.

source

pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T>

Forwards to the method defined on the type dyn Error.

Implementations on Foreign Types§

source§

impl Error for ParseIntError

source§

impl Error for TryFromSliceError

source§

impl Error for ParseBoolError

source§

impl Error for TryFromIntError

source§

impl Error for Utf8Error

source§

impl Error for ParseFloatError

source§

impl Error for LayoutErr

Implementors§