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 and  macros, which indicate that this part of the code hasn't been written yet.
 * The macro which indicates that this branch of the code should never happen.
 * The, or  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. ). As this is unwanted behaviour but doesn't cause UB, it only is checked in debug mode.

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.

When you panic, these things happen:
 * 1) The panic hook is run - by default this will print the panic message to stderr.
 * 2) Scopes are exited one by one, running the destructors for all items in scope, until the top-level scope is reached or a scope catches the panic.

The module provides support for registering custom panic hooks and catching panics. The function runs a given closure, returning an error if the closure panics, and the  and  functions allow you to set and get the global panic hook respectively.