← All notes

tokio::spawn runs an async block as a task; it must be 'static and Send

Answer: Pass an async block to tokio::spawn; it returns a JoinHandle, and .await on the handle gives a Result with the block's return value. The spawned task must be 'static (use async move so it owns its data) and Send (everything held across an .await must be Send).

Example

#[tokio::main]
async fn main() {
    let handle = tokio::spawn(async {
        "return value"
    });
    let out = handle.await.unwrap();
    println!("GOT {}", out);
}

A server spawns a new task for each inbound socket and moves the socket into it:

loop {
    let (socket, _) = listener.accept().await.unwrap();
    tokio::spawn(async move { process(socket).await; });
}

Errors from the JoinHandle

Awaiting on JoinHandle returns a Result. It is an Err when the task panics or when the runtime shutting down forcefully cancels the task.

The 'static bound

The spawned task must not contain any references to data owned outside the task. By default, variables are not moved into async blocks; async move will instruct the compiler to move v into the spawned task, so the task owns all of its data. If a single piece of data must be accessible from more than one task concurrently, share it with synchronization primitives such as Arc.

The Send bound

Send allows the Tokio runtime to move the tasks between threads while suspended at an .await. A task is Send when all data that is held across .await calls is Send. An Rc dropped before the .await is fine; an Rc used after it makes the task not Send.

Cost

Tasks in Tokio are very lightweight: under the hood, each one needs only a single allocation and 64 bytes of memory, so spawning thousands, if not millions of tasks is fine.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Tokio tutorial, Spawning; tokio-rs/website (GitHub), spawning.md (source page). MIT (https://raw.githubusercontent.com/tokio-rs/website/master/LICENSE). Copyright Tokio contributors. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Tokio tutorial, Spawning; tokio-rs/website (GitHub), spawning.md (source page). MIT (https://raw.githubusercontent.com/tokio-rs/website/master/LICENSE). Copyright Tokio contributors.

Cite this

Permanent ID: iil:note:ffgt7ah3

iinventedlight. 2026. "tokio::spawn runs an async block as a task; it must be 'static and Send." iinventedlight Library, iil:note:ffgt7ah3, version of 2026-11-18. https://iinventedlight.com/library/notes/rust-tokio-spawn/. License: CC-BY-4.0.

@misc{iil_note_ffgt7ah3,
  author = {{iinventedlight}},
  title = {{tokio::spawn runs an async block as a task; it must be 'static and Send}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-18},
  url = {https://iinventedlight.com/library/notes/rust-tokio-spawn/},
  note = {Permanent ID iil:note:ffgt7ah3. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-tokio-spawn.md.