Remember Rust's `for` loop? Here's an example:
```{rust}
-for x in range(0, 10) {
+for x in 0..10 {
println!("{}", x);
}
```
Like this:
```{rust}
-let mut range = range(0, 10);
+let mut range = 0..10;
loop {
match range.next() {
```{rust}
let nums = vec![1, 2, 3];
-for i in range(0, nums.len()) {
+for i in 0..nums.len() {
println!("{}", nums[i]);
}
```
but it shows the intention:
```{rust,ignore}
-let one_to_one_hundred = range(1, 101).collect();
+let one_to_one_hundred = (1..101i32).collect();
```
As you can see, we call `collect()` on our iterator. `collect()` takes
Here's the version that does compile:
```{rust}
-let one_to_one_hundred = range(1, 101).collect::<Vec<i32>>();
+let one_to_one_hundred = (1..101i32).collect::<Vec<i32>>();
```
If you remember, the `::<>` syntax allows us to give a type hint,
is one:
```{rust}
-let greater_than_forty_two = range(0, 100)
+let greater_than_forty_two = (0..100i32)
.find(|x| *x > 42);
match greater_than_forty_two {
Another important consumer is `fold`. Here's what it looks like:
```{rust}
-let sum = range(1, 4)
+let sum = (1..4)
.fold(0, |sum, x| sum + x);
```
We called `fold()` with these arguments:
```{rust}
-# range(1, 4)
+# (1..4)
.fold(0, |sum, x| sum + x);
```
`1-100`, and just creates a value that represents the sequence:
```{rust}
-let nums = range(1, 100);
+let nums = 1..100;
```
Since we didn't do anything with the range, it didn't generate the sequence.
Let's add the consumer:
```{rust}
-let nums = range(1, 100).collect::<Vec<i32>>();
+let nums = (1..100).collect::<Vec<i32>>();
```
-Now, `collect()` will require that `range()` give it some numbers, and so
+Now, `collect()` will require that the range gives it some numbers, and so
it will do the work of generating the sequence.
-`range` is one of two basic iterators that you'll see. The other is `iter()`,
+A range is one of two basic iterators that you'll see. The other is `iter()`,
which you've used before. `iter()` can turn a vector into a simple iterator
that gives you each element in turn:
a new iterator. The simplest one is called `map`:
```{rust,ignore}
-range(1, 100).map(|x| x + 1);
+(1..100i32).map(|x| x + 1);
```
`map` is called upon another iterator, and produces a new iterator where each
```{notrust,ignore}
warning: unused result which must be used: iterator adaptors are lazy and
do nothing unless consumed, #[warn(unused_must_use)] on by default
- range(1, 100).map(|x| x + 1);
+(1..100).map(|x| x + 1);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
doesn't print any numbers:
```{rust,ignore}
-range(1, 100).map(|x| println!("{}", x));
+(1..100).map(|x| println!("{}", x));
```
If you are trying to execute a closure on an iterator for its side effects,
only the elements that that closure returns `true` for:
```{rust}
-for i in range(1, 100).filter(|&x| x % 2 == 0) {
+for i in (1..100i32).filter(|&x| x % 2 == 0) {
println!("{}", i);
}
```
a few times, and then consume the result. Check it out:
```{rust}
-range(1, 1000)
+(1..1000i32)
.filter(|&x| x % 2 == 0)
.filter(|&x| x % 3 == 0)
.take(5)
#[bench]
fn bench_xor_1000_ints(b: &mut Bencher) {
b.iter(|| {
- range(0, 1000).fold(0, |old, new| old ^ new);
+ (0..1000).fold(0, |old, new| old ^ new);
});
}
```
# impl X { fn iter<T, F>(&self, _: F) where F: FnMut() -> T {} } let b = X;
b.iter(|| {
// note lack of `;` (could also use an explicit `return`).
- range(0, 1000).fold(0, |old, new| old ^ new)
+ (0..1000).fold(0, |old, new| old ^ new)
});
```
b.iter(|| {
let n = test::black_box(1000);
- range(0, n).fold(0, |a, b| a ^ b)
+ (0..n).fold(0, |a, b| a ^ b)
})
# }
```