-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsArea: Documentation for any part of the project, including the compiler, standard library, and toolsT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.
Description
Location
https://doc.rust-lang.org/beta/core/panic/struct.PanicInfo.html#method.location
rust/library/core/src/panic/panic_info.rs
Lines 65 to 95 in 9bad7ba
/// Returns information about the location from which the panic originated, | |
/// if available. | |
/// | |
/// This method will currently always return [`Some`], but this may change | |
/// in future versions. | |
/// | |
/// # Examples | |
/// | |
/// ```should_panic | |
/// use std::panic; | |
/// | |
/// panic::set_hook(Box::new(|panic_info| { | |
/// if let Some(location) = panic_info.location() { | |
/// println!("panic occurred in file '{}' at line {}", | |
/// location.file(), | |
/// location.line(), | |
/// ); | |
/// } else { | |
/// println!("panic occurred but can't get location information..."); | |
/// } | |
/// })); | |
/// | |
/// panic!("Normal panic"); | |
/// ``` | |
#[must_use] | |
#[stable(feature = "panic_hooks", since = "1.10.0")] | |
pub fn location(&self) -> Option<&Location<'_>> { | |
// NOTE: If this is changed to sometimes return None, | |
// deal with that case in std::panicking::default_hook and core::panicking::panic_fmt. | |
Some(&self.location) | |
} |
Summary
since #115974 the PanicInfo
and PanicHookInfo
structs have been split up, but the docs for PanicInfo
still show how to use it with a panic hook, even though it is now exclusively for the #[panic_handler]
and the panic hook functionality has been moved into PanicHookInfo
Metadata
Metadata
Assignees
Labels
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsArea: Documentation for any part of the project, including the compiler, standard library, and toolsT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.