async fn — asynchronous function returning a Future.
Basic:
1async fn fetch_data() -> String {2 // Async operation3 tokio::time::sleep(std::time::Duration::from_secs(1)).await;4 String::from("data")5}67#[tokio::main]8async fn main() {9 let data = fetch_data().await;10 println!("{}", data);11}
Async blocks:
1let future = async {2 423};45let result = future.await;
Concurrent tasks:
1let (a, b) = tokio::join!(fetch_a(), fetch_b());
Key: .await to wait, #[tokio::main] to run.