Skip to content

Commit fd1b938

Browse files
authored
Add a simple example for future::select() (#2182)
1 parent 263dbc6 commit fd1b938

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

futures-util/src/future/select.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,34 @@ impl<A: Unpin, B: Unpin> Unpin for Select<A, B> {}
2727
///
2828
/// # Examples
2929
///
30+
/// A simple example
31+
///
32+
/// ```
33+
/// # futures::executor::block_on(async {
34+
/// use futures::future::{self, Either};
35+
/// use futures::pin_mut;
36+
///
37+
/// // These two futures have different types even though their outputs have the same type
38+
/// let future1 = async { 1 };
39+
/// let future2 = async { 2 };
40+
///
41+
/// // 'select' requires Future + Unpin bounds
42+
/// pin_mut!(future1);
43+
/// pin_mut!(future2);
44+
///
45+
/// let value = match future::select(future1, future2).await {
46+
/// Either::Left((value1, _)) => value1, // `value1` is resolved from `future1`
47+
/// // `_` represents `future2`
48+
/// Either::Right((value2, _)) => value2, // `value2` is resolved from `future2`
49+
/// // `_` represents `future1`
50+
/// };
51+
///
52+
/// assert!(value == 1 || value == 2);
53+
/// # });
54+
/// ```
55+
///
56+
/// A more complex example
57+
///
3058
/// ```
3159
/// use futures::future::{self, Either, Future, FutureExt};
3260
///

0 commit comments

Comments
 (0)