Puzzle 19 | Sleepless in Tokio |
| [package] |
| name = "sleepless" |
| version = "0.1.0" |
| edition = "2018" |
| |
| [dependencies] |
| tokio = { version = "1.7", features = ["full"] } |
| use tokio::join; |
| use std::time::Duration; |
| |
| async fn count_and_wait(n: u64) -> u64 { |
| println!("Starting {}", n); |
| std::thread::sleep(Duration::from_millis(n * 100)); |
| println!("Returning {}", n); |
| n |
| } |
| |
| #[tokio::main] |
| async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| // Join runs multiple tasks concurrently and returns when they all |
| // complete execution. |
| join!(count_and_wait(1), count_and_wait(2), count_and_wait(3)); |
| Ok(()) |
| } |
Guess the ... |
---|
Get Rust Brain Teasers now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.