Rust Panics and Unwinds


When Rust programs reach a state where a critical error has occurred, the panic! macro can be called to exit quickly (often compared, but subtly different, to an exception in other languages). Proper error handling should involve Result types, though this section will only discuss panic! and its concepts.


Panics don't always cause memory leaks or other resource leaks. In fact, panics typically preserve RAII invariants, running the destructors (Drop implementations) of structs as the stack unwinds. However, if there is a second panic during this process, the program simply aborts; at that point, RAII invariant guarantees are void.