Skip to content

Commit af5a17b

Browse files
committed
document random-access iterators
1 parent 8c02272 commit af5a17b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

doc/tutorial-container.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,3 +322,31 @@ for it.invert().advance |x| {
322322
printfln!("%?", x);
323323
}
324324
~~~
325+
326+
## Random-access iterators
327+
328+
The `RandomAccessIterator` trait represents an iterator offering random access
329+
to the whole range. The `indexable` method retrieves the number of elements
330+
accessible with the `idx` method.
331+
332+
The `chain_` adaptor is an implementation of `RandomAccessIterator` if the
333+
underlying iterators are.
334+
335+
~~~
336+
let xs = [1, 2, 3, 4, 5];
337+
let ys = ~[7, 9, 11];
338+
let mut it = xs.iter().chain_(ys.iter());
339+
printfln!("%?", it.idx(0)); // prints `Some(&1)`
340+
printfln!("%?", it.idx(5)); // prints `Some(&7)`
341+
printfln!("%?", it.idx(7)); // prints `Some(&11)`
342+
printfln!("%?", it.idx(8)); // prints `None`
343+
344+
// yield two elements from the beginning, and one from the end
345+
it.next();
346+
it.next();
347+
it.next_back();
348+
349+
printfln!("%?", it.idx(0)); // prints `Some(&3)`
350+
printfln!("%?", it.idx(4)); // prints `Some(&9)`
351+
printfln!("%?", it.idx(6)); // prints `None`
352+
~~~

0 commit comments

Comments
 (0)