panic!
in Rust is akin to exceptions in other languages, with two major differences:
- you cannot specify a "type"
- you cannot specify "data"
This makes it awkward to use it for out-of-bands signalling, and therefore reserves its use to "Oops" situations.
Otherwise, it behaves much in the same way in general: the current thread of execution unwinds (calling destructors as appropriate) until one of 3 things happen:
- a destructor
panic!
: the process aborts immediately
- the bottom of the stack is reached: the process aborts immediately
- a
catch_unwind
is reached: the handler decides what to do
Along the way, the various destructors might poison some multi-thread data structures, which may cause other threads to panic when they attempt to use them, etc... that is however a library decision and is not enforced by the language itself.
Note: as noted by others, there is now another behavior for panics, immediate abortion. It shaves off some code, and obviously prevents graceful recovery.