Suppose we have a list of elements Vec<K> and we want to transform it into another list Vec<V>. If we have f: (&K) -> V, this straight forward.
Suppose now, that the function f can fail, so we have f: (&K) -> Option<V> or f: (&K) -> anyhow::Result<V>. Then, if we use collect, we get either (1) a Vec<V> or (2) the first error.
Suppose we wanted a different behavior, (Vec<V>, Vec<Err>), two vectors -- a collection of all the good output, and a collection of all the errors. This is not a difficult function to write. Is there an idiomatic way to do this in Rust ?
[Suppose that the index does not matter, i.e. within both V and Err, we have enough info to know which K it came from]
Not really. That's only if you purposefully collect into a Result<Vec<_>, _>. Instead, you could flip the composition of types inside out, and collect into a Vec<Result<_, _>>, or even directly unzip the iterator if you are fine with having Options in your collections.
Also note that for less specialized iterator methods, you can always use the very general fold() method. Here's a function that spares you the two unwrap()s by using fold(), and which is generic over both collection types: Playground.
fn collect_results<T, E, I, C, D>(iter: I) -> (C, D)
where
I: IntoIterator<Item=Result<T, E>>,
C: Default + Extend<T>,
D: Default + Extend<E>,
{
iter.into_iter().fold(Default::default(), |(mut v, mut e), r| {
match r {
Ok(val) => v.extend(Some(val)),
Err(err) => e.extend(Some(err)),
}
(v, e)
})
}
No it isn't. Obviously there won't be a large discovery in these kind of trivial algorithms. But partition bakes in the type of the output items (they must be of the same as the input), so if you use it, you must then redundantly unwrap and unwrap_err each individual item of both collections.
My solution solves this problem by pattern matching the Results in-place, hence it is both safer and faster, because it avoids unwrapping and obviates the need for a second pass, saving two allocations.