Created
December 27, 2023 01:02
-
-
Save ming900518/80304204b2f9eb7c927a2ae863a2390e to your computer and use it in GitHub Desktop.
Rust Async Closure Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(async_closure)] | |
use futures_util::future::join_all; | |
use std::{iter::zip, time::Duration}; | |
use tokio::time::sleep; | |
#[tokio::main] | |
async fn main() { | |
let ids = [1, 2, 3, 4]; | |
let durations = [200, 100, 300, 250]; | |
join_all( | |
zip(ids, durations) | |
.map(async move |(id, duration)| { | |
let async_fn = hello(id, Duration::from_millis(duration)).await; | |
async_fn | |
}) | |
.collect::<Vec<_>>(), | |
) | |
.await; | |
} | |
async fn hello(id: usize, duration: Duration) { | |
sleep(duration).await; | |
println!("Hello, {id}"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result