Skip to content

Commit 6acaa81

Browse files
tunglitunglitaiki-e
authored
small change in select example. (#2345)
In the previous example one future always finished first (because of implementation details), possibly confusing users about what `select` does. This commit resolves the issue by being explicit about one future finishing before the other. Co-authored-by: tungli <tun@mail.muni.cz> Co-authored-by: Taiki Endo <te316e89@gmail.com>
1 parent 59f62b0 commit 6acaa81

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

futures-util/src/future/select.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,33 @@ impl<A: Unpin, B: Unpin> Unpin for Select<A, B> {}
3232
///
3333
/// ```
3434
/// # futures::executor::block_on(async {
35-
/// use futures::future::{self, Either};
36-
/// use futures::pin_mut;
37-
///
38-
/// // These two futures have different types even though their outputs have the same type
39-
/// let future1 = async { 1 };
40-
/// let future2 = async { 2 };
35+
/// use futures::{
36+
/// pin_mut,
37+
/// future::Either,
38+
/// future::self,
39+
/// };
40+
///
41+
/// // These two futures have different types even though their outputs have the same type.
42+
/// let future1 = async {
43+
/// future::pending::<()>().await; // will never finish
44+
/// 1
45+
/// };
46+
/// let future2 = async {
47+
/// future::ready(2).await
48+
/// };
4149
///
4250
/// // 'select' requires Future + Unpin bounds
4351
/// pin_mut!(future1);
4452
/// pin_mut!(future2);
4553
///
4654
/// let value = match future::select(future1, future2).await {
47-
/// Either::Left((value1, _)) => value1, // `value1` is resolved from `future1`
48-
/// // `_` represents `future2`
55+
/// Either::Left((value1, _)) => value1, // `value1` is resolved from `future1`
56+
/// // `_` represents `future2`
4957
/// Either::Right((value2, _)) => value2, // `value2` is resolved from `future2`
5058
/// // `_` represents `future1`
5159
/// };
5260
///
53-
/// assert!(value == 1 || value == 2);
61+
/// assert!(value == 2);
5462
/// # });
5563
/// ```
5664
///

0 commit comments

Comments
 (0)