set_hook installs a global panic hook that replaces Rust’s default panic output. It accepts a closure, so the program can choose what to print when a panic occurs. section 21.2
A small custom hook
std::panic::set_hook(Box::new(|info| { println!("Well, that didn't work: {info}"); })); panic!(); The closure receives PanicInfo, which can provide the panic message and location. Its payload can also be inspected and downcast when the panic carries a compatible value. section 21.2
fn main() {
std::panic::set_hook(Box::new(|_| {
println!("Oops, that didn't work.");
println!("앗 뭔가 잘못 됐네요."); #1
}));
panic!();
}
View a runnable example that replaces the default panic report with custom messages. section 21.2
