Trait core::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(); let n = iter.next(); assert_eq!(Some(&1), n); let n = iter.next_back(); assert_eq!(Some(&3), n); let n = iter.next_back(); assert_eq!(Some(&2), n); let n = iter.next(); assert_eq!(None, n); let n = iter.next_back(); assert_eq!(None, n); }
let numbers = vec![1, 2, 3];

let mut iter = numbers.iter();

let n = iter.next();
assert_eq!(Some(&1), n);

let n = iter.next_back();
assert_eq!(Some(&3), n);

let n = iter.next_back();
assert_eq!(Some(&2), n);

let n = iter.next();
assert_eq!(None, n);

let n = iter.next_back();
assert_eq!(None, n);

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(); let n = iter.next(); assert_eq!(Some(&1), n); let n = iter.next_back(); assert_eq!(Some(&3), n); let n = iter.next_back(); assert_eq!(Some(&2), n); let n = iter.next(); assert_eq!(None, n); let n = iter.next_back(); assert_eq!(None, n); }
let numbers = vec![1, 2, 3];

let mut iter = numbers.iter();

let n = iter.next();
assert_eq!(Some(&1), n);

let n = iter.next_back();
assert_eq!(Some(&3), n);

let n = iter.next_back();
assert_eq!(Some(&2), n);

let n = iter.next();
assert_eq!(None, n);

let n = iter.next_back();
assert_eq!(None, n);

Implementors