Rust tips @rust@octodon.social
Follow

If you have an iterator of `Result`s, you can collect and unwrap all values into a `Vec` on success or return the first `Err` found:

let files_contents = file_names.iter().map(read_file).collect::<Result<Vec<_>, _>>()?;

If you did `.collect::<Vec<_>>()` instead, you'd have an array of mixed `Ok`/`Err` values.

See in playground: is.gd/4GZ45I

· Web · 0 · 0