// Cargo.toml:
// tokio = { version = "1", features = ["full"] }
use tokio::time::{sleep, Duration};
async fn delayed_greeting(name: &str, delay_ms: u64) -> String {
sleep(Duration::from_millis(delay_ms)).await;
format!("Hello, {name}!")
}
#[tokio::main]
async fn main() {
// Sequential — total time ≈ 100 + 50 = 150ms
let a = delayed_greeting("Alice", 100).await;
let b = delayed_greeting("Bob", 50).await;
println!("{a} {b}");
// Concurrent — total time ≈ max(100, 50) = 100ms
let (a, b) = tokio::join!(
delayed_greeting("Alice", 100),
delayed_greeting("Bob", 50),
);
println!("{a} {b}");
}
Create a free account and build your private vault. Share publicly whenever you want.