Skip to content

Commit 8c02272

Browse files
committed
expand on double-ended iterators in the tutorial
1 parent 626bb5a commit 8c02272

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

doc/tutorial-container.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,15 @@ let mut it = xs.iter().zip(ys.iter());
192192

193193
// print out the pairs of elements up to (&3, &"baz")
194194
for it.advance |(x, y)| {
195-
println(fmt!("%d %s", *x, *y));
195+
printfln!("%d %s", *x, *y);
196196

197197
if *x == 3 {
198198
break;
199199
}
200200
}
201201

202202
// yield and print the last pair from the iterator
203-
println(fmt!("last: %?", it.next()));
203+
printfln!("last: %?", it.next());
204204

205205
// the iterator is now fully consumed
206206
assert!(it.next().is_none());
@@ -294,15 +294,31 @@ another `DoubleEndedIterator` with `next` and `next_back` exchanged.
294294
~~~
295295
let xs = [1, 2, 3, 4, 5, 6];
296296
let mut it = xs.iter();
297-
println(fmt!("%?", it.next())); // prints `Some(&1)`
298-
println(fmt!("%?", it.next())); // prints `Some(&2)`
299-
println(fmt!("%?", it.next_back())); // prints `Some(&6)`
297+
printfln!("%?", it.next()); // prints `Some(&1)`
298+
printfln!("%?", it.next()); // prints `Some(&2)`
299+
printfln!("%?", it.next_back()); // prints `Some(&6)`
300300

301301
// prints `5`, `4` and `3`
302302
for it.invert().advance |&x| {
303-
println(fmt!("%?", x))
303+
printfln!("%?", x)
304304
}
305305
~~~
306306
307307
The `rev_iter` and `mut_rev_iter` methods on vectors just return an inverted
308308
version of the standard immutable and mutable vector iterators.
309+
310+
The `chain_`, `transform`, `filter`, `filter_map` and `peek` adaptors are
311+
`DoubleEndedIterator` implementations if the underlying iterators are.
312+
313+
~~~
314+
let xs = [1, 2, 3, 4];
315+
let ys = [5, 6, 7, 8];
316+
let mut it = xs.iter().chain_(ys.iter()).transform(|&x| x * 2);
317+
318+
printfln!("%?", it.next()); // prints `Some(2)`
319+
320+
// prints `16`, `14`, `12`, `10`, `8`, `6`, `4`
321+
for it.invert().advance |x| {
322+
printfln!("%?", x);
323+
}
324+
~~~

0 commit comments

Comments
 (0)