Skip to main content

Infallible

Type Alias Infallible 

1.34.0 · Source
pub type Infallible = !;
Expand description

The error type for errors that can never happen.

Since this is an alias to the never type, a value of this type can never actually exist. This can be useful for generic APIs that use Result and parameterize the error type, to indicate that the result is always Ok.

For example, the TryFrom trait (conversion that returns a Result) has a blanket implementation for all types where a reverse Into implementation exists.

ⓘ
impl<T, U> TryFrom<U> for T where U: Into<T> {
    type Error = Infallible;

    fn try_from(value: U) -> Result<Self, Infallible> {
        Ok(U::into(value))  // Never returns `Err`
    }
}

Note: Since ! (the “never” type) was stabilized in Rust 1.100.0, it is preferred to use ! directly unless targeting older Rust versions. Infallible may be deprecated in a future Rust version.