diff options
| author | Daniel Micay <danielmicay@gmail.com> | 2013-07-23 17:14:32 -0400 |
|---|---|---|
| committer | Daniel Micay <danielmicay@gmail.com> | 2013-07-24 09:45:20 -0400 |
| commit | af5a17b7d0788438bb6b39cccfc96a0b13e00250 (patch) | |
| tree | 62c1956ba56f77eba72ab1b44fec7a1167c5dc78 | |
| parent | 8c0227251299982eaab1c8ebebc76bd856efd38f (diff) | |
| download | rust-af5a17b7d0788438bb6b39cccfc96a0b13e00250.tar.gz rust-af5a17b7d0788438bb6b39cccfc96a0b13e00250.zip | |
document random-access iterators
| -rw-r--r-- | doc/tutorial-container.md | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/doc/tutorial-container.md b/doc/tutorial-container.md index ada55de805f..1b195e99979 100644 --- a/doc/tutorial-container.md +++ b/doc/tutorial-container.md @@ -322,3 +322,31 @@ for it.invert().advance |x| { printfln!("%?", x); } ~~~ + +## Random-access iterators + +The `RandomAccessIterator` trait represents an iterator offering random access +to the whole range. The `indexable` method retrieves the number of elements +accessible with the `idx` method. + +The `chain_` adaptor is an implementation of `RandomAccessIterator` if the +underlying iterators are. + +~~~ +let xs = [1, 2, 3, 4, 5]; +let ys = ~[7, 9, 11]; +let mut it = xs.iter().chain_(ys.iter()); +printfln!("%?", it.idx(0)); // prints `Some(&1)` +printfln!("%?", it.idx(5)); // prints `Some(&7)` +printfln!("%?", it.idx(7)); // prints `Some(&11)` +printfln!("%?", it.idx(8)); // prints `None` + +// yield two elements from the beginning, and one from the end +it.next(); +it.next(); +it.next_back(); + +printfln!("%?", it.idx(0)); // prints `Some(&3)` +printfln!("%?", it.idx(4)); // prints `Some(&9)` +printfln!("%?", it.idx(6)); // prints `None` +~~~ |
