expression

Transfer ownership into a spawned future

Move owned data into a future to avoid borrowing a short-lived caller variable. Static does not require the task to live forever. Moving a reference does not extend its referent lifetime. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): 7 Common mistake: Using async move to assume all captured borrowed references become static.

Formal Syntax & Structure

rust syntaxrust
tokio::spawn(async move { ... })

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    let name = String::from("library");    let task = tokio::spawn(async move { name.len() });    println!("{}", task.await.unwrap());}
Rust Reference