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

macro_rules! panic(
    () => ({
        panic!("explicit panic")
    });
    ($msg:expr) => ({
        // static requires less code at runtime, more constant data
        static _FILE_LINE: (&'static str, uint) = (file!(), line!());
        ::std::rt::begin_unwind($msg, &_FILE_LINE)
    });
    ($fmt:expr, $($arg:tt)*) => ({
        // a closure can't have return type !, so we need a full
        // function to pass to format_args!, *and* we need the
        // file and line numbers right here; so an inner bare fn
        // is our only choice.
        //
        // LLVM doesn't tend to inline this, presumably because begin_unwind_fmt
        // is #[cold] and #[inline(never)] and because this is flagged as cold
        // as returning !. We really do want this to be inlined, however,
        // because it's just a tiny wrapper. Small wins (156K to 149K in size)
        // were seen when forcing this to be inlined, and that number just goes
        // up with the number of calls to panic!()
        //
        // The leading _'s are to avoid dead code warnings if this is
        // used inside a dead function. Just `#[allow(dead_code)]` is
        // insufficient, since the user may have
        // `#[forbid(dead_code)]` and which cannot be overridden.
        #[inline(always)]
        fn _run_fmt(fmt: &::std::fmt::Arguments) -> ! {
            static _FILE_LINE: (&'static str, uint) = (file!(), line!());
            ::std::rt::begin_unwind_fmt(fmt, &_FILE_LINE)
        }
        format_args!(_run_fmt, $fmt, $($arg)*)
    });
)

The entry point for panic of Rust tasks.

This macro is used to inject panic into a Rust task, causing the task to unwind and panic entirely. Each task's panic can be reaped as the Box<Any> type, and the single-argument form of the panic! macro will be the value which is transmitted.

The multi-argument form of this macro panics with a string and has the format! syntax for building a string.

Example

fn main() { #![allow(unreachable_code)] panic!(); panic!("this is a terrible mistake!"); panic!(4i); // panic with the value of 4 to be collected elsewhere panic!("this is a {} {message}", "fancy", message = "message"); }
panic!();
panic!("this is a terrible mistake!");
panic!(4i); // panic with the value of 4 to be collected elsewhere
panic!("this is a {} {message}", "fancy", message = "message");