Skip to content

Commit 592c314

Browse files
Rollup merge of #34371 - frewsxcv:thread-name, r=steveklabnik
Add examples for `std::thread::Thread::name`. None
2 parents 0b566f6 + d5a2759 commit 592c314

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/libstd/thread/mod.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,37 @@ impl Thread {
507507
}
508508

509509
/// Gets the thread's name.
510+
///
511+
/// # Examples
512+
///
513+
/// Threads by default have no name specified:
514+
///
515+
/// ```
516+
/// use std::thread;
517+
///
518+
/// let builder = thread::Builder::new();
519+
///
520+
/// let handler = builder.spawn(|| {
521+
/// assert!(thread::current().name().is_none());
522+
/// }).unwrap();
523+
///
524+
/// handler.join().unwrap();
525+
/// ```
526+
///
527+
/// Thread with a specified name:
528+
///
529+
/// ```
530+
/// use std::thread;
531+
///
532+
/// let builder = thread::Builder::new()
533+
/// .name("foo".into());
534+
///
535+
/// let handler = builder.spawn(|| {
536+
/// assert_eq!(thread::current().name(), Some("foo"))
537+
/// }).unwrap();
538+
///
539+
/// handler.join().unwrap();
540+
/// ```
510541
#[stable(feature = "rust1", since = "1.0.0")]
511542
pub fn name(&self) -> Option<&str> {
512543
self.cname().map(|s| unsafe { str::from_utf8_unchecked(s.to_bytes()) } )

0 commit comments

Comments
 (0)