[][src]Function tokio::spawn

pub fn spawn<F>(f: F) -> Spawn where
    F: Future<Output = ()> + 'static + Send

Spawns a future on the default executor.

In order for a future to do work, it must be spawned on an executor. The spawn function is the easiest way to do this. It spawns a future on the default executor for the current execution context (tracked using a thread-local variable).

The default executor is usually a thread pool.


In this example, a server is started and spawn is used to start a new task that processes each received connection.

use tokio::net::TcpListener;

let mut listener = TcpListener::bind("").await?;

loop {
    let (socket, _) = listener.accept().await?;

    tokio::spawn(async move {
        // Process each socket concurrently.


This function will panic if the default executor is not set or if spawning onto the default executor returns an error. To avoid the panic, use DefaultExecutor.