Panic

Panics in Rust are a mechanism to allow the program to immediately abort with an error. Panics are an indication of a logic bug in your program, and should never happen otherwise.

Panics are typically triggered by an invocation of the [https://doc.rust-lang.org/stable/core/macro.panic.html panic! macro]. It will cause the current thread to exit and it will print out an error message to stderr. Panics return, the Never type, as they never return.

Panics can also be caused by:
 * The todo! and unimplemented! macros, which indicate that this part of the code hasn't been written yet.
 * The unreachable! macro which indicates that this branch of the code should never happen.
 * The assert!, assert_eq!, or assert_ne! macros failing. These are primarily used in tests.
 * The  or   methods of  or  types. Expect is identical to unwrap except it panics with a specified message, which is usually preferred.
 * When indexing is out of range (e.g. ). This panics in release mode too, as it has the risk of memory unsafety.

And in debug mode:
 * The  sisters of the   macros, which assert but only in debug mode. This is useful if a condition should be true, but you don't want to impact runtime performance. If memory safety is on the line, never use these macros.
 * When arithmetic overflows (e.g. ).

Panic Hooks and Handlers
Panicking is implemented using stack unwinding by default. This means that it is possible to catch panics and define custom behaviour on a panic. Do not use this for general-purpose error handling - for that, see.