Skip to content

Instantly share code, notes, and snippets.

@Ragnyll
Last active January 26, 2022 05:33
Show Gist options
  • Save Ragnyll/f77d24acb98a647b7d64c92c9b10e9d0 to your computer and use it in GitHub Desktop.
Save Ragnyll/f77d24acb98a647b7d64c92c9b10e9d0 to your computer and use it in GitHub Desktop.
shows that any consumes an iterator
#[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
}));
}
}
}
@Ragnyll
Copy link
Author

Ragnyll commented Jan 26, 2022

This made me think I was crazy for a minute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment