Skip to content

Instantly share code, notes, and snippets.

@gregberns
Created April 18, 2019 22:37
Show Gist options
  • Save gregberns/f5fac3df9d5790c03647e014a91fdbbd to your computer and use it in GitHub Desktop.
Save gregberns/f5fac3df9d5790c03647e014a91fdbbd to your computer and use it in GitHub Desktop.
Rust implementation for `partition` function for `Result`
fn partition<A,B>(list: Vec<Result<A,B>>) -> (Vec<A>, Vec<B>) {
let mut aa = vec!();
let mut bb = vec!();
for i in list {
match i {
Ok(a) => aa.push(a),
Err(b) => bb.push(b),
};
}
(aa, bb)
}
fn main() {
let rows_result: Vec<Result<String, i32>> =
vec!(Ok("abc".to_string()), Err(123));
let (rows, row_parse_errors): (Vec<String>, Vec<i32>) =
partition(rows_result);
assert_eq!(rows, vec!("abc".to_string()));
assert_eq!(row_parse_errors, vec!(123));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment