Trait std::iter::DoubleEndedIterator [] [src]

pub trait DoubleEndedIterator: Iterator {
    fn next_back(&mut self) -> Option<Self::Item>;
}

An iterator able to yield elements from both ends.

Something that implements DoubleEndedIterator has one extra capability over something that implements Iterator: the ability to also take Items from the back, as well as the front.

It is important to note that both back and forth work on the same range, and do not cross: iteration is over when they meet in the middle.

In a similar fashion to the Iterator protocol, once a DoubleEndedIterator returns None from a next_back(), calling it again may or may not ever return Some again. next() and next_back() are interchangable for this purpose.

Examples

Basic usage:

fn main() { let numbers = vec![1, 2, 3]; let mut iter = numbers.iter(); assert_eq!(Some(&1), iter.next()); assert_eq!(Some(&3), iter.next_back()); assert_eq!(Some(&2), iter.next_back()); assert_eq!(None, iter.next()); assert_eq!(None, iter.next_back()); }
let numbers = vec![1, 2, 3];

let mut iter = numbers.iter();

assert_eq!(Some(&1), iter.next());
assert_eq!(Some(&3), iter.next_back());
assert_eq!(Some(&2), iter.next_back());
assert_eq!(None, iter.next());
assert_eq!(None, iter.next_back());

Required Methods

fn next_back(&mut self) -> Option<Self::Item>

An iterator able to yield elements from both ends.

As this is the only method for this trait, the trait-level docs contain more details.

Examples

Basic usage:

fn main() { let numbers = vec![1, 2, 3]; let mut iter = numbers.iter(); assert_eq!(Some(&1), iter.next()); assert_eq!(Some(&3), iter.next_back()); assert_eq!(Some(&2), iter.next_back()); assert_eq!(None, iter.next()); assert_eq!(None, iter.next_back()); }
let numbers = vec![1, 2, 3];

let mut iter = numbers.iter();

assert_eq!(Some(&1), iter.next());
assert_eq!(Some(&3), iter.next_back());
assert_eq!(Some(&2), iter.next_back());
assert_eq!(None, iter.next());
assert_eq!(None, iter.next_back());

Implementors