9951 explained code solutions for 126 technologies


rustHow to loop async in Rust


Looping asynchronously in Rust is possible using the async/await syntax. The following example code shows how to loop asynchronously over a vector of strings:

async fn loop_async() {
    let strings = vec!["foo", "bar", "baz"];
    for s in strings {
        println!("{}", s);
    }
}

Output example

foo
bar
baz

Code explanation

  • async fn loop_async(): declares an asynchronous function
  • let strings = vec!["foo", "bar", "baz"];: creates a vector of strings
  • for s in strings: loops over the vector of strings
  • println!("{}", s);: prints the current string

Helpful links

Edit this code on GitHub