expression

Spawn an independent async task

Submit a future to the scheduler independently of the caller. Awaiting the returned handle observes completion. The spawned future and output must satisfy Send and static bounds for this API, even on a current-thread runtime. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): 42 Common mistake: Thinking spawn needs to be awaited before the task can start.

Formal Syntax & Structure

rust syntaxrust
tokio::spawn(future) -> JoinHandle<T>

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    let task = tokio::spawn(async { 14 * 3 });    println!("{}", task.await.unwrap());}
Rust Reference