]> git.lizzy.rs Git - rust.git/commitdiff
expand on double-ended iterators in the tutorial
authorDaniel Micay <danielmicay@gmail.com>
Tue, 23 Jul 2013 18:37:06 +0000 (14:37 -0400)
committerDaniel Micay <danielmicay@gmail.com>
Wed, 24 Jul 2013 13:45:20 +0000 (09:45 -0400)
doc/tutorial-container.md

index 2146b76a4afbd13ee3d1801318db28c181e3afc4..ada55de805fa1a636b8553893cac8f6ac0516b03 100644 (file)
@@ -192,7 +192,7 @@ let mut it = xs.iter().zip(ys.iter());
 
 // print out the pairs of elements up to (&3, &"baz")
 for it.advance |(x, y)| {
-    println(fmt!("%d %s", *x, *y));
+    printfln!("%d %s", *x, *y);
 
     if *x == 3 {
         break;
@@ -200,7 +200,7 @@ for it.advance |(x, y)| {
 }
 
 // yield and print the last pair from the iterator
-println(fmt!("last: %?", it.next()));
+printfln!("last: %?", it.next());
 
 // the iterator is now fully consumed
 assert!(it.next().is_none());
@@ -294,15 +294,31 @@ another `DoubleEndedIterator` with `next` and `next_back` exchanged.
 ~~~
 let xs = [1, 2, 3, 4, 5, 6];
 let mut it = xs.iter();
-println(fmt!("%?", it.next())); // prints `Some(&1)`
-println(fmt!("%?", it.next())); // prints `Some(&2)`
-println(fmt!("%?", it.next_back())); // prints `Some(&6)`
+printfln!("%?", it.next()); // prints `Some(&1)`
+printfln!("%?", it.next()); // prints `Some(&2)`
+printfln!("%?", it.next_back()); // prints `Some(&6)`
 
 // prints `5`, `4` and `3`
 for it.invert().advance |&x| {
-    println(fmt!("%?", x))
+    printfln!("%?", x)
 }
 ~~~
 
 The `rev_iter` and `mut_rev_iter` methods on vectors just return an inverted
 version of the standard immutable and mutable vector iterators.
+
+The `chain_`, `transform`, `filter`, `filter_map` and `peek` adaptors are
+`DoubleEndedIterator` implementations if the underlying iterators are.
+
+~~~
+let xs = [1, 2, 3, 4];
+let ys = [5, 6, 7, 8];
+let mut it = xs.iter().chain_(ys.iter()).transform(|&x| x * 2);
+
+printfln!("%?", it.next()); // prints `Some(2)`
+
+// prints `16`, `14`, `12`, `10`, `8`, `6`, `4`
+for it.invert().advance |x| {
+    printfln!("%?", x);
+}
+~~~