What returns a match on an empty enum?

When reading Rust convert.rs, I found 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 listing with no options. What returns match *self {} ?

+5
source share
1 answer

Since Infallible has no possible meanings, you will never have an instance of it. This means that a match on it will never happen. Rust represents this by creating a match on an empty enumeration with a type ! , which is a built-in type that does not matter.

This type forces any other type because the statement will never be reached, because for this you will need a value of type Infallible , which you cannot have for obvious reasons.

+9
source

Source: https://habr.com/ru/post/1273784/


All Articles