@@ -16,44 +16,7 @@ the resulting variables. The `statement` result becomes the result of the
16
16
` select! ` macro.
17
17
18
18
``` rust,editable,compile_fail
19
- use tokio::sync::mpsc::{self, Receiver};
20
- use tokio::time::{sleep, Duration};
21
-
22
- #[derive(Debug, PartialEq)]
23
- enum Animal {
24
- Cat { name: String },
25
- Dog { name: String },
26
- }
27
-
28
- async fn first_animal_to_finish_race(
29
- mut cat_rcv: Receiver<String>,
30
- mut dog_rcv: Receiver<String>,
31
- ) -> Option<Animal> {
32
- tokio::select! {
33
- cat_name = cat_rcv.recv() => Some(Animal::Cat { name: cat_name? }),
34
- dog_name = dog_rcv.recv() => Some(Animal::Dog { name: dog_name? })
35
- }
36
- }
37
-
38
- #[tokio::main]
39
- async fn main() {
40
- let (cat_sender, cat_receiver) = mpsc::channel(32);
41
- let (dog_sender, dog_receiver) = mpsc::channel(32);
42
- tokio::spawn(async move {
43
- sleep(Duration::from_millis(500)).await;
44
- cat_sender.send(String::from("Felix")).await.expect("Failed to send cat.");
45
- });
46
- tokio::spawn(async move {
47
- sleep(Duration::from_millis(50)).await;
48
- dog_sender.send(String::from("Rex")).await.expect("Failed to send dog.");
49
- });
50
-
51
- let winner = first_animal_to_finish_race(cat_receiver, dog_receiver)
52
- .await
53
- .expect("Failed to receive winner");
54
-
55
- println!("Winner is {winner:?}");
56
- }
19
+ {{#include select.rs}}
57
20
```
58
21
59
22
<details >
0 commit comments