liveBook · Manning

PanicInfo

As explained in Learn Rust in a Month of Lunches

PanicInfo is a Rust structure containing contextual information about a panic, including its location and payload.

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

How Learn Rust in a Month of Lunches covers it

How deep it goes
Explains formatting, payload inspection, downcasting, and panic-hook safety, while stopping short of broader panic-runtime internals.
Examples
Demonstrates custom messages, formatted PanicInfo output, string-payload extraction, and failure caused by panicking inside a hook.
What it assumes
Assumes basic Rust closures and panics, then teaches the unfamiliar Any payload and downcast_ref operation through examples.
What you can do after
Diagnose/debug with it by formatting panic context and conditionally extracting string payloads inside a custom hook.
How it connects
Connects PanicInfo with panic hooks, closures, Any, downcasting, payloads, and stack unwinding.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.