Trait core2::error::Error [−][src]
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 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.