Rust 1.85: async closures stabilized — goodbye Pin<Box<dyn Future>>

Posted by Radha Rani · 2025-03-07

Async closures are finally stable. The Pin<Box<dyn Future>> gymnastics are over. Clean, ergonomic async code that looks like synchronous code. https://github.com/rust-lang/rust/releases/tag/1.85.0

// Rust 1.85 — stabilized async closures
let fetch_user = async |id: u64| -> Result<User, Error> {
    let response = reqwest::get(format!("/api/users/{id}")).await?;
    response.json::<User>().await
};

async fn retry<F, T, E>(f: F, attempts: u32) -> Result<T, E>
where
    F: async Fn() -> Result<T, E>,
{
    for _ in 0..attempts {
        if let Ok(result) = f().await {
            return Ok(result);
        }
    }
    f().await
}

Explore GitVibes