Skip to content

Commit

Permalink
Rollup merge of rust-lang#110110 - lukas-code:display-panic-info, r=J…
Browse files Browse the repository at this point in the history
…ohnTitor

Use `Display` in top-level example for `PanicInfo`

Addresses rust-lang#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.
  • Loading branch information
matthiaskrgr authored Apr 14, 2023
2 parents 4b8351f + d9f99c3 commit 5107c4c
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions library/core/src/panic/panic_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@ use crate::panic::Location;
/// use std::panic;
///
/// panic::set_hook(Box::new(|panic_info| {
/// if let Some(s) = panic_info.payload().downcast_ref::<&str>() {
/// println!("panic occurred: {s:?}");
/// } else {
/// println!("panic occurred");
/// }
/// println!("panic occurred: {panic_info}");
/// }));
///
/// panic!("Normal panic");
/// panic!("critical system failure");
/// ```
#[lang = "panic_info"]
#[stable(feature = "panic_hooks", since = "1.10.0")]
Expand Down

0 comments on commit 5107c4c

Please sign in to comment.