Skip to content

Commit 91e210d

Browse files
committed
---
yaml --- r: 236206 b: refs/heads/stable c: ea5cc76 h: refs/heads/master v: v3
1 parent b9f5605 commit 91e210d

File tree

10 files changed

+224
-216
lines changed

10 files changed

+224
-216
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ refs/heads/tmp: afae2ff723393b3ab4ccffef6ac7c6d1809e2da0
2929
refs/tags/1.0.0-alpha.2: 4c705f6bc559886632d3871b04f58aab093bfa2f
3030
refs/tags/homu-tmp: f859507de8c410b648d934d8f5ec1c52daac971d
3131
refs/tags/1.0.0-beta: 8cbb92b53468ee2b0c2d3eeb8567005953d40828
32-
refs/heads/stable: 74787b98badd43eb069349e30d56b28f8f13be0f
32+
refs/heads/stable: ea5cc76aac7094abfe01657db8312c8450b15200
3333
refs/tags/1.0.0: 55bd4f8ff2b323f317ae89e254ce87162d52a375
3434
refs/tags/1.1.0: bc3c16f09287e5545c1d3f76b7abd54f2eca868b
3535
refs/tags/1.2.0: f557861f822c34f07270347b94b5280de20a597e

branches/stable/.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ sudo: false
2020
before_script:
2121
- ./configure --enable-ccache
2222
script:
23-
- make tidy
24-
- make rustc-stage1 -j4
23+
- make tidy check -j4
2524

2625
env:
2726
- CXX=/usr/bin/g++-4.7

branches/stable/src/doc/trpl/SUMMARY.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
* [Primitive Types](primitive-types.md)
2727
* [Comments](comments.md)
2828
* [if](if.md)
29-
* [for loops](for-loops.md)
30-
* [while loops](while-loops.md)
29+
* [Loops](loops.md)
3130
* [Ownership](ownership.md)
3231
* [References and Borrowing](references-and-borrowing.md)
3332
* [Lifetimes](lifetimes.md)

branches/stable/src/doc/trpl/choosing-your-guarantees.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ scope.
308308

309309
Both of these provide safe shared mutability across threads, however they are prone to deadlocks.
310310
Some level of additional protocol safety can be obtained via the type system.
311+
311312
#### Costs
312313

