@@ -5,7 +5,7 @@ Let's talk about loops.
5
5
Remember Rust's ` for ` loop? Here's an example:
6
6
7
7
``` {rust}
8
- for x in range(0i, 10i ) {
8
+ for x in range(0, 10 ) {
9
9
println!("{}", x);
10
10
}
11
11
```
@@ -17,7 +17,7 @@ call the `.next()` method on repeatedly, and it gives us a sequence of things.
17
17
Like this:
18
18
19
19
``` {rust}
20
- let mut range = range(0i, 10i );
20
+ let mut range = range(0, 10 );
21
21
22
22
loop {
23
23
match range.next() {
32
32
We make a mutable binding to the return value of ` range ` , which is our iterator.
33
33
We then ` loop ` , with an inner ` match ` . This ` match ` is used on the result of
34
34
` range.next() ` , which gives us a reference to the next value of the iterator.
35
- ` next ` returns an ` Option<int > ` , in this case, which will be ` Some(int ) ` when
36
- we have a value and ` None ` once we run out. If we get ` Some(int ) ` , we print it
35
+ ` next ` returns an ` Option<i32 > ` , in this case, which will be ` Some(i32 ) ` when
36
+ we have a value and ` None ` once we run out. If we get ` Some(i32 ) ` , we print it
37
37
out, and if we get ` None ` , we ` break ` out of the loop.
38
38
39
39
This code sample is basically the same as our ` for ` loop version. The ` for `
@@ -50,9 +50,9 @@ primitive. For example, if you needed to iterate over the contents of
50
50
a vector, you may be tempted to write this:
51
51
52
52
``` {rust}
53
- let nums = vec![1i, 2i, 3i ];
53
+ let nums = vec![1, 2, 3 ];
54
54
55
- for i in range(0u , nums.len()) {
55
+ for i in range(0 , nums.len()) {
56
56
println!("{}", nums[i]);
57
57
}
58
58
```
@@ -62,7 +62,7 @@ vectors returns an iterator which iterates through a reference to each element
62
62
of the vector in turn. So write this:
63
63
64
64
``` {rust}
65
- let nums = vec![1i, 2i, 3i ];
65
+ let nums = vec![1, 2, 3 ];
66
66
67
67
for num in nums.iter() {
68
68
println!("{}", num);
@@ -79,12 +79,12 @@ very common with iterators: we can ignore unnecessary bounds checks, but still
79
79
know that we're safe.
80
80
81
81
There's another detail here that's not 100% clear because of how ` println! `
82
- works. ` num ` is actually of type ` &int ` . That is, it's a reference to an ` int ` ,
83
- not an ` int ` itself. ` println! ` handles the dereferencing for us, so we don't
82
+ works. ` num ` is actually of type ` &i32 ` . That is, it's a reference to an ` i32 ` ,
83
+ not an ` i32 ` itself. ` println! ` handles the dereferencing for us, so we don't
84
84
see it. This code works fine too:
85
85
86
86
``` {rust}
87
- let nums = vec![1i, 2i, 3i ];
87
+ let nums = vec![1, 2, 3 ];
88
88
89
89
for num in nums.iter() {
90
90
println!("{}", *num);
@@ -118,7 +118,7 @@ The most common consumer is `collect()`. This code doesn't quite compile,
118
118
but it shows the intention:
119
119
120
120
``` {rust,ignore}
121
- let one_to_one_hundred = range(1i, 101i ).collect();
121
+ let one_to_one_hundred = range(1, 101 ).collect();
122
122
```
123
123
124
124
As you can see, we call ` collect() ` on our iterator. ` collect() ` takes
@@ -128,7 +128,7 @@ type of things you want to collect, and so you need to let it know.
128
128
Here's the version that does compile:
129
129
130
130
``` {rust}
131
- let one_to_one_hundred = range(1i, 101i ).collect::<Vec<int >>();
131
+ let one_to_one_hundred = range(1, 101 ).collect::<Vec<i32 >>();
132
132
```
133
133
134
134
If you remember, the ` ::<> ` syntax allows us to give a type hint,
@@ -138,7 +138,7 @@ and so we tell it that we want a vector of integers.
138
138
is one:
139
139
140
140
``` {rust}
141
- let greater_than_forty_two = range(0i, 100i )
141
+ let greater_than_forty_two = range(0, 100 )
142
142
.find(|x| *x > 42);
143
143
144
144
match greater_than_forty_two {
@@ -155,8 +155,8 @@ element, `find` returns an `Option` rather than the element itself.
155
155
Another important consumer is ` fold ` . Here's what it looks like:
156
156
157
157
``` {rust}
158
- let sum = range(1i, 4i )
159
- .fold(0i , |sum, x| sum + x);
158
+ let sum = range(1, 4 )
159
+ .fold(0 , |sum, x| sum + x);
160
160
```
161
161
162
162
` fold() ` is a consumer that looks like this:
@@ -172,24 +172,24 @@ in this iterator:
172
172
173
173
| base | accumulator | element | closure result |
174
174
| ------| -------------| ---------| ----------------|
175
- | 0i | 0i | 1i | 1i |
176
- | 0i | 1i | 2i | 3i |
177
- | 0i | 3i | 3i | 6i |
175
+ | 0 | 0 | 1 | 1 |
176
+ | 0 | 1 | 2 | 3 |
177
+ | 0 | 3 | 3 | 6 |
178
178
179
179
We called ` fold() ` with these arguments:
180
180
181
181
``` {rust}
182
- # range(1i, 4i )
183
- .fold(0i , |sum, x| sum + x);
182
+ # range(1, 4 )
183
+ .fold(0 , |sum, x| sum + x);
184
184
```
185
185
186
- So, ` 0i ` is our base, ` sum ` is our accumulator, and ` x ` is our element. On the
187
- first iteration, we set ` sum ` to ` 0i ` , and ` x ` is the first element of ` nums ` ,
188
- ` 1i ` . We then add ` sum ` and ` x ` , which gives us ` 0i + 1i = 1i ` . On the second
186
+ So, ` 0 ` is our base, ` sum ` is our accumulator, and ` x ` is our element. On the
187
+ first iteration, we set ` sum ` to ` 0 ` , and ` x ` is the first element of ` nums ` ,
188
+ ` 1 ` . We then add ` sum ` and ` x ` , which gives us ` 0 + 1 = 1 ` . On the second
189
189
iteration, that value becomes our accumulator, ` sum ` , and the element is
190
- the second element of the array, ` 2i ` . ` 1i + 2i = 3i ` , and so that becomes
190
+ the second element of the array, ` 2 ` . ` 1 + 2 = 3 ` , and so that becomes
191
191
the value of the accumulator for the last iteration. On that iteration,
192
- ` x ` is the last element, ` 3i ` , and ` 3i + 3i = 6i ` , which is our final
192
+ ` x ` is the last element, ` 3 ` , and ` 3 + 3 = 6 ` , which is our final
193
193
result for our sum. ` 1 + 2 + 3 = 6 ` , and that's the result we got.
194
194
195
195
Whew. ` fold ` can be a bit strange the first few times you see it, but once it
@@ -210,14 +210,14 @@ This code, for example, does not actually generate the numbers
210
210
` 1-100 ` , and just creates a value that represents the sequence:
211
211
212
212
``` {rust}
213
- let nums = range(1i, 100i );
213
+ let nums = range(1, 100 );
214
214
```
215
215
216
216
Since we didn't do anything with the range, it didn't generate the sequence.
217
217
Let's add the consumer:
218
218
219
219
``` {rust}
220
- let nums = range(1i, 100i ).collect::<Vec<int >>();
220
+ let nums = range(1, 100 ).collect::<Vec<i32 >>();
221
221
```
222
222
223
223
Now, ` collect() ` will require that ` range() ` give it some numbers, and so
@@ -228,7 +228,7 @@ which you've used before. `iter()` can turn a vector into a simple iterator
228
228
that gives you each element in turn:
229
229
230
230
``` {rust}
231
- let nums = [1i, 2i, 3i ];
231
+ let nums = [1, 2, 3 ];
232
232
233
233
for num in nums.iter() {
234
234
println!("{}", num);
@@ -239,12 +239,12 @@ These two basic iterators should serve you well. There are some more
239
239
advanced iterators, including ones that are infinite. Like ` count ` :
240
240
241
241
``` {rust}
242
- std::iter::count(1i, 5i );
242
+ std::iter::count(1, 5 );
243
243
```
244
244
245
245
This iterator counts up from one, adding five each time. It will give
246
246
you a new integer every time, forever (well, technically, until it reaches the
247
- maximum number representable by an ` int ` ). But since iterators are lazy,
247
+ maximum number representable by an ` i32 ` ). But since iterators are lazy,
248
248
that's okay! You probably don't want to use ` collect() ` on it, though...
249
249
250
250
That's enough about iterators. Iterator adapters are the last concept
@@ -256,7 +256,7 @@ we need to talk about with regards to iterators. Let's get to it!
256
256
a new iterator. The simplest one is called ` map ` :
257
257
258
258
``` {rust,ignore}
259
- range(1i, 100i ).map(|x| x + 1i );
259
+ range(1, 100 ).map(|x| x + 1 );
260
260
```
261
261
262
262
` map ` is called upon another iterator, and produces a new iterator where each
@@ -267,15 +267,15 @@ compile the example, you'll get a warning:
267
267
``` {notrust,ignore}
268
268
warning: unused result which must be used: iterator adaptors are lazy and
269
269
do nothing unless consumed, #[warn(unused_must_use)] on by default
270
- range(1i, 100i ).map(|x| x + 1i );
270
+ range(1, 100 ).map(|x| x + 1 );
271
271
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
272
272
```
273
273
274
274
Laziness strikes again! That closure will never execute. This example
275
275
doesn't print any numbers:
276
276
277
277
``` {rust,ignore}
278
- range(1i, 100i ).map(|x| println!("{}", x));
278
+ range(1, 100 ).map(|x| println!("{}", x));
279
279
```
280
280
281
281
If you are trying to execute a closure on an iterator for its side effects,
@@ -287,7 +287,7 @@ has no side effect on the original iterator. Let's try it out with our infinite
287
287
iterator from before, ` count() ` :
288
288
289
289
``` {rust}
290
- for i in std::iter::count(1i, 5i ).take(5) {
290
+ for i in std::iter::count(1, 5 ).take(5) {
291
291
println!("{}", i);
292
292
}
293
293
```
@@ -307,7 +307,7 @@ returns `true` or `false`. The new iterator `filter()` produces
307
307
only the elements that that closure returns ` true ` for:
308
308
309
309
``` {rust}
310
- for i in range(1i, 100i ).filter(|&x| x % 2 == 0) {
310
+ for i in range(1, 100 ).filter(|&x| x % 2 == 0) {
311
311
println!("{}", i);
312
312
}
313
313
```
@@ -322,11 +322,11 @@ You can chain all three things together: start with an iterator, adapt it
322
322
a few times, and then consume the result. Check it out:
323
323
324
324
``` {rust}
325
- range(1i, 1000i )
325
+ range(1, 1000 )
326
326
.filter(|&x| x % 2 == 0)
327
327
.filter(|&x| x % 3 == 0)
328
328
.take(5)
329
- .collect::<Vec<int >>();
329
+ .collect::<Vec<i32 >>();
330
330
```
331
331
332
332
This will give you a vector containing ` 6 ` , ` 12 ` , ` 18 ` , ` 24 ` , and ` 30 ` .
0 commit comments