summaryrefslogtreecommitdiff
path: root/rustlings/solutions/18_iterators/iterators1.rs
diff options
context:
space:
mode:
authorHunter Kvalevog <hunter@kvog.sh>2026-09-27 17:28:34 -0500
committerHunter Kvalevog <hunter@kvog.sh>2026-09-27 17:28:34 -0500
commit67fb6c0eb507965836201551f90c7d637bf6cee0 (patch)
tree59e405f7f98a0451d3a9051c2e2ed03dc3585dfa /rustlings/solutions/18_iterators/iterators1.rs
parent54731f0fc0b0fde18438853c0a8abaa968edb57a (diff)
rustlings: complete 18
Diffstat (limited to 'rustlings/solutions/18_iterators/iterators1.rs')
-rw-r--r--rustlings/solutions/18_iterators/iterators1.rs26
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
+ }
}