Skip to content

Commit 3ea744e

Browse files
committed
Recommend panic::resume_unwind instead of panicking.
Fixes #79950.
1 parent e4297ba commit 3ea744e

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

library/std/src/thread/mod.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -1186,32 +1186,37 @@ impl fmt::Debug for Thread {
11861186
/// the [`Error`](crate::error::Error) trait.
11871187
///
11881188
/// Thus, a sensible way to handle a thread panic is to either:
1189-
/// 1. `unwrap` the `Result<T>`, propagating the panic
1189+
///
1190+
/// 1. propagate the panic with [`std::panic::resume_unwind`]
11901191
/// 2. or in case the thread is intended to be a subsystem boundary
11911192
/// that is supposed to isolate system-level failures,
1192-
/// match on the `Err` variant and handle the panic in an appropriate way.
1193+
/// match on the `Err` variant and handle the panic in an appropriate way
11931194
///
11941195
/// A thread that completes without panicking is considered to exit successfully.
11951196
///
11961197
/// # Examples
11971198
///
1199+
/// Matching on the result of a joined thread:
1200+
///
11981201
/// ```no_run
1199-
/// use std::thread;
1200-
/// use std::fs;
1202+
/// use std::{fs, thread, panic};
12011203
///
12021204
/// fn copy_in_thread() -> thread::Result<()> {
1203-
/// thread::spawn(move || { fs::copy("foo.txt", "bar.txt").unwrap(); }).join()
1205+
/// thread::spawn(|| {
1206+
/// fs::copy("foo.txt", "bar.txt").unwrap();
1207+
/// }).join()
12041208
/// }
12051209
///
12061210
/// fn main() {
12071211
/// match copy_in_thread() {
1208-
/// Ok(_) => println!("this is fine"),
1209-
/// Err(_) => println!("thread panicked"),
1212+
/// Ok(_) => println!("copy succeeded"),
1213+
/// Err(e) => panic::resume_unwind(e),
12101214
/// }
12111215
/// }
12121216
/// ```
12131217
///
12141218
/// [`Result`]: crate::result::Result
1219+
/// [`std::panic::resume_unwind`]: crate::panic::resume_unwind
12151220
#[stable(feature = "rust1", since = "1.0.0")]
12161221
pub type Result<T> = crate::result::Result<T, Box<dyn Any + Send + 'static>>;
12171222

0 commit comments

Comments
 (0)