Skip to content

Document for-loop in tutorial section on loops #12161

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

Closed
wants to merge 2 commits into from
Closed
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
26 changes: 26 additions & 0 deletions src/doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,32 @@ loop {
This code prints out a weird sequence of numbers and stops as soon as
it finds one that can be divided by five.

There is also a for-loop that can be used to iterate over a range of numbers:

~~~~
for n in range(0, 5) {
println!("{}", n);
}
~~~~

The snippet above prints integer numbers under 5 starting at 0.

More generally, a for loop works with anything implementing the `Iterator` trait.
Data structures can provide one or more methods that return iterators over
their contents. For example, strings support iteration over their contents in
various ways:

~~~~
let s = "Hello";
for c in s.chars() {
println!("{}", c);
}
~~~~

The snippet above prints the characters in "Hello" vertically, adding a new
line after each character.


# Data structures

## Structs
Expand Down