Is there a standard Empty Type?


#1

I was reading the nomicon and the paragraph regarding Empty Types caught my eye:
https://doc.rust-lang.org/beta/nomicon/exotic-sizes.html#empty-types

The gist of it is that you can use an Empty Type to statically guarantee that a returned Result is never an error.

Is there a standard Empty Type like Void in the nomicon example?

I have some infallible functions that are required to return Result where such a type would be helpful.


#2

I think you may want the soon-to-be-stable ! type?


#3

To add to that, the nightly documentation already has some explanations of ! (or “never” type).


#4

I see. I didn’t know you could use ! yourself. I thought it was a compiler internals thing :slight_smile:


#5

It was/is, but it’s being able to be used by regular Rust programs soon :slight_smile:


#6

In older nightlies, the example given would compile with #![feature(never_type)], but that was changed to #![feature(exhaustive_patterns)] as ! emerges from the night.

#![feature(exhaustive_patterns)]
fn main() {
    enum Void {}
    let res: Result<u32, Void> = Ok(0);
    // Err doesn't exist anymore, so Ok is actually irrefutable.
    let Ok(num) = res;
}

Relevant comment in the stabilization RFC as never is stabilized but exhaustive patterns are not: https://github.com/rust-lang/rust/issues/35121#issuecomment-360027186

So in stable rust you’ll need a match arm Err(never) => never, which I suppose is the idiomatic name for “never doesn’t actually have a value, and its type is ! which will coerce to any type”.