Skip to content

Commit 9f06dfb

Browse files
authored
Rollup merge of #41543 - z1mvader:master, r=steveklabnik
Rewrote the thread struct docs #29378
2 parents 8305394 + cf52121 commit 9f06dfb

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

src/libstd/thread/mod.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -715,21 +715,32 @@ struct Inner {
715715
#[stable(feature = "rust1", since = "1.0.0")]
716716
/// A handle to a thread.
717717
///
718+
/// You can use it to identify a thread (by name, for example). Most of the
719+
/// time, there is no need to directly create a `Thread` struct using the
720+
/// constructor, instead you should use a function like `spawn` to create
721+
/// new threads, see the docs of [`Builder`] and [`spawn`] for more.
722+
///
718723
/// # Examples
719724
///
720725
/// ```
721-
/// use std::thread;
722-
///
723-
/// let handler = thread::Builder::new()
724-
/// .name("foo".into())
725-
/// .spawn(|| {
726-
/// let thread = thread::current();
727-
/// println!("thread name: {}", thread.name().unwrap());
728-
/// })
729-
/// .unwrap();
730-
///
731-
/// handler.join().unwrap();
726+
/// use std::thread::Builder;
727+
///
728+
/// for i in 0..5 {
729+
/// let thread_name = format!("thread_{}", i);
730+
/// Builder::new()
731+
/// .name(thread_name) // Now you can identify which thread panicked
732+
/// // thanks to the handle's name
733+
/// .spawn(move || {
734+
/// if i == 3 {
735+
/// panic!("I'm scared!!!");
736+
/// }
737+
/// })
738+
/// .unwrap();
739+
/// }
732740
/// ```
741+
/// [`Builder`]: ../../std/thread/struct.Builder.html
742+
/// [`spawn`]: ../../std/thread/fn.spawn.html
743+
733744
pub struct Thread {
734745
inner: Arc<Inner>,
735746
}

0 commit comments

Comments
 (0)