std::unreachable! [-] [+] [src]

(  ) => ( { panic ! ( "internal error: entered unreachable code" ) } ) ; (
$ msg : expr ) => ( { unreachable ! ( "{}" , $ msg ) } ) ; (
$ fmt : expr , $ ( $ arg : tt ) * ) => (
{
panic ! (
concat ! ( "internal error: entered unreachable code: " , $ fmt ) , $ ( $ arg
) * ) } ) ;

A utility macro for indicating unreachable code.

This is useful any time that the compiler can't determine that some code is unreachable. For example:

Panics

This will always panic.

Examples

Match arms:

fn main() { fn foo(x: Option<int>) { match x { Some(n) if n >= 0 => println!("Some(Non-negative)"), Some(n) if n < 0 => println!("Some(Negative)"), Some(_) => unreachable!(), // compile error if commented out None => println!("None") } } }
fn foo(x: Option<int>) {
    match x {
        Some(n) if n >= 0 => println!("Some(Non-negative)"),
        Some(n) if n <  0 => println!("Some(Negative)"),
        Some(_)           => unreachable!(), // compile error if commented out
        None              => println!("None")
    }
}

Iterators:

fn main() { fn divide_by_three(x: u32) -> u32 { // one of the poorest implementations of x/3 for i in std::iter::count(0_u32, 1) { if 3*i < i { panic!("u32 overflow"); } if x < 3*i { return i-1; } } unreachable!(); } }
fn divide_by_three(x: u32) -> u32 { // one of the poorest implementations of x/3
    for i in std::iter::count(0_u32, 1) {
        if 3*i < i { panic!("u32 overflow"); }
        if x < 3*i { return i-1; }
    }
    unreachable!();
}