I'm trying to learn rust (again) and obviously I have skill issues trying to do something that seems pretty straightforward.
// spawn a connection handler for each
listener
.incoming()
.for_each_concurrent(None, |stream| {
let stream = stream.unwrap();
let remote_addr = stream.peer_addr();
let Ok(remote_addr) = remote_addr else {
stream.shutdown(std::net::Shutdown::Both).unwrap();
return **<what here>**;
};
async move {
spawn(handle_connection(stream));
}
})
.await;
I want to early return from the for_each_concurrent function but I can't for the life of me work out how to return a futures::future::Future<Output=()> which is what it's expecting. Anything I return seems to infer a different return type to what for_each_concurrent wants.
seems to indicate the |stream|{} closure is a FnMut(Self::Item) -> Fut where Fut is defined as Future<Output = ()> but nothing I try seems to be allowed.
Is it possible to early return from this function ?
Similar to closures, every compiler generated future has unique, unnameable type. So in the example, you're trying to return two different types. But Rust is strongly typed, so that's an error.
Putting all the logic in one future would look like so: