Never type

August 25, 2026 ยท View on GitHub

r[type.never]

Never type

r[type.never.intro] The never type ! is a type with no values, representing computations that never complete, also known as [diverging][divergence] computations.

[!EXAMPLE]

fn foo() -> ! {
    loop {}
}
unsafe extern "C" {
    pub safe fn no_return_extern_func() -> !;
}
let _: ! = loop {};
fn always_ok() -> Result<u32, !> {
    Ok(42)
}
# use std::str::FromStr;
struct Anything(String);

impl FromStr for Anything {
    type Err = !;

    fn from_str(s: &str) -> Result<Self, !> {
        Ok(Anything(s.to_owned()))
    }
}

// This does not need to check for the `Err` variant because
// `FromStr::Err` is the never type.
let Ok(s) = Anything::from_str("example");

r[type.never.syntax]

NeverType -> `!`

r[type.never.coercion] Expressions of type ! can be coerced into any type.

Note

The standard library type Infallible is a type alias for !.