Skip to content

Commit 9952982

Browse files
Add missing examples in some thread functions
1 parent 1b38776 commit 9952982

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

src/libstd/thread/mod.rs

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -318,20 +318,38 @@ impl Builder {
318318
// Free functions
319319
////////////////////////////////////////////////////////////////////////////////
320320

321-
/// Spawns a new thread, returning a `JoinHandle` for it.
321+
/// Spawns a new thread, returning a [`JoinHandle`] for it.
322322
///
323323
/// The join handle will implicitly *detach* the child thread upon being
324324
/// dropped. In this case, the child thread may outlive the parent (unless
325325
/// the parent thread is the main thread; the whole process is terminated when
326-
/// the main thread finishes.) Additionally, the join handle provides a `join`
326+
/// the main thread finishes). Additionally, the join handle provides a [`join`]
327327
/// method that can be used to join the child thread. If the child thread
328-
/// panics, `join` will return an `Err` containing the argument given to
329-
/// `panic`.
328+
/// panics, [`join`] will return an [`Err`] containing the argument given to
329+
/// [`panic`].
330330
///
331331
/// # Panics
332332
///
333-
/// Panics if the OS fails to create a thread; use `Builder::spawn`
333+
/// Panics if the OS fails to create a thread; use [`Builder::spawn`]
334334
/// to recover from such errors.
335+
///
336+
/// [`JoinHandle`]: ../../std/thread/struct.JoinHandle.html
337+
/// [`join`]: ../../std/thread/struct.JoinHandle.html#method.join
338+
/// [`Err`]: ../../std/result/enum.Result.html#variant.Err
339+
/// [`panic!`]: ../../std/macro.panic.html
340+
/// [`Builder::spawn`]: ../../std/thread/struct.Builder.html#method.spawn
341+
///
342+
/// # Examples
343+
///
344+
/// ```
345+
/// use std::thread;
346+
///
347+
/// let handler = thread::spawn(|| {
348+
/// // thread code
349+
/// });
350+
///
351+
/// handler.join().unwrap();
352+
/// ```
335353
#[stable(feature = "rust1", since = "1.0.0")]
336354
pub fn spawn<F, T>(f: F) -> JoinHandle<T> where
337355
F: FnOnce() -> T, F: Send + 'static, T: Send + 'static
@@ -341,7 +359,7 @@ pub fn spawn<F, T>(f: F) -> JoinHandle<T> where
341359

342360
/// Gets a handle to the thread that invokes it.
343361
///
344-
/// #Examples
362+
/// # Examples
345363
///
346364
/// Getting a handle to the current thread with `thread::current()`:
347365
///
@@ -366,6 +384,14 @@ pub fn current() -> Thread {
366384
}
367385

368386
/// Cooperatively gives up a timeslice to the OS scheduler.
387+
///
388+
/// # Examples
389+
///
390+
/// ```
391+
/// use std::thread;
392+
///
393+
/// thread::yield_now();
394+
/// ```
369395
#[stable(feature = "rust1", since = "1.0.0")]
370396
pub fn yield_now() {
371397
imp::Thread::yield_now()
@@ -375,7 +401,7 @@ pub fn yield_now() {
375401
///
376402
/// # Examples
377403
///
378-
/// ```rust,should_panic
404+
/// ```should_panic
379405
/// use std::thread;
380406
///
381407
/// struct SomeStruct;
@@ -413,6 +439,15 @@ pub fn panicking() -> bool {
413439
/// specifics or platform-dependent functionality. Note that on unix platforms
414440
/// this function will not return early due to a signal being received or a
415441
/// spurious wakeup.
442+
///
443+
/// # Examples
444+
///
445+
/// ```no_run
446+
/// use std::thread;
447+
///
448+
/// // Let's sleep for 2 seconds:
449+
/// thread::sleep_ms(2000);
450+
/// ```
416451
#[stable(feature = "rust1", since = "1.0.0")]
417452
#[rustc_deprecated(since = "1.6.0", reason = "replaced by `std::thread::sleep`")]
418453
pub fn sleep_ms(ms: u32) {
@@ -433,7 +468,7 @@ pub fn sleep_ms(ms: u32) {
433468
///
434469
/// # Examples
435470
///
436-
/// ```rust,no_run
471+
/// ```no_run
437472
/// use std::{thread, time};
438473
///
439474
/// let ten_millis = time::Duration::from_millis(10);

0 commit comments

Comments
 (0)