-1

I'm following the documentation for tokio::runtime::Runtime and trying to adapt it to create a TcpStream that serves as a client to a remote server. I'd like to have the support of a thread pool because my applications may have hundreds of clients each connecting to a remote server.

The problem is when I adapt the example to use TcpStream instead of a TcpListener, it seems to have a problem with the return type:

let runtime = runtime::Builder::new().threaded_scheduler().build()?;
let ret = runtime.block_on(async {
    let mut listener = TcpStream::connect("127.0.0.1:8080").await?;
});

It fails with the error:

the ? operator can only be used in an async block that returns Result or Option (or another type that implements std::ops::Try) the trait std::ops::Try is not implemented for () required by std::ops::Try::from_errorrustc(E0277)

I believe this comes from .await? which is used with bind() and accept(). So is the example outdated or am I missing something with accept()?

ruipacheco
  • 15,025
  • 19
  • 82
  • 138

1 Answers1

0

The method where this code is called from should return Result<TcpStream, Box<dyn std::error::Error>>. I had io::Error instead.

ruipacheco
  • 15,025
  • 19
  • 82
  • 138