Tremaine is a user on octodon.social. You can follow them or interact with them if you have an account anywhere in the fediverse. If you don't, you can sign up here.

did you know, if you have an iterator that emits Result<T, E>, you can collect it into a Result<Vec<T>, E>?

Since an example fits in a toot:

extern crate rand;

fn resultize(i: usize) -> Result<usize, String> {
if rand::random() {
Ok(i)
} else {
Err("whoops!".into())
}
}

fn main() {
let iter = (0..10).map(|i| resultize(i));
let results = iter.collect::<Result<Vec<_>, _>>();
println!("{:?}", results);
}

Tremaine @Mainebot

@balrogboogie

Whoa whoa whoa
Hold on
What's a toot

· Web · 0 · 0