Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flesh out for loop example #1841

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading