@@ -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(0, 10) {
8
+ for x in 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(0, 10) ;
20
+ let mut range = 0..10 ;
21
21
22
22
loop {
23
23
match range.next() {
@@ -52,7 +52,7 @@ a vector, you may be tempted to write this:
52
52
``` {rust}
53
53
let nums = vec![1, 2, 3];
54
54
55
- for i in range(0, nums.len() ) {
55
+ for i in 0.. nums.len() {
56
56
println!("{}", nums[i]);
57
57
}
58
58
```
@@ -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(1, 101 ).collect();
121
+ let one_to_one_hundred = (1..101i32 ).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(1, 101 ).collect::<Vec<i32>>();
131
+ let one_to_one_hundred = (1..101i32 ).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(0, 100 )
141
+ let greater_than_forty_two = (0..100i32 )
142
142
.find(|x| *x > 42);
143
143
144
144
match greater_than_forty_two {
@@ -155,7 +155,7 @@ 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(1, 4)
158
+ let sum = (1.. 4)
159
159
.fold(0, |sum, x| sum + x);
160
160
```
161
161
@@ -179,7 +179,7 @@ in this iterator:
179
179
We called ` fold() ` with these arguments:
180
180
181
181
``` {rust}
182
- # range(1, 4)
182
+ # (1.. 4)
183
183
.fold(0, |sum, x| sum + x);
184
184
```
185
185
@@ -210,20 +210,20 @@ 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(1, 100) ;
213
+ let nums = 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(1, 100).collect::<Vec<i32>>();
220
+ let nums = (1.. 100).collect::<Vec<i32>>();
221
221
```
222
222
223
- Now, ` collect() ` will require that ` range() ` give it some numbers, and so
223
+ Now, ` collect() ` will require that the range gives it some numbers, and so
224
224
it will do the work of generating the sequence.
225
225
226
- ` range ` is one of two basic iterators that you'll see. The other is ` iter() ` ,
226
+ A range is one of two basic iterators that you'll see. The other is ` iter() ` ,
227
227
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
@@ -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(1, 100 ).map(|x| x + 1);
259
+ (1..100i32 ).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(1, 100).map(|x| x + 1);
270
+ (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(1, 100).map(|x| println!("{}", x));
278
+ (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,
@@ -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(1, 100 ).filter(|&x| x % 2 == 0) {
310
+ for i in (1..100i32 ).filter(|&x| x % 2 == 0) {
311
311
println!("{}", i);
312
312
}
313
313
```
@@ -322,7 +322,7 @@ 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(1, 1000 )
325
+ (1..1000i32 )
326
326
.filter(|&x| x % 2 == 0)
327
327
.filter(|&x| x % 3 == 0)
328
328
.take(5)
0 commit comments