You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use tokio::net::TcpListener;pubasyncfnecho(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(asyncmove{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.
pubasyncfnecho(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(asyncmove{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).
The text was updated successfully, but these errors were encountered:
In the chapter on async spawn, there's example code for the function
echoes
: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.
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).
The text was updated successfully, but these errors were encountered: