Skip to content

Commit 3392775

Browse files
authored
Auto merge of rust-lang#36390 - frewsxcv:panic-set-hook, r=GuillaumeGomez
Add basic doc examples for `std::panic::{set_hook, take_hook}`. None
2 parents 22d15ea + 5505ebc commit 3392775

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Diff for: src/libstd/panicking.rs

+30
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ static mut HOOK: Hook = Hook::Default;
8484
/// # Panics
8585
///
8686
/// Panics if called from a panicking thread.
87+
///
88+
/// # Examples
89+
///
90+
/// The following will print "Custom panic hook":
91+
///
92+
/// ```should_panic
93+
/// use std::panic;
94+
///
95+
/// panic::set_hook(Box::new(|_| {
96+
/// println!("Custom panic hook");
97+
/// }));
98+
///
99+
/// panic!("Normal panic");
100+
/// ```
87101
#[stable(feature = "panic_hooks", since = "1.10.0")]
88102
pub fn set_hook(hook: Box<Fn(&PanicInfo) + 'static + Sync + Send>) {
89103
if thread::panicking() {
@@ -109,6 +123,22 @@ pub fn set_hook(hook: Box<Fn(&PanicInfo) + 'static + Sync + Send>) {
109123
/// # Panics
110124
///
111125
/// Panics if called from a panicking thread.
126+
///
127+
/// # Examples
128+
///
129+
/// The following will print "Normal panic":
130+
///
131+
/// ```should_panic
132+
/// use std::panic;
133+
///
134+
/// panic::set_hook(Box::new(|_| {
135+
/// println!("Custom panic hook");
136+
/// }));
137+
///
138+
/// let _ = panic::take_hook();
139+
///
140+
/// panic!("Normal panic");
141+
/// ```
112142
#[stable(feature = "panic_hooks", since = "1.10.0")]
113143
pub fn take_hook() -> Box<Fn(&PanicInfo) + 'static + Sync + Send> {
114144
if thread::panicking() {

0 commit comments

Comments
 (0)