@@ -315,6 +315,8 @@ impl Builder {
315
315
/// thread finishes). The join handle can be used to block on
316
316
/// termination of the child thread, including recovering its panics.
317
317
///
318
+ /// For a more complete documentation see [`thread::spawn`][`spawn`].
319
+ ///
318
320
/// # Errors
319
321
///
320
322
/// Unlike the [`spawn`] free function, this method yields an
@@ -392,14 +394,10 @@ impl Builder {
392
394
/// Panics if the OS fails to create a thread; use [`Builder::spawn`]
393
395
/// to recover from such errors.
394
396
///
395
- /// [`JoinHandle`]: ../../std/thread/struct.JoinHandle.html
396
- /// [`join`]: ../../std/thread/struct.JoinHandle.html#method.join
397
- /// [`Err`]: ../../std/result/enum.Result.html#variant.Err
398
- /// [`panic`]: ../../std/macro.panic.html
399
- /// [`Builder::spawn`]: ../../std/thread/struct.Builder.html#method.spawn
400
- ///
401
397
/// # Examples
402
398
///
399
+ /// Simple thread creation.
400
+ ///
403
401
/// ```
404
402
/// use std::thread;
405
403
///
@@ -409,6 +407,53 @@ impl Builder {
409
407
///
410
408
/// handler.join().unwrap();
411
409
/// ```
410
+ ///
411
+ /// As mentionned in the module documentation, threads are usualy made to
412
+ /// communicate using [`channel`s][`channels`], here is how it usually looks.
413
+ ///
414
+ /// This example also shows how to use `move`, in order to give ownership
415
+ /// of values to a thread.
416
+ ///
417
+ /// ```
418
+ /// use std::thread;
419
+ /// use std::sync::mpsc::channel;
420
+ ///
421
+ /// let (tx, rx) = channel();
422
+ ///
423
+ /// let sender = thread::spawn(move || {
424
+ /// tx.send("Hello, thread".to_owned());
425
+ /// });
426
+ ///
427
+ /// let receiver = thread::spawn(move || {
428
+ /// println!("{}", rx.recv().unwrap());
429
+ /// });
430
+ ///
431
+ /// sender.join();
432
+ /// receiver.join();
433
+ /// ```
434
+ ///
435
+ /// A thread can also return a value through its [`JoinHandle`], you can use
436
+ /// this to make asynchronous computations (futures might be more appropriate
437
+ /// though).
438
+ ///
439
+ /// ```
440
+ /// use std::thread;
441
+ ///
442
+ /// let computation = thread::spawn(|| {
443
+ /// // Some expensive computation.
444
+ /// 42
445
+ /// });
446
+ ///
447
+ /// let result = computation.join().unwrap();
448
+ /// println!("{}", v);
449
+ /// ```
450
+ ///
451
+ /// [`channels`]: ../../std/sync/mpsc/index.html
452
+ /// [`JoinHandle`]: ../../std/thread/struct.JoinHandle.html
453
+ /// [`join`]: ../../std/thread/struct.JoinHandle.html#method.join
454
+ /// [`Err`]: ../../std/result/enum.Result.html#variant.Err
455
+ /// [`panic`]: ../../std/macro.panic.html
456
+ /// [`Builder::spawn`]: ../../std/thread/struct.Builder.html#method.spawn
412
457
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
413
458
pub fn spawn < F , T > ( f : F ) -> JoinHandle < T > where
414
459
F : FnOnce ( ) -> T , F : Send + ' static , T : Send + ' static
0 commit comments