Created
March 15, 2020 20:17
-
-
Save NebulaFox/341f96c8d16c3286e6d992e8f2a023ec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashSet; | |
use std::rc::Rc; | |
#[derive(Debug)] | |
enum Check { | |
Known(Rc<String>), | |
Duplicate(Rc<String>), | |
Missing(Rc<String>), | |
Unknown(Rc<String>) | |
} | |
fn main() { | |
let known_values: HashSet<_> = [ | |
Rc::new("a".to_string()), | |
Rc::new("b".to_string()), | |
Rc::new("c".to_string())] | |
.iter().cloned().collect(); | |
let provided_values = vec![ | |
Rc::new("a".to_string()), | |
Rc::new("b".to_string()), | |
Rc::new("z".to_string()), | |
Rc::new("b".to_string()) | |
]; | |
let mut found = HashSet::new(); | |
let mut check_values: Vec<_> = provided_values.iter().cloned() | |
.map(|v| { | |
if known_values.contains(&v) { | |
if found.contains(&v) { | |
Check::Duplicate(v) | |
} else { | |
found.insert(v.clone()); | |
Check::Known(v) | |
} | |
} else { | |
Check::Unknown(v) | |
} | |
}).collect(); | |
let missing = known_values.difference(&found); | |
check_values = missing | |
.cloned() | |
.fold(check_values, |mut cv, m| { | |
cv.push(Check::Missing(m)); | |
cv | |
}); | |
println!("check_values: {:#?}", check_values); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment