How to `try_collect()` on Non-Futures

I was just using the try_collect() function out of the futures crate when dealing with Streams and it was exactly what I needed, but when I needed the same thing with the standard library, I got stuck because I couldn't find it. I looked around for a crate for it, but nothing came up anywhere. I even started to try to implement it myself real quick ( which did work, but it required an extra Vec allocation and the nightly try_trait feature, so that wasn't good ).

Finaly I found this topic which said that the out of the box collect() actually works like try_collect() when iterating over Results/Options!

Here was my use-case. I hope it helps anybody who might have the same problem:

(playground):

// Source data
let socket_strings = vec!["192.168.0.133:8800", "192.168.0.123:8000"];

// Parse to Vector  
let socket_addrs: Vec<SocketAddr> = socket_strings
    .iter()
    .map(|x| x.parse())
    .collect::<Result<_, _>>()?;

It is nice and succinct and I'm very satisfied with the Result :wink: .

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.