Unwinding
Option and Result are overwhelmingly preferred in most situations, especiallysince they can be promoted into a panic or abort at the API user’s discretion.Panics cause the thread to halt normal execution and unwind its stack, callingdestructors as if every function instantly returned.
Unwinding was important to this story because if a task’sdestructors weren’t called, it would cause memory and other system resources toleak. Since tasks were expected to die during normal execution, this would makeRust very poor for long-running systems!
There is an unstable API called that enables catching a panicwithout spawning a thread. Still, we would encourage you to only do thissparingly. In particular, Rust’s current unwinding implementation is heavilyoptimized for the “doesn’t unwind” case. If a program doesn’t unwind, thereshould be no runtime cost for the program being ready to unwind. As aconsequence, actually unwinding will be more expensive than in e.g. Java.Don’t build your programs to unwind under normal circumstances. Ideally, youshould only panic for programming errors or extreme problems.