Skip to content

Commit

Permalink
Add examples for std::thread::Thread::name.
Browse files Browse the repository at this point in the history
  • Loading branch information
frewsxcv committed Jun 19, 2016
1 parent f4d03da commit d5a2759
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/libstd/thread/mod.rs
Expand Up @@ -478,6 +478,37 @@ impl Thread {
}

/// Gets the thread's name.
///
/// # Examples
///
/// Threads by default have no name specified:
///
/// ```
/// use std::thread;
///
/// let builder = thread::Builder::new();
///
/// let handler = builder.spawn(|| {
/// assert!(thread::current().name().is_none());
/// }).unwrap();
///
/// handler.join().unwrap();
/// ```
///
/// Thread with a specified name:
///
/// ```
/// use std::thread;
///
/// let builder = thread::Builder::new()
/// .name("foo".into());
///
/// let handler = builder.spawn(|| {
/// assert_eq!(thread::current().name(), Some("foo"))
/// }).unwrap();
///
/// handler.join().unwrap();
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn name(&self) -> Option<&str> {
self.cname().map(|s| unsafe { str::from_utf8_unchecked(s.to_bytes()) } )
Expand Down

0 comments on commit d5a2759

Please sign in to comment.