擷取解開堆疊的動作
根據預設,恐慌會造成解開堆疊。您可以擷取這類動作:
use std::panic; fn main() { let result = panic::catch_unwind(|| { println!("hello!"); }); assert!(result.is_ok()); let result = panic::catch_unwind(|| { panic!("oh no!"); }); assert!(result.is_err()); }
- 如果伺服器需要持續運作 (即使有單一要求崩潰也不例外),這種做法就能派上用場。
- 如果您在
Cargo.toml
中設定panic = 'abort'
,就無法採取此做法。