Last active
January 26, 2022 05:33
-
-
Save Ragnyll/f77d24acb98a647b7d64c92c9b10e9d0 to your computer and use it in GitHub Desktop.
shows that any consumes an iterator
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
#[cfg(test)] | |
mod test { | |
struct SimpleStruct { | |
id: String, | |
} | |
impl SimpleStruct { | |
fn new(id: &str) -> Self { | |
Self { | |
id: String::from(id), | |
} | |
} | |
} | |
#[test] | |
fn test_any_works() { | |
let expected = vec![String::from("1")]; | |
let my_ids = vec![ | |
SimpleStruct::new(&"1"), | |
SimpleStruct::new(&"2"), | |
SimpleStruct::new(&"3"), | |
]; | |
let mut just_ids = my_ids.iter().map(|s| s.id.clone()); | |
for e in expected { | |
assert!(just_ids.any(|d| { | |
d == e | |
})); | |
} | |
} | |
#[test] | |
#[should_panic] | |
fn test_any_is_consuming() { | |
let expected = vec![String::from("1"), String::from("1")]; | |
let my_ids = vec![ | |
SimpleStruct::new(&"0"), | |
SimpleStruct::new(&"1"), | |
SimpleStruct::new(&"2"), | |
SimpleStruct::new(&"3"), | |
]; | |
let mut just_ids = my_ids.iter().map(|s| s.id.clone()); | |
for e in expected { | |
assert!(just_ids.any(|d| { | |
d == e | |
})); | |
} | |
} | |
#[test] | |
#[should_panic] | |
fn test_any_consumes_non_matching() { | |
let my_ids = vec![ | |
SimpleStruct::new(&"0"), | |
SimpleStruct::new(&"1"), | |
SimpleStruct::new(&"2"), | |
SimpleStruct::new(&"3"), | |
]; | |
let mut just_ids = my_ids.iter().map(|s| s.id.clone()); | |
assert!(just_ids.any(|d| { | |
d == String::from("1") | |
})); | |
assert!(just_ids.any(|d| { | |
d == String::from("0") | |
})); | |
} | |
#[test] | |
fn test_any_works_into_iter() { | |
let expected = vec![String::from("1")]; | |
let my_ids = vec![ | |
SimpleStruct::new(&"1"), | |
SimpleStruct::new(&"2"), | |
SimpleStruct::new(&"3"), | |
]; | |
let mut just_ids = my_ids.into_iter().map(|s| s.id.clone()); | |
for e in expected { | |
assert!(just_ids.any(|d| { | |
d == e | |
})); | |
} | |
} | |
#[test] | |
#[should_panic] | |
fn test_any_is_consuming_into_iter() { | |
let expected = vec![String::from("1"), String::from("1")]; | |
let my_ids = vec![ | |
SimpleStruct::new(&"1"), | |
SimpleStruct::new(&"2"), | |
SimpleStruct::new(&"3"), | |
]; | |
let mut just_ids = my_ids.into_iter().map(|s| s.id.clone()); | |
for e in expected { | |
assert!(just_ids.any(|d| { | |
d == e | |
})); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This made me think I was crazy for a minute.