Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Async closure lifetime issue
In the following code, I do not understand why the async move has a lifetime issue?
use std::time::Duration;
use tokio::time::sleep;
use std::sync::Arc;
async fn closure() {
let num = Arc::new(42u64);
let task =
async move || sleep(Duration::from_millis(*num)).await;
tokio::spawn(task()).await.unwrap();
}
async fn block() {
let num = Arc::new(42u64);
let task =
async move { sleep(Duration::from_millis(*num)).await };
tokio::spawn(task).await.unwrap();
}
#[tokio::main]
async fn main() {
block().await;
closure().await;
}
5 posts - 3 participants
🏷️ Rust_feed