Rollup merge of #110110 - lukas-code:display-panic-info, r=JohnTitor
Use `Display` in top-level example for `PanicInfo` Addresses https://github.com/rust-lang/rust/issues/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.
This commit is contained in:
commit
5107c4c556
1 changed files with 2 additions and 6 deletions
|
@ -15,14 +15,10 @@ use crate::panic::Location;
|
||||||
/// use std::panic;
|
/// use std::panic;
|
||||||
///
|
///
|
||||||
/// panic::set_hook(Box::new(|panic_info| {
|
/// panic::set_hook(Box::new(|panic_info| {
|
||||||
/// if let Some(s) = panic_info.payload().downcast_ref::<&str>() {
|
/// println!("panic occurred: {panic_info}");
|
||||||
/// println!("panic occurred: {s:?}");
|
|
||||||
/// } else {
|
|
||||||
/// println!("panic occurred");
|
|
||||||
/// }
|
|
||||||
/// }));
|
/// }));
|
||||||
///
|
///
|
||||||
/// panic!("Normal panic");
|
/// panic!("critical system failure");
|
||||||
/// ```
|
/// ```
|
||||||
#[lang = "panic_info"]
|
#[lang = "panic_info"]
|
||||||
#[stable(feature = "panic_hooks", since = "1.10.0")]
|
#[stable(feature = "panic_hooks", since = "1.10.0")]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue