Skip to content

Commit

Permalink
auto merge of rust-lang#12161 : aepsil0n/rust/docs/for-loop, r=alexcr…
Browse files Browse the repository at this point in the history
…ichton

I just started learning Rust and the absence of an explanation of the for-loop in the beginning really bugged me about the tutorial. Hence I simply added these lines, where I would have expected them. I know that there is something later on in the section on traits. However, this simple iteration scheme feels like something that you should be aware of right away.
  • Loading branch information
bors committed Feb 20, 2014
2 parents 1366e04 + 68c960a commit 6532d2f
Showing 1 changed file with 26 additions and 0 deletions.
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

0 comments on commit 6532d2f

Please sign in to comment.