Rust - Handling Results In A Map Closure
10 points by BD103
10 points by BD103
I usually lean towards collect(), but it's cool to see alternatives
I often struggle with fallible functions in maps, and felt really empowered when I first learned that you can collect into a Result. But doing that does sometimes produce some unwieldy-looking code. Those turbofishes especially get gnarly.
I sometimes have to remind myself that a for loop is almost never the least clear option, especially if I'm calling a bunch of fallible operations, or get lost in the intermediate types.
There’s a yet unstable try_collect() that can be used as an alternative to the plain collect to nudge the compiler towards collecting into a Result. If you’re using the itertools crate, the same function is also available via the Itertools extension trait.