From fd1b93873771da8fbc7185b8fc5c173207d1247e Mon Sep 17 00:00:00 2001 From: keepsimple1 Date: Mon, 21 Sep 2020 12:44:08 -0700 Subject: [PATCH] Add a simple example for future::select() (#2182) --- futures-util/src/future/select.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/futures-util/src/future/select.rs b/futures-util/src/future/select.rs index 3e0a447da6..bc247796dd 100644 --- a/futures-util/src/future/select.rs +++ b/futures-util/src/future/select.rs @@ -27,6 +27,34 @@ impl Unpin for Select {} /// /// # Examples /// +/// A simple example +/// +/// ``` +/// # futures::executor::block_on(async { +/// use futures::future::{self, Either}; +/// use futures::pin_mut; +/// +/// // These two futures have different types even though their outputs have the same type +/// let future1 = async { 1 }; +/// let future2 = async { 2 }; +/// +/// // 'select' requires Future + Unpin bounds +/// pin_mut!(future1); +/// pin_mut!(future2); +/// +/// let value = match future::select(future1, future2).await { +/// Either::Left((value1, _)) => value1, // `value1` is resolved from `future1` +/// // `_` represents `future2` +/// Either::Right((value2, _)) => value2, // `value2` is resolved from `future2` +/// // `_` represents `future1` +/// }; +/// +/// assert!(value == 1 || value == 2); +/// # }); +/// ``` +/// +/// A more complex example +/// /// ``` /// use futures::future::{self, Either, Future, FutureExt}; ///