@@ -5,7 +5,7 @@ Let's talk about loops.
55Remember Rust's ` for ` loop? Here's an example:
66
77``` {rust}
8- for x in range(0, 10) {
8+ for x in 0..10 {
99 println!("{}", x);
1010}
1111```
@@ -17,7 +17,7 @@ call the `.next()` method on repeatedly, and it gives us a sequence of things.
1717Like this:
1818
1919``` {rust}
20- let mut range = range(0, 10) ;
20+ let mut range = 0..10 ;
2121
2222loop {
2323 match range.next() {
@@ -52,7 +52,7 @@ a vector, you may be tempted to write this:
5252``` {rust}
5353let nums = vec![1, 2, 3];
5454
55- for i in range(0, nums.len() ) {
55+ for i in 0.. nums.len() {
5656 println!("{}", nums[i]);
5757}
5858```
@@ -118,7 +118,7 @@ The most common consumer is `collect()`. This code doesn't quite compile,
118118but it shows the intention:
119119
120120``` {rust,ignore}
121- let one_to_one_hundred = range(1, 101 ).collect();
121+ let one_to_one_hundred = (1..101i32 ).collect();
122122```
123123
124124As 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.
128128Here's the version that does compile:
129129
130130``` {rust}
131- let one_to_one_hundred = range(1, 101 ).collect::<Vec<i32>>();
131+ let one_to_one_hundred = (1..101i32 ).collect::<Vec<i32>>();
132132```
133133
134134If 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.
138138is one:
139139
140140``` {rust}
141- let greater_than_forty_two = range(0, 100 )
141+ let greater_than_forty_two = (0..100i32 )
142142 .find(|x| *x > 42);
143143
144144match greater_than_forty_two {
@@ -155,7 +155,7 @@ element, `find` returns an `Option` rather than the element itself.
155155Another important consumer is ` fold ` . Here's what it looks like:
156156
157157``` {rust}
158- let sum = range(1, 4)
158+ let sum = (1.. 4)
159159 .fold(0, |sum, x| sum + x);
160160```
161161
@@ -179,7 +179,7 @@ in this iterator:
179179We called ` fold() ` with these arguments:
180180
181181``` {rust}
182- # range(1, 4)
182+ # (1.. 4)
183183.fold(0, |sum, x| sum + x);
184184```
185185
@@ -210,20 +210,20 @@ This code, for example, does not actually generate the numbers
210210` 1-100 ` , and just creates a value that represents the sequence:
211211
212212``` {rust}
213- let nums = range(1, 100) ;
213+ let nums = 1.. 100;
214214```
215215
216216Since we didn't do anything with the range, it didn't generate the sequence.
217217Let's add the consumer:
218218
219219``` {rust}
220- let nums = range(1, 100).collect::<Vec<i32>>();
220+ let nums = (1.. 100).collect::<Vec<i32>>();
221221```
222222
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
224224it will do the work of generating the sequence.
225225
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() ` ,
227227which you've used before. ` iter() ` can turn a vector into a simple iterator
228228that gives you each element in turn:
229229
@@ -256,7 +256,7 @@ we need to talk about with regards to iterators. Let's get to it!
256256a new iterator. The simplest one is called ` map ` :
257257
258258``` {rust,ignore}
259- range(1, 100 ).map(|x| x + 1);
259+ (1..100i32 ).map(|x| x + 1);
260260```
261261
262262` 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:
267267``` {notrust,ignore}
268268warning: unused result which must be used: iterator adaptors are lazy and
269269 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);
271271 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
272272```
273273
274274Laziness strikes again! That closure will never execute. This example
275275doesn't print any numbers:
276276
277277``` {rust,ignore}
278- range(1, 100).map(|x| println!("{}", x));
278+ (1.. 100).map(|x| println!("{}", x));
279279```
280280
281281If 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
307307only the elements that that closure returns ` true ` for:
308308
309309``` {rust}
310- for i in range(1, 100 ).filter(|&x| x % 2 == 0) {
310+ for i in (1..100i32 ).filter(|&x| x % 2 == 0) {
311311 println!("{}", i);
312312}
313313```
@@ -322,7 +322,7 @@ You can chain all three things together: start with an iterator, adapt it
322322a few times, and then consume the result. Check it out:
323323
324324``` {rust}
325- range(1, 1000 )
325+ (1..1000i32 )
326326 .filter(|&x| x % 2 == 0)
327327 .filter(|&x| x % 3 == 0)
328328 .take(5)
0 commit comments