rustlings/exercises/18_iterators/iterators1.rs

26 lines
949 B
Rust
Raw Normal View History

2024-10-28 20:46:17 +00:00
// When performing operations on elements within a collection, iterators are
// essential. This module helps you get familiar with the structure of using an
// iterator and how to go through elements within an iterable collection.
fn main() {
// You can optionally experiment here.
}
#[cfg(test)]
mod tests {
#[test]
fn iterators() {
let my_fav_fruits = ["banana", "custard apple", "avocado", "peach", "raspberry"];
// TODO: Create an iterator over the array.
let mut fav_fruits_iterator = my_fav_fruits.iter();
2024-10-28 20:46:17 +00:00
assert_eq!(fav_fruits_iterator.next(), Some(&"banana"));
assert_eq!(fav_fruits_iterator.next(), Some(&"custard apple"));
2024-10-28 20:46:17 +00:00
assert_eq!(fav_fruits_iterator.next(), Some(&"avocado"));
assert_eq!(fav_fruits_iterator.next(), Some(&"peach"));
2024-10-28 20:46:17 +00:00
assert_eq!(fav_fruits_iterator.next(), Some(&"raspberry"));
assert_eq!(fav_fruits_iterator.next(), None);
2024-10-28 20:46:17 +00:00
}
}