I learned that we can collect() an iterable of Result<T> into a Result<Vec<T>>, hence early-terminating the iteration.
fn t_to_u(t: &T) -> Result<U> { ... }
let tt: Vec<T> = vec![];
let result_of_all: Result<Vec<U>> = tt.iter().map(t_to_u).collect();
let result_of_each: Vec<Result<U>> = tt.iter().map(t_to_u).collect();
And also the type signature is partially inferrable:
let result_of_all: Result<Vec<_>> = tt.iter().map(t_to_u).collect();
let result_of_each: Vec<Result<_>> = tt.iter().map(t_to_u).collect();
What I still don't understand is the best practice for converting between different error types, so we don't ever have to worry about specifying the E generic type of anyhow::Result. Googlling came up with thiserror (https://docs.rs/thiserror/1.0.26/thiserror/) which may be worth looking into.
Sorting files from a dir scan.
I learned that we can
collect()
an iterable ofResult<T>
into aResult<Vec<T>>
, hence early-terminating the iteration.And also the type signature is partially inferrable:
What I still don't understand is the best practice for converting between different error types, so we don't ever have to worry about specifying the
E
generic type ofanyhow::Result
. Googlling came up withthiserror
(https://docs.rs/thiserror/1.0.26/thiserror/) which may be worth looking into.