Skip to content

Instantly share code, notes, and snippets.

@illicitonion
Created January 21, 2018 09:42
Show Gist options
  • Save illicitonion/9f49f7de0eff262e1e54b5d6d21712ac to your computer and use it in GitHub Desktop.
Save illicitonion/9f49f7de0eff262e1e54b5d6d21712ac to your computer and use it in GitHub Desktop.
pub fn try_collect<Item, Error, Iter: std::iter::Iterator<Item=Result<Item, Error>>, Collection: Default + Extend<Item>>(iter: Iter) -> Result<Collection, Error> {
let mut values: Collection = Default::default();
for item in iter {
match item {
Ok(v) => values.extend(std::iter::once(v)),
Err(err) => return Err(err),
}
}
return Ok(values)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment