rustlings/exercises/18_iterators/iterators1.rs

26 lines
998 B
Rust
Raw Normal View History

// 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.
2020-08-04 14:57:01 +03:00
fn main() {
2024-04-18 00:34:27 +03:00
// You can optionally experiment here.
}
#[cfg(test)]
mod tests {
#[test]
fn iterators() {
2024-06-28 03:07:56 +03:00
let my_fav_fruits = ["banana", "custard apple", "avocado", "peach", "raspberry"];
2020-08-04 14:57:01 +03:00
2024-06-28 03:07:56 +03:00
// TODO: Create an iterator over the array.
2024-06-28 03:26:35 +03:00
let mut fav_fruits_iterator = todo!();
2020-08-04 14:57:01 +03:00
2024-06-28 03:07:56 +03:00
assert_eq!(fav_fruits_iterator.next(), Some(&"banana"));
2024-06-28 03:26:35 +03:00
assert_eq!(fav_fruits_iterator.next(), todo!()); // TODO: Replace `todo!()`
2024-06-28 03:07:56 +03:00
assert_eq!(fav_fruits_iterator.next(), Some(&"avocado"));
2024-06-28 03:26:35 +03:00
assert_eq!(fav_fruits_iterator.next(), todo!()); // TODO: Replace `todo!()`
2024-06-28 03:07:56 +03:00
assert_eq!(fav_fruits_iterator.next(), Some(&"raspberry"));
2024-06-28 03:26:35 +03:00
assert_eq!(fav_fruits_iterator.next(), todo!()); // TODO: Replace `todo!()`
2024-04-18 00:34:27 +03:00
}
2020-08-04 14:57:01 +03:00
}