8.2: tokio spawn + .async?

Author: RedGlowCreated Jun 27, 2026Updated Jun 27, 2026

In one of the examples at https://rust-exercises.com/100-exercises/08_futures/02_spawn#tokiospawn , there's this snippet of code:

rust
use tokio::net::TcpListener;

pub async fn echo(listener: TcpListener) -> Result<(), anyhow::Error> {
    loop {
        let (mut socket, _) = listener.accept().await?;
        // Spawn a background task to handle the connection
        // thus allowing the main task to immediately start 
        // accepting new connections
        tokio::spawn(async move {
            let (mut reader, mut writer) = socket.split();
            tokio::io::copy(&mut reader, &mut writer).await?;
        });
    }
}

It doesn't compile correctly, with the error Cannot use the `?` operator in a function that returns `()` [E0277] on the very last ? operator.

In the solution provided for the exercise, the code is indeed different, and uses an unwrap:

rust
async fn echo(listener: TcpListener) -> Result<(), anyhow::Error> {
    loop {
        let (mut socket, _) = listener.accept().await?;
        tokio::spawn(async move {
            let (mut reader, mut writer) = socket.split();
            tokio::io::copy(&mut reader, &mut writer).await.unwrap();
        });
    }
}

Source: mainmatter/100-exercises-to-learn-rust