100-exercises-to-learn-rust icon indicating copy to clipboard operation
100-exercises-to-learn-rust copied to clipboard

Error in 8.2 (Futures/Spawn) Example Code

Open iUnknwn opened this issue 1 year ago • 0 comments

In the chapter on async spawn, there's example code for the function echoes:

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?;
        });
    }
}

This code doesn't compile, due to the question mark operator on the final await, with the error: "cannot use the ? operator in an async block that returns ()"

The solution handles this by simply unwrapping the operation, and that works.

Another option can be explicitly returning an Ok, setting the error type with the turbofish syntax.

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?;

            Ok::<(), anyhow::Error>(())
        });
    }
}

This might be also worth calling out in the book, since it seems like there's no way to specify an async block's return type (at least not in the same way it can be done for a closure).

iUnknwn avatar Oct 01 '24 00:10 iUnknwn