Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Never type

The never type ! is a type with no values, representing computations that never complete, also known as diverging computations.

Example

#![allow(unused)]
fn main() {
fn foo() -> ! {
    loop {}
}
}
#![allow(unused)]
fn main() {
unsafe extern "C" {
    pub safe fn no_return_extern_func() -> !;
}
}
#![allow(unused)]
fn main() {
let _: ! = loop {};
}
#![allow(unused)]
fn main() {
fn always_ok() -> Result<u32, !> {
    Ok(42)
}
}
#![allow(unused)]
fn main() {
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");
}

Syntax
NeverType!

Expressions of type ! can be coerced into any type.

Note

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