Skip to main content

ErrorKind

Trait ErrorKind 

Source
pub trait ErrorKind:
    Clone
    + Copy
    + Debug
    + Display { }
Expand description

A marker trait for a type that denotes a kind of error that took place.

These types are expected to be very simple enums, which contain only unit variants denoting the kind of error that occurred.

If you want to compose an ErrorKind which is a combination of multiple different ErrorKind, see the ErrorDomain trait.

§Display Formatting

The derive macro for this trait will automatically implement Display for you. The default process is to break the variant name into words, lowercase it, and then separate the words by spaces.

#[derive(Clone, Copy, Debug, ErrorKind)]
enum IoErrorKind {
    BadFileDescriptor, // Prints "bad file descriptor"
    FileNotFound,      // Prints "file not found"
    // ...
}

If you don’t like a particular display format, you can change it with the #[error(...)] attribute on a variant.

#[derive(Clone, Copy, Debug, ErrorKind)]
enum IoErrorKind {
    BadFileDescriptor, // Prints "bad file descriptor"
    #[error("custom display")]
    FileNotFound,      // Prints "custom display"
    // ...
}

§Examples

An error kind denotes a set of semantically related errors.

#[derive(Clone, Copy, Debug, ErrorKind)]
enum IoErrorKind {
    BadFileDescriptor,
    FileNotFound,
    // ...
}

You can define multiple kinds of errors by creating separate enums.

#[derive(Clone, Copy, Debug, ErrorKind)]
enum IoErrorKind {
    BadFileDescriptor,
    FileNotFound,
    // ...
}

#[derive(Clone, Copy, Debug, ErrorKind)]
enum ParseErrorKind {
    InvalidFormat,
    UnexpectedEndOfData,
    // ...
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§