1.0.0[][src]Trait std::error::Error

pub trait Error: Debug + Display {
    fn description(&self) -> &str { ... }
fn cause(&self) -> Option<&Error> { ... }
fn source(&self) -> Option<&(Error + 'static)> { ... } }
[]

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:

The cause method is generally used when errors cross "abstraction boundaries", i.e. when a one module must report an error that is "caused" by an error from a lower-level module. This setup makes it possible for the high-level module to provide its own errors that do not commit to any particular implementation, but also reveal some of its implementation for debugging via cause chains.

Provided Methods

[]

This method is soft-deprecated.

Although using it won’t cause compilation warning, new code should use Display instead and new impls can omit it.

To obtain error description as a string, use to_string().

Examples

match "xc".parse::<u32>() {
    Err(e) => {
        // Print `e` itself, not `e.description()`.
        println!("Error: {}", e);
    }
    _ => println!("No error"),
}Run

[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

The lower-level cause 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 description(&self) -> &str {
        "I'm the superhero of errors"
    }

    fn cause(&self) -> Option<&Error> {
        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 description(&self) -> &str {
        "I'm SuperError side kick"
    }
}

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

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

[]

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 description(&self) -> &str {
        "I'm the superhero of errors"
    }

    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 description(&self) -> &str {
        "I'm SuperError side kick"
    }
}

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

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

Methods

impl Error + 'static
[src]
[]

[]

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.

impl Error + Send + 'static
[src]
[]

[]

Forwards to the method defined on the type Any.

[]

Forwards to the method defined on the type Any.

[]

Forwards to the method defined on the type Any.

impl Error + Send + Sync + 'static
[src]
[]

[]

Forwards to the method defined on the type Any.

[]

Forwards to the method defined on the type Any.

[]

Forwards to the method defined on the type Any.

impl Error
[src]
[]

[]

Attempt to downcast the box to a concrete type.

impl Error + Send
[src]
[]

[]

Attempt to downcast the box to a concrete type.

impl Error + Send + Sync
[src]
[]

[]

Attempt to downcast the box to a concrete type.

Implementations on Foreign Types

impl Error for TryFromSliceError
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

Implementors

impl Error for VarError
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for ParseError
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for RecvTimeoutError
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for TryRecvError
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for !
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for AllocErr
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for CannotReallocInPlace
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for LayoutErr
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for BorrowError
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for BorrowMutError
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for CharTryFromError
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for DecodeUtf16Error
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for ParseCharError
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for JoinPathsError
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for FromBytesWithNulError
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for IntoStringError
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for NulError
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for std::fmt::Error
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for std::io::Error
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for AddrParseError
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for ParseFloatError
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for ParseIntError
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for TryFromIntError
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for StripPrefixError
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for ParseBoolError
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for Utf8Error
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for FromUtf16Error
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for FromUtf8Error
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for RecvError
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl Error for SystemTimeError
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl<T> Error for TryLockError<T>
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl<T> Error for PoisonError<T>
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl<T: Error> Error for Box<T>
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl<T: Send> Error for TrySendError<T>
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl<T: Send> Error for SendError<T>
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting

impl<W: Send + Debug> Error for IntoInnerError<W>
[src]
[]

Deprecating in 1.33.0

: replaced by Error::source, which can support downcasting