PanicInfo is the context passed to a Rust panic hook. It implements Debug and Display, and provides details such as the panic location and payload. section 21.2
The structure helps a custom hook inspect and report a panic:
- payload() returns the value associated with the panic; it is commonly a &'static str or String, but may be another type. section 21.2
- The payload is exposed through Any, so downcast_ref::<&str>() can recover a string when that is its concrete type. section 21.2
- A hook should avoid panicking: a panic during panic handling can abort the process instead of unwinding the stack. section 21.2
Reading a panic payload
A custom hook receives PanicInfo through its closure argument: `std::panic::set_hook(Box::new(|info| { if let Some(payload) = info.payload().downcast_ref::<&str>() { println!("{payload}"); } }));`. The conditional handles downcast failure without causing another panic. section 21.2
pub struct PanicInfo<'a> {
payload: &'a (dyn Any + Send),
message: Option<&'a fmt::Arguments<'a>>,
location: &'a Location<'a>,
can_unwind: bool,
}
Inspect the displayed structure definition, including payload, message, location, and can_unwind fields. section 21.2
