File tree Expand file tree Collapse file tree 1 file changed +11
-5
lines changed Expand file tree Collapse file tree 1 file changed +11
-5
lines changed Original file line number Diff line number Diff line change @@ -42,35 +42,41 @@ Rust does not have the “C-style” `for` loop on purpose. Manually controlling
42
42
each element of the loop is complicated and error prone, even for experienced C
43
43
developers.
44
44
45
- # Loopcounter
45
+ # Enumerate
46
46
47
47
When you need to keep track of how many times you already looped, you can use the ` .enumerate() ` function.
48
48
49
- #### On ranges:
49
+ ## On ranges:
50
50
51
51
``` rust
52
52
for (i ,j ) in (5 .. 10 ). enumerate () {
53
53
println! (" i = {} and j = {}" , i , j );
54
54
}
55
55
```
56
+
56
57
Outputs:
57
- ```
58
+
59
+ ``` rust
58
60
i = 0 and j = 5
59
61
i = 1 and j = 6
60
62
i = 2 and j = 7
61
63
i = 3 and j = 8
62
64
i = 4 and j = 9
63
65
```
66
+
64
67
Don't forget to add the parentheses around the range.
65
68
66
- #### On iterators:
69
+ ## On iterators:
70
+
67
71
``` rust
68
72
for (linenumber , line ) in lines . enumerate () {
69
73
println! (" {}: {}" , linenumber , line );
70
74
}
71
75
```
76
+
72
77
Outputs:
73
- ```
78
+
79
+ ``` rust
74
80
0 : Content of line one
75
81
1 : Content of line two
76
82
2 : Content of line tree
You can’t perform that action at this time.
0 commit comments