rustlings/exercises/18_iterators/iterators1.rs

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