From d75dd5d6819943e22fe2234c0adccb8db8c3c353 Mon Sep 17 00:00:00 2001 From: Nicole L Date: Thu, 22 Feb 2024 07:49:49 -0800 Subject: [PATCH] Flesh out `for` loop example (#1841) * Add example of iterating over a collection. * Update speaker notes to call out the use of iterators. I think it's useful to call out that `for` loops primarily are used to iterate over a collection of objects, even though we haven't yet talked about any concrete collection types at this point. I think using the array literal syntax is simple enough to understand that it should be quick to explain when we get to this slide. --- src/control-flow-basics/loops/for.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/control-flow-basics/loops/for.md b/src/control-flow-basics/loops/for.md index edbfbaa55c8e..c26c0b4a9b70 100644 --- a/src/control-flow-basics/loops/for.md +++ b/src/control-flow-basics/loops/for.md @@ -1,19 +1,25 @@ # `for` The [`for` loop](https://doc.rust-lang.org/std/keyword.for.html) iterates over -ranges of values: +ranges of values or the items in a collection: ```rust,editable fn main() { for x in 1..5 { println!("x: {x}"); } + + for elem in [1, 2, 3, 4, 5] { + println!("elem: {elem}"); + } } ```
-- We will discuss iteration later; for now, just stick to range expressions. +- Under the hood `for` loops use a concept called "iterators" to handle + iterating over different kinds of ranges/collections. Iterators will be + discussed in more detail later. - Note that the `for` loop only iterates to `4`. Show the `1..=5` syntax for an inclusive range.