Protocol std::iter::DoubleEndedIterator
Iterators that can be consumed from both ends.
Implementing a double-ended iterator
Iterators that can be iterated from both ends efficiently should implement this protocol. next_back is the method that is used to iterate in reverse order. The convention is that next and next_back "meet in the middle", so that each element is visited once regardless of the direction of iteration.
All types implementing DoubleEndedIterator should also mix in DoubleEndedIteratorExt, which provides relevant extension methods, notably rev, which reverses the direction of iteration.
Example
struct Range { lower: i32, upper: i32 }
impl Range {
use std::iter::{Iterator, IteratorExt, DoubleEndedIterator, DoubleEndedIteratorExt};
fn next(self: &mut Range) -> Option<i32> {
if self.lower < self.upper {
let result = self.lower;
self.lower += 1;
Option::some(result)
} else {
Option::none()
}
}
fn next_back(self: &mut Range) -> Option<i32> {
if self.lower < self.upper {
self.upper -= 1;
Option::some(self.upper)
} else {
Option::none()
}
}
mixin Iterator<Range, i32>;
mixin IteratorExt<Range, i32>;
mixin DoubleEndedIterator<Range, i32>;
mixin DoubleEndedIteratorExt<Range, i32>;
}
Run this example
Required methods
-
Returns the next item from the back, if any.
If the iterator has reached the beginning, it returns none.
See DoubleEndedIterator for more information.