@@ -1552,13 +1552,6 @@ fn each(v: &[int], op: &fn(v: &int)) {
15521552}
15531553~~~~
15541554
1555- As an aside, the reason we pass in a * pointer* to an integer rather
1556- than the integer itself is that this is how the actual ` each() `
1557- function for vectors works. ` vec::each ` though is a
1558- [ generic] ( #generics ) function, so must be efficient to use for all
1559- types. Passing the elements by pointer avoids copying potentially
1560- large objects.
1561-
15621555As a caller, if we use a closure to provide the final operator
15631556argument, we can write it in a way that has a pleasant, block-like
15641557structure.
@@ -1616,6 +1609,9 @@ To enable `debug!` logging, set the RUST_LOG environment variable to the name of
16161609
16171610## For loops
16181611
1612+ > *** Note:*** The closure-based protocol used ` for ` loop is on the way out. The ` for ` loop will
1613+ > use iterator objects in the future instead.
1614+
16191615The most common way to express iteration in Rust is with a ` for `
16201616loop. Like ` do ` , ` for ` is a nice syntax for describing control flow
16211617with closures. Additionally, within a ` for ` loop, ` break ` , ` loop ` ,
@@ -1640,7 +1636,16 @@ fn each(v: &[int], op: &fn(v: &int) -> bool) -> bool {
16401636And using this function to iterate over a vector:
16411637
16421638~~~~
1643- # use each = std::vec::each;
1639+ # fn each(v: &[int], op: &fn(v: &int) -> bool) -> bool {
1640+ # let mut n = 0;
1641+ # while n < v.len() {
1642+ # if !op(&v[n]) {
1643+ # return false;
1644+ # }
1645+ # n += 1;
1646+ # }
1647+ # return true;
1648+ # }
16441649each([2, 4, 8, 5, 16], |n| {
16451650 if *n % 2 != 0 {
16461651 println("found odd number!");
@@ -1656,7 +1661,16 @@ out of the loop, you just write `break`. To skip ahead
16561661to the next iteration, write ` loop ` .
16571662
16581663~~~~
1659- # use each = std::vec::each;
1664+ # fn each(v: &[int], op: &fn(v: &int) -> bool) -> bool {
1665+ # let mut n = 0;
1666+ # while n < v.len() {
1667+ # if !op(&v[n]) {
1668+ # return false;
1669+ # }
1670+ # n += 1;
1671+ # }
1672+ # return true;
1673+ # }
16601674for each([2, 4, 8, 5, 16]) |n| {
16611675 if *n % 2 != 0 {
16621676 println("found odd number!");
@@ -1671,7 +1685,16 @@ normally allowed in closures, in a block that appears as the body of a
16711685the enclosing function, not just the loop body.
16721686
16731687~~~~
1674- # use each = std::vec::each;
1688+ # fn each(v: &[int], op: &fn(v: &int) -> bool) -> bool {
1689+ # let mut n = 0;
1690+ # while n < v.len() {
1691+ # if !op(&v[n]) {
1692+ # return false;
1693+ # }
1694+ # n += 1;
1695+ # }
1696+ # return true;
1697+ # }
16751698fn contains(v: &[int], elt: int) -> bool {
16761699 for each(v) |x| {
16771700 if (*x == elt) { return true; }
@@ -1686,7 +1709,16 @@ In these situations it can be convenient to lean on Rust's
16861709argument patterns to bind ` x ` to the actual value, not the pointer.
16871710
16881711~~~~
1689- # use each = std::vec::each;
1712+ # fn each(v: &[int], op: &fn(v: &int) -> bool) -> bool {
1713+ # let mut n = 0;
1714+ # while n < v.len() {
1715+ # if !op(&v[n]) {
1716+ # return false;
1717+ # }
1718+ # n += 1;
1719+ # }
1720+ # return true;
1721+ # }
16901722# fn contains(v: &[int], elt: int) -> bool {
16911723 for each(v) |&x| {
16921724 if (x == elt) { return true; }
@@ -1841,10 +1873,9 @@ vector consisting of the result of applying `function` to each element
18411873of ` vector ` :
18421874
18431875~~~~
1844- # use std::vec;
18451876fn map<T, U>(vector: &[T], function: &fn(v: &T) -> U) -> ~[U] {
18461877 let mut accumulator = ~[];
1847- for vec::each( vector) |element| {
1878+ for vector.iter().advance |element| {
18481879 accumulator.push(function(element));
18491880 }
18501881 return accumulator;
0 commit comments