In one of the examples at https://rust-exercises.com/100-exercises/08_futures/02_spawn#tokiospawn , there's this snippet of code:
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:
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();
});
}
}
In one of the examples at https://rust-exercises.com/100-exercises/08_futures/02_spawn#tokiospawn , there's this snippet of code:
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: