Skip to content

Commit

Permalink
Explain container iteration in the loop tutorial
Browse files Browse the repository at this point in the history
 As suggested by @pnkfelix in #12161, this extends the examples by a for-loop
 that iterates over a string as an example for container iteration.
  • Loading branch information
milibopp committed Feb 11, 2014
1 parent fbadb36 commit 68c960a
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/doc/tutorial.md
Expand Up @@ -582,8 +582,7 @@ 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
(or, more generally, anything implementing the `Iterator` trait):
There is also a for-loop that can be used to iterate over a range of numbers:
~~~~
for n in range(0, 5) {
Expand All @@ -593,6 +592,21 @@ for n in range(0, 5) {
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
Expand Down

5 comments on commit 68c960a

@bors
Copy link
Contributor

@bors bors commented on 68c960a Feb 20, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at milibopp@68c960a

@bors
Copy link
Contributor

@bors bors commented on 68c960a Feb 20, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging aepsil0n/rust/docs/for-loop = 68c960a into auto

@bors
Copy link
Contributor

@bors bors commented on 68c960a Feb 20, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aepsil0n/rust/docs/for-loop = 68c960a merged ok, testing candidate = 6532d2f

@bors
Copy link
Contributor

@bors bors commented on 68c960a Feb 20, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 68c960a Feb 20, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 6532d2f

Please sign in to comment.