@@ -567,11 +567,6 @@ loop {
567
567
This code prints out a weird sequence of numbers and stops as soon as
568
568
it finds one that can be divided by five.
569
569
570
- Rust also has a ` for ` construct. It's different from C's ` for ` and it works
571
- best when iterating over collections. See the section on [ closures] ( #closures )
572
- to find out how to use ` for ` and higher-order functions for enumerating
573
- elements of a collection.
574
-
575
570
# Data structures
576
571
577
572
## Structs
@@ -1397,8 +1392,8 @@ assert!(crayons.len() == 3);
1397
1392
assert!(!crayons.is_empty());
1398
1393
1399
1394
// Iterate over a vector, obtaining a pointer to each element
1400
- // (`for` is explained in the next section )
1401
- foreach crayon in crayons.iter() {
1395
+ // (`for` is explained in the container/iterator tutorial )
1396
+ for crayon in crayons.iter() {
1402
1397
let delicious_crayon_wax = unwrap_crayon(*crayon);
1403
1398
eat_crayon_wax(delicious_crayon_wax);
1404
1399
}
@@ -1749,7 +1744,7 @@ of `vector`:
1749
1744
~~~~
1750
1745
fn map<T, U>(vector: &[T], function: &fn(v: &T) -> U) -> ~[U] {
1751
1746
let mut accumulator = ~[];
1752
- foreach element in vector.iter() {
1747
+ for element in vector.iter() {
1753
1748
accumulator.push(function(element));
1754
1749
}
1755
1750
return accumulator;
@@ -2027,7 +2022,7 @@ generic types.
2027
2022
~~~~
2028
2023
# trait Printable { fn print(&self); }
2029
2024
fn print_all<T: Printable>(printable_things: ~[T]) {
2030
- foreach thing in printable_things.iter() {
2025
+ for thing in printable_things.iter() {
2031
2026
thing.print();
2032
2027
}
2033
2028
}
@@ -2073,7 +2068,7 @@ However, consider this function:
2073
2068
trait Drawable { fn draw(&self); }
2074
2069
2075
2070
fn draw_all<T: Drawable>(shapes: ~[T]) {
2076
- foreach shape in shapes.iter() { shape.draw(); }
2071
+ for shape in shapes.iter() { shape.draw(); }
2077
2072
}
2078
2073
# let c: Circle = new_circle();
2079
2074
# draw_all(~[c]);
@@ -2088,7 +2083,7 @@ an _object_.
2088
2083
~~~~
2089
2084
# trait Drawable { fn draw(&self); }
2090
2085
fn draw_all(shapes: &[@Drawable]) {
2091
- foreach shape in shapes.iter() { shape.draw(); }
2086
+ for shape in shapes.iter() { shape.draw(); }
2092
2087
}
2093
2088
~~~~
2094
2089
0 commit comments