File tree 1 file changed +22
-11
lines changed
1 file changed +22
-11
lines changed Original file line number Diff line number Diff line change @@ -715,21 +715,32 @@ struct Inner {
715
715
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
716
716
/// A handle to a thread.
717
717
///
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
+ ///
718
723
/// # Examples
719
724
///
720
725
/// ```
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
+ /// }
732
740
/// ```
741
+ /// [`Builder`]: ../../std/thread/struct.Builder.html
742
+ /// [`spawn`]: ../../std/thread/fn.spawn.html
743
+
733
744
pub struct Thread {
734
745
inner : Arc < Inner > ,
735
746
}
You can’t perform that action at this time.
0 commit comments