6

When reading Rust's convert.rs, I encountered the following code:

#[unstable(feature = "try_from", issue = "33417")]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Infallible {}

#[unstable(feature = "try_from", issue = "33417")]
impl fmt::Display for Infallible {
    fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
        match *self {
        }
    }
}

Infallible is an empty enum with no variants. What does match *self {} return?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Cheng-Chang Wu
  • 187
  • 1
  • 7

1 Answers1

11

Since Infallible has no possible values, you can never have an instance of it. This means matching on it can never happen. Rust represents this by making matching on an empty enum yield the ! type, which is a builtin type that has no values.

This type coerces to any other type, because the statement can never be reached, because you'd need a value of type Infallible for that, which you can't have out of obvious reasons.

oli_obk
  • 28,729
  • 6
  • 82
  • 98