Skip to content

Commit f07fa76

Browse files
committed
---
yaml --- r: 224731 b: refs/heads/tmp c: 6e61783 h: refs/heads/master i: 224729: 2666031 224727: 497c8f3 v: v3
1 parent fb908b3 commit f07fa76

File tree

7 files changed

+202
-214
lines changed

7 files changed

+202
-214
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ refs/tags/0.11.0: e1247cb1d0d681be034adb4b558b5a0c0d5720f9
2525
refs/tags/0.12.0: f0c419429ef30723ceaf6b42f9b5a2aeb5d2e2d1
2626
refs/heads/beta: 83dee3dfbb452a7558193f3ce171b3c60bf4a499
2727
refs/tags/1.0.0-alpha: e42bd6d93a1d3433c486200587f8f9e12590a4d7
28-
refs/heads/tmp: 504eaa5929d6439402393b03b6e33b96536e8616
28+
refs/heads/tmp: 6e61783dce0efac346845d6a74462d2698632b49
2929
refs/tags/1.0.0-alpha.2: 4c705f6bc559886632d3871b04f58aab093bfa2f
3030
refs/tags/homu-tmp: e58601ab085591c71a27ae82137fc313222c2270
3131
refs/tags/1.0.0-beta: 8cbb92b53468ee2b0c2d3eeb8567005953d40828

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

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

branches/tmp/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
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
% for Loops
2+
3+
The `for` loop is used to loop a particular number of times. Rust’s `for` loops
4+
work a bit differently than in other systems languages, however. Rust’s `for`
5+
loop doesn’t look like this “C-style” `for` loop:
6+
7+
```c
8+
for (x = 0; x < 10; x++) {
9+
printf( "%d\n", x );
10+
}
11+
```
12+
13+
Instead, it looks like this:
14+
15+
```rust
16+
for x in 0..10 {
17+
println!("{}", x); // x: i32
18+
}
19+
```
20+
21+
In slightly more abstract terms,
22+
23+
```ignore
24+
for var in expression {
25+
code
26+
}
27+
```
28+
29+
The expression is an [iterator][iterator]. The iterator gives back a series of
30+
elements. Each element is one iteration of the loop. That value is then bound
31+
to the name `var`, which is valid for the loop body. Once the body is over, the
32+
next value is fetched from the iterator, and we loop another time. When there
33+
are no more values, the `for` loop is over.
34+
35+
[iterator]: iterators.html
36+
37+
In our example, `0..10` is an expression that takes a start and an end position,
38+
and gives an iterator over those values. The upper bound is exclusive, though,
39+
so our loop will print `0` through `9`, not `10`.
40+
41+
Rust does not have the “C-style” `for` loop on purpose. Manually controlling
42+
each element of the loop is complicated and error prone, even for experienced C
43+
developers.
44+
45+
# Enumerate
46+
47+
When you need to keep track of how many times you already looped, you can use the `.enumerate()` function.
48+
49+
## On ranges:
50+
51+
```rust
52+
for (i,j) in (5..10).enumerate() {
53+
println!("i = {} and j = {}", i, j);
54+
}
55+
```
56+
57+
Outputs:
58+
59+
```text
60+
i = 0 and j = 5
61+
i = 1 and j = 6
62+
i = 2 and j = 7
63+
i = 3 and j = 8
64+
i = 4 and j = 9
65+
```
66+
67+
Don't forget to add the parentheses around the range.
68+
69+
## On iterators:
70+
71+
```rust
72+
# let lines = "hello\nworld".lines();
73+
for (linenumber, line) in lines.enumerate() {
74+
println!("{}: {}", linenumber, line);
75+
}
76+
```
77+
78+
Outputs:
79+
80+
```text
81+
0: Content of line one
82+
1: Content of line two
83+
2: Content of line tree
84+
3: Content of line four
85+
```

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

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

0 commit comments

Comments
 (0)