Panic is a kind of error that should never happend
. Rust can either unwind the stack when a panic happens or abort the process. Unwinding is the default. Panic proceed as follows
There are two circumstances in which Rust does not try to unwind the stack.
.drop()
method triggers a second panic while Rust is still trying to clean up after the first:struct PanicOnDrop;
impl Drop for PanicOnDrop {
fn drop(&mut self) {
println!("Dropping!");
panic!("Panic in Drop!");
}
}
fn main() {
let _x = PanicOnDrop;
// This will cause a panic
panic!("Main panic!");
}
-C panic=abort
, the first panic in your program immediately aborts the process. (With this option, Rust does not need to know how to unwind the stack, so this can reduce the size of your compiled code.)