[−][src]Type Definition bad::Never
type Never = <fn() -> ! as HasOutput>::Output;
A type alias to !
(never) that works in places !
doesn't
currently in stable Rust.
Examples
This is a way to indirectly refer to !
in places where using it
directly isn't allowed. Such as simply making an alias to !
via normal
means:
type Never = !;
However, with this alias, you can make another alias to !
indirectly:
type Never = bad::Never;
Return type
Just like !
can already, Never
can be used as a function return type:
fn error() -> bad::Never { panic!(); } let error_fn: fn() -> ! = error;
Input type
Currently, one can't use !
as a function input type:
fn forward(never: !) -> ! { never }
The same goes for expressing the function type:
type F = fn(!) -> !;
By using Never
in place of !
, the function compiles:
fn forward(never: bad::Never) -> ! { never } let forward_fn: fn(bad::Never) -> ! = forward;
Trait impl
s on !
Currently, one can't impl
custom traits directly on !
:
trait NeverType {} impl NeverType for ! {}
By using Never
in place of !
, the impl
works:
impl NeverType for bad::Never {}
However, this isn't of much use since !
turns into ()
in the context
of trait bounds.
Array Item Type
Currently, one can't use !
as the item type of an array:
let array: [!; 0] = [];
The same for slices:
let slice: &[!] = &[];
By using Never
in place of !
the above works:
let array: [bad::Never; 0] = []; let slice: &[bad::Never] = &[];