313314
These use internal atomic-like types to maintain the locks, which are pretty costly (they can block

branches/stable/src/doc/trpl/for-loops.md

Lines changed: 0 additions & 85 deletions
This file was deleted.

branches/stable/src/doc/trpl/loops.md

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
% Loops
2+
3+
Rust currently provides three approaches to performing some kind of iterative activity. They are: `loop`, `while` and `for`. Each approach has its own set of uses.
4+
5+
## loop
6+
7+
The infinite `loop` is the simplest form of loop available in Rust. Using the keyword `loop`, Rust provides a way to loop indefinitely until some terminating statement is reached. Rust's infinite `loop`s look like this:
8+
9+
```rust,ignore
10+
loop {
11+
println!("Loop forever!");
12+
}
13+
```
14+
15+
## while
16+
17+
Rust also has a `while` loop. It looks like this:
18+
19+
```rust
20+
let mut x = 5; // mut x: i32
21+
let mut done = false; // mut done: bool
22+
23+
while !done {
24+
x += x - 3;
25+
26+
println!("{}", x);
27+
28+
if x % 5 == 0 {
29+
done = true;
30+
}
31+
}
32+
```
33+
34+
`while` loops are the correct choice when you’re not sure how many times
35+
you need to loop.
36+
37+
If you need an infinite loop, you may be tempted to write this:
38+
39+
```rust,ignore
40+
while true {
41+
```
42+
43+
However, `loop` is far better suited to handle this case:
44+
45+
```rust,ignore
46+
loop {
47+
```
48+
49+
Rust’s control-flow analysis treats this construct differently than a `while
50+
true`, since we know that it will always loop. In general, the more information
51+
we can give to the compiler, the better it can do with safety and code
52+
generation, so you should always prefer `loop` when you plan to loop
53+
infinitely.
54+
55+
## for
56+
57+
The `for` loop is used to loop a particular number of times. Rust’s `for` loops
58+
work a bit differently than in other systems languages, however. Rust’s `for`
59+
loop doesn’t look like this “C-style” `for` loop:
60+
61+
```c
62+
for (x = 0; x < 10; x++) {
63+
printf( "%d\n", x );
64+
}
65+
```
66+
67+
Instead, it looks like this:
68+
69+
```rust
70+
for x in 0..10 {
71+
println!("{}", x); // x: i32
72+
}
73+
```
74+
75+
In slightly more abstract terms,
76+
77+
```ignore
78+
for var in expression {
79+
code
80+
}
81+
```
82+
83+
The expression is an [iterator][iterator]. The iterator gives back a series of
84+
elements. Each element is one iteration of the loop. That value is then bound
85+
to the name `var`, which is valid for the loop body. Once the body is over, the
86+
next value is fetched from the iterator, and we loop another time. When there
87+
are no more values, the `for` loop is over.
88+
89+
[iterator]: iterators.html
90+
91+
In our example, `0..10` is an expression that takes a start and an end position,
92+
and gives an iterator over those values. The upper bound is exclusive, though,
93+
so our loop will print `0` through `9`, not `10`.
94+
95+
Rust does not have the “C-style” `for` loop on purpose. Manually controlling
96+
each element of the loop is complicated and error prone, even for experienced C
97+
developers.
98+
99+
### Enumerate
100+
101+
When you need to keep track of how many times you already looped, you can use the `.enumerate()` function.
102+
103+
#### On ranges:
104+
105+
```rust
106+
for (i,j) in (5..10).enumerate() {
107+
println!("i = {} and j = {}", i, j);
108+
}
109+
```
110+
111+
Outputs:
112+
113+
```text
114+
i = 0 and j = 5
115+
i = 1 and j = 6
116+
i = 2 and j = 7
117+
i = 3 and j = 8
118+
i = 4 and j = 9
119+
```
120+
121+
Don't forget to add the parentheses around the range.
122+
123+
#### On iterators:
124+
125+
```rust
126+
# let lines = "hello\nworld".lines();
127+
for (linenumber, line) in lines.enumerate() {
128+
println!("{}: {}", linenumber, line);
129+
}
130+
```
131+
132+
Outputs:
133+
134+
```text
135+
0: Content of line one
136+
1: Content of line two
137+
2: Content of line tree
138+
3: Content of line four
139+
```
140+
141+
## Ending iteration early
142+
143+
Let’s take a look at that `while` loop we had earlier:
144+
145+
```rust
146+
let mut x = 5;
147+
let mut done = false;
148+
149+
while !done {
150+
x += x - 3;
151+
152+
println!("{}", x);
153+
154+
if x % 5 == 0 {
155+
done = true;
156+
}
157+
}
158+
```
159+
160+
We had to keep a dedicated `mut` boolean variable binding, `done`, to know
161+
when we should exit out of the loop. Rust has two keywords to help us with
162+
modifying iteration: `break` and `continue`.
163+
164+
In this case, we can write the loop in a better way with `break`:
165+
166+
```rust
167+
let mut x = 5;
168+
169+
loop {
170+
x += x - 3;
171+
172+
println!("{}", x);
173+
174+
if x % 5 == 0 { break; }
175+
}
176+
```
177+
178+
We now loop forever with `loop` and use `break` to break out early. Issuing an explicit `return` statement will also serve to terminate the loop early.
179+
180+
`continue` is similar, but instead of ending the loop, goes to the next
181+
iteration. This will only print the odd numbers:
182+
183+
```rust
184+
for x in 0..10 {
185+
if x % 2 == 0 { continue; }
186+
187+
println!("{}", x);
188+
}
189+
```
190+
191+
## Loop labels
192+
193+
You may also encounter situations where you have nested loops and need to
194+
specify which one your `break` or `continue` statement is for. Like most
195+
other languages, by default a `break` or `continue` will apply to innermost
196+
loop. In a sitation where you would like to a `break` or `continue` for one
197+
of the outer loops, you can use labels to specify which loop the `break` or
198+
`continue` statement applies to. This will only print when both `x` and `y` are
199+
odd:
200+
201+
```rust
202+
'outer: for x in 0..10 {
203+
'inner: for y in 0..10 {
204+
if x % 2 == 0 { continue 'outer; } // continues the loop over x
205+
if y % 2 == 0 { continue 'inner; } // continues the loop over y
206+
println!("x: {}, y: {}", x, y);
207+
}
208+
}
209+
```

branches/stable/src/doc/trpl/the-stack-and-the-heap.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ frame. But before we can show what happens when `foo()` is called, we need to
7373
visualize what’s going on with memory. Your operating system presents a view of
7474
memory to your program that’s pretty simple: a huge list of addresses, from 0
7575
to a large number, representing how much RAM your computer has. For example, if
76-
you have a gigabyte of RAM, your addresses go from `0` to `1,073,741,824`. That
76+
you have a gigabyte of RAM, your addresses go from `0` to `1,073,741,823`. That
7777
number comes from 2<sup>30</sup>, the number of bytes in a gigabyte.
7878

7979
This memory is kind of like a giant array: addresses start at zero and go
@@ -551,7 +551,7 @@ is a great introduction.
551551

552552
[wilson]: http://www.cs.northwestern.edu/~pdinda/icsclass/doc/dsa.pdf
553553

554-
## Semantic impact
554+
## Semantic impact
555555

556556
Stack-allocation impacts the Rust language itself, and thus the developer’s
557557
mental model. The LIFO semantics is what drives how the Rust language handles

0 commit comments

Comments
 (0)