Skip to content

Commit 5107c4c

Browse files
authored
Rollup merge of #110110 - lukas-code:display-panic-info, r=JohnTitor
Use `Display` in top-level example for `PanicInfo` Addresses #110098. This confused me as well, when I was writing a `no_std` panic handler for the first time, so here's a better top-level example. `Display` is stable, prints the `.message()` if available, and falls back to `.payload().downcast_ref<&str>()` if the message is not available. So this example should provide strictly more information and also work for formatted panics. The old example still exists on the `payload` method.
2 parents 4b8351f + d9f99c3 commit 5107c4c

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

library/core/src/panic/panic_info.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,10 @@ use crate::panic::Location;
1515
/// use std::panic;
1616
///
1717
/// panic::set_hook(Box::new(|panic_info| {
18-
/// if let Some(s) = panic_info.payload().downcast_ref::<&str>() {
19-
/// println!("panic occurred: {s:?}");
20-
/// } else {
21-
/// println!("panic occurred");
22-
/// }
18+
/// println!("panic occurred: {panic_info}");
2319
/// }));
2420
///
25-
/// panic!("Normal panic");
21+
/// panic!("critical system failure");
2622
/// ```
2723
#[lang = "panic_info"]
2824
#[stable(feature = "panic_hooks", since = "1.10.0")]

0 commit comments

Comments
 (0)