I was reading the nomicon and the paragraph regarding Empty Types caught my eye:
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.
I think you may want the soon-to-be-stable ! type?
1 Like
To add to that, the nightly documentation already has some explanations of !
(or "never" type).
2 Likes
I see. I didn't know you could use !
yourself. I thought it was a compiler internals thing
It was/is, but it's being able to be used by regular Rust programs soon
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".