diff options
| author | Hunter Kvalevog <hunter@kvog.sh> | 2026-09-27 17:28:34 -0500 |
|---|---|---|
| committer | Hunter Kvalevog <hunter@kvog.sh> | 2026-09-27 17:28:34 -0500 |
| commit | cf7c9c5e9e878d74370cb90d7d38cee0b67201c5 (patch) | |
| tree | 59e405f7f98a0451d3a9051c2e2ed03dc3585dfa /rustlings/solutions/18_iterators/iterators1.rs | |
| parent | bb34800b0459bf2db984ea9e4b8d3978cd68a80e (diff) | |
| download | demos-cf7c9c5e9e878d74370cb90d7d38cee0b67201c5.tar.gz demos-cf7c9c5e9e878d74370cb90d7d38cee0b67201c5.zip | |
Diffstat (limited to 'rustlings/solutions/18_iterators/iterators1.rs')
| -rw-r--r-- | rustlings/solutions/18_iterators/iterators1.rs | 26 |
1 files changed, 24 insertions, 2 deletions
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 + } } |
