Error with ? use in Tokio TCP Server
⚓ Rust 📅 2026-05-21 👤 surdeus 👁️ 1Hey Everyone,
I'm trying my hand at writing a TCP server with Tokio. I'm attempting to put some of the functionality into a lib instead of writing it all in the binary itself. here's the current relevant portion of the code in the lib: Basic Tokio TCP server in Rust Lib. - Pastebin.com
I'm getting an error on line 30 and 36 in the pastebin code regarding the ?. The error for both is:
the `?` operator can only be used in an async block that returns `Result` or `Option` (or another type that implements `FromResidual`) cannot use the `?` operator in an async block that returns `()`
Now I've seen this before and usually I just need to have a Result return on the function, which I do in this case. And Tokio's documentation has the same thing but I'm getting the error. I'm not sure why. Maybe it's because I'm in the scope of the loop and it's not able to bubble up through it? I messed around a bit with the match arms trying to add OK(()) returns thinking although the error was pointing to those ?'s maybe adding returns to each relevant match arm would work but it didn't do anything. Is it because I'm in a lib instead of a bin for Rust that changes the way this logic behaves? I wouldn't think so but this is the first time I'm trying to make a lib.
Additionally, I have a UDP server written already in the lib that uses basically the same framework, same function return type, and that has the ? working just fine. Granted it's in a single loop and not spawning another tokio thread so there is admittedly less complexity with that portion: Basic Tokio UDP Impl. - Pastebin.com.
Does anyone have any guidance on the ? error?
Thanks
4 posts - 3 participants
🏷️ Rust_feed