Skip to content

Commit

Permalink
Flesh out for loop example (google#1841)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
randomPoison authored Feb 22, 2024
1 parent 9023dd9 commit d75dd5d
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/control-flow-basics/loops/for.md
Original file line number Diff line number Diff line change
@@ -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}");
}
}
```

<details>

- 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.

Expand Down

0 comments on commit d75dd5d

Please sign in to comment.