Skip to content

Commit d5a2759

Browse files
committed
Add examples for std::thread::Thread::name.
1 parent f4d03da commit d5a2759

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
@@ -478,6 +478,37 @@ impl Thread {
478478
}
479479

480480
/// Gets the thread's name.
481+
///
482+
/// # Examples
483+
///
484+
/// Threads by default have no name specified:
485+
///
486+
/// ```
487+
/// use std::thread;
488+
///
489+
/// let builder = thread::Builder::new();
490+
///
491+
/// let handler = builder.spawn(|| {
492+
/// assert!(thread::current().name().is_none());
493+
/// }).unwrap();
494+
///
495+
/// handler.join().unwrap();
496+
/// ```
497+
///
498+
/// Thread with a specified name:
499+
///
500+
/// ```
501+
/// use std::thread;
502+
///
503+
/// let builder = thread::Builder::new()
504+
/// .name("foo".into());
505+
///
506+
/// let handler = builder.spawn(|| {
507+
/// assert_eq!(thread::current().name(), Some("foo"))
508+
/// }).unwrap();
509+
///
510+
/// handler.join().unwrap();
511+
/// ```
481512
#[stable(feature = "rust1", since = "1.0.0")]
482513
pub fn name(&self) -> Option<&str> {
483514
self.cname().map(|s| unsafe { str::from_utf8_unchecked(s.to_bytes()) } )

0 commit comments

Comments
 (0)