Add examples for std:🧵:Thread::name.
This commit is contained in:
@@ -478,6 +478,37 @@ impl Thread {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the thread's name.
|
/// 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")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub fn name(&self) -> Option<&str> {
|
pub fn name(&self) -> Option<&str> {
|
||||||
self.cname().map(|s| unsafe { str::from_utf8_unchecked(s.to_bytes()) } )
|
self.cname().map(|s| unsafe { str::from_utf8_unchecked(s.to_bytes()) } )
|
||||||
|
|||||||
Reference in New Issue
Block a user