@@ -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(0i, 10i ) {
8+ for x in range(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(0i, 10i );
20+ let mut range = range(0, 10 );
2121
2222loop {
2323 match range.next() {
3232We make a mutable binding to the return value of ` range ` , which is our iterator.
3333We then ` loop ` , with an inner ` match ` . This ` match ` is used on the result of
3434` 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
3737out, and if we get ` None ` , we ` break ` out of the loop.
3838
3939This 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
5050a vector, you may be tempted to write this:
5151
5252``` {rust}
53- let nums = vec![1i, 2i, 3i ];
53+ let nums = vec![1, 2, 3 ];
5454
55- for i in range(0u , nums.len()) {
55+ for i in range(0 , nums.len()) {
5656 println!("{}", nums[i]);
5757}
5858```
@@ -62,7 +62,7 @@ vectors returns an iterator which iterates through a reference to each element
6262of the vector in turn. So write this:
6363
6464``` {rust}
65- let nums = vec![1i, 2i, 3i ];
65+ let nums = vec![1, 2, 3 ];
6666
6767for num in nums.iter() {
6868 println!("{}", num);
@@ -79,12 +79,12 @@ very common with iterators: we can ignore unnecessary bounds checks, but still
7979know that we're safe.
8080
8181There'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
8484see it. This code works fine too:
8585
8686``` {rust}
87- let nums = vec![1i, 2i, 3i ];
87+ let nums = vec![1, 2, 3 ];
8888
8989for num in nums.iter() {
9090 println!("{}", *num);
@@ -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(1i, 101i ).collect();
121+ let one_to_one_hundred = range(1, 101 ).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(1i, 101i ).collect::<Vec<int >>();
131+ let one_to_one_hundred = range(1, 101 ).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(0i, 100i )
141+ let greater_than_forty_two = range(0, 100 )
142142 .find(|x| *x > 42);
143143
144144match greater_than_forty_two {
@@ -155,8 +155,8 @@ 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(1i, 4i )
159- .fold(0i , |sum, x| sum + x);
158+ let sum = range(1, 4 )
159+ .fold(0 , |sum, x| sum + x);
160160```
161161
162162` fold() ` is a consumer that looks like this:
@@ -172,24 +172,24 @@ in this iterator:
172172
173173| base | accumulator | element | closure result |
174174| ------| -------------| ---------| ----------------|
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 |
178178
179179We called ` fold() ` with these arguments:
180180
181181``` {rust}
182- # range(1i, 4i )
183- .fold(0i , |sum, x| sum + x);
182+ # range(1, 4 )
183+ .fold(0 , |sum, x| sum + x);
184184```
185185
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
189189iteration, 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
191191the 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
193193result for our sum. ` 1 + 2 + 3 = 6 ` , and that's the result we got.
194194
195195Whew. ` 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
210210` 1-100 ` , and just creates a value that represents the sequence:
211211
212212``` {rust}
213- let nums = range(1i, 100i );
213+ let nums = range(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(1i, 100i ).collect::<Vec<int >>();
220+ let nums = range(1, 100 ).collect::<Vec<i32 >>();
221221```
222222
223223Now, ` 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
228228that gives you each element in turn:
229229
230230``` {rust}
231- let nums = [1i, 2i, 3i ];
231+ let nums = [1, 2, 3 ];
232232
233233for num in nums.iter() {
234234 println!("{}", num);
@@ -239,12 +239,12 @@ These two basic iterators should serve you well. There are some more
239239advanced iterators, including ones that are infinite. Like ` count ` :
240240
241241``` {rust}
242- std::iter::count(1i, 5i );
242+ std::iter::count(1, 5 );
243243```
244244
245245This iterator counts up from one, adding five each time. It will give
246246you 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,
248248that's okay! You probably don't want to use ` collect() ` on it, though...
249249
250250That'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!
256256a new iterator. The simplest one is called ` map ` :
257257
258258``` {rust,ignore}
259- range(1i, 100i ).map(|x| x + 1i );
259+ range(1, 100 ).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(1i, 100i ).map(|x| x + 1i );
270+ range(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(1i, 100i ).map(|x| println!("{}", x));
278+ range(1, 100 ).map(|x| println!("{}", x));
279279```
280280
281281If 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
287287iterator from before, ` count() ` :
288288
289289``` {rust}
290- for i in std::iter::count(1i, 5i ).take(5) {
290+ for i in std::iter::count(1, 5 ).take(5) {
291291 println!("{}", i);
292292}
293293```
@@ -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(1i, 100i ).filter(|&x| x % 2 == 0) {
310+ for i in range(1, 100 ).filter(|&x| x % 2 == 0) {
311311 println!("{}", i);
312312}
313313```
@@ -322,11 +322,11 @@ 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(1i, 1000i )
325+ range(1, 1000 )
326326 .filter(|&x| x % 2 == 0)
327327 .filter(|&x| x % 3 == 0)
328328 .take(5)
329- .collect::<Vec<int >>();
329+ .collect::<Vec<i32 >>();
330330```
331331
332332This will give you a vector containing ` 6 ` , ` 12 ` , ` 18 ` , ` 24 ` , and ` 30 ` .
0 commit comments