Async closure lifetime issue

⚓ Rust    📅 2025-09-01    👤 surdeus    👁️ 2      

surdeus

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

Read full topic

🏷️ Rust_feed