[][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:

This example deliberately fails to compile
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:

This example deliberately fails to compile
fn forward(never: !) -> ! {
    never
}

The same goes for expressing the function type:

This example deliberately fails to compile
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 impls on !

Currently, one can't impl custom traits directly on !:

This example deliberately fails to compile
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:

This example deliberately fails to compile
let array: [!; 0] = [];

The same for slices:

This example deliberately fails to compile
let slice: &[!] = &[];

By using Never in place of ! the above works:

let array: [bad::Never; 0] = [];
let slice: &[bad::Never] = &[];