From 67fb6c0eb507965836201551f90c7d637bf6cee0 Mon Sep 17 00:00:00 2001 From: Hunter Kvalevog Date: Sun, 27 Sep 2026 17:28:34 -0500 Subject: rustlings: complete 18 --- rustlings/solutions/18_iterators/iterators1.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'rustlings/solutions/18_iterators/iterators1.rs') diff --git a/rustlings/solutions/18_iterators/iterators1.rs b/rustlings/solutions/18_iterators/iterators1.rs index dcf2377..93a6008 100644 --- a/rustlings/solutions/18_iterators/iterators1.rs +++ b/rustlings/solutions/18_iterators/iterators1.rs @@ -1,4 +1,26 @@ +// 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() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + // You can optionally experiment here. +} + +#[cfg(test)] +mod tests { + #[test] + fn iterators() { + let my_fav_fruits = ["banana", "custard apple", "avocado", "peach", "raspberry"]; + + // Create an iterator over the array. + let mut fav_fruits_iterator = my_fav_fruits.iter(); + + assert_eq!(fav_fruits_iterator.next(), Some(&"banana")); + assert_eq!(fav_fruits_iterator.next(), Some(&"custard apple")); + assert_eq!(fav_fruits_iterator.next(), Some(&"avocado")); + assert_eq!(fav_fruits_iterator.next(), Some(&"peach")); + assert_eq!(fav_fruits_iterator.next(), Some(&"raspberry")); + assert_eq!(fav_fruits_iterator.next(), None); + // ^^^^ reached the end + } } -- cgit v1.2.3