Trait DoubleEndedIterator
trait DoubleEndedIterator: Iterator
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 interchangeable for this purpose.
Examples
Basic usage:
let numbers = vec!;
let mut iter = numbers.iter;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Required Methods
fn next_back(self: &mut Self) -> Option<<Self as >::Item>Removes and returns an element from the end of the iterator.
Returns
Nonewhen there are no more elements.The trait-level docs contain more details.
Examples
Basic usage:
let numbers = vec!; let mut iter = numbers.iter; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;Remarks
The elements yielded by
DoubleEndedIterator's methods may differ from the ones yielded byIterator's methods:let vec = vec!; let uniq_by_fst_comp = ; assert_eq!; assert_eq!; assert_eq!; assert_eq!;
Provided Methods
fn advance_back_by(self: &mut Self, n: usize) -> Result<(), NonZero<usize>>Advances the iterator from the back by
nelements.advance_back_byis the reverse version ofadvance_by. This method will eagerly skipnelements starting from the back by callingnext_backup tontimes untilNoneis encountered.advance_back_by(n)will returnOk(())if the iterator successfully advances bynelements, or aErr(NonZero<usize>)with valuekifNoneis encountered, wherekis remaining number of steps that could not be advanced because the iterator ran out. Ifselfis empty andnis non-zero, then this returnsErr(n). Otherwise,kis always less thann.Calling
advance_back_by(0)can do meaningful work, for exampleFlattencan advance its outer iterator until it finds an inner iterator that is not empty, which then often allows it to return a more accuratesize_hint()than in its initial state.Examples
Basic usage:
use NonZero; let a = ; let mut iter = a.iter; assert_eq!; assert_eq!; assert_eq!; assert_eq!; // only `&3` was skippedfn nth_back(self: &mut Self, n: usize) -> Option<<Self as >::Item>Returns the
nth element from the end of the iterator.This is essentially the reversed version of [
Iterator::nth()]. Although like most indexing operations, the count starts from zero, sonth_back(0)returns the first value from the end,nth_back(1)the second, and so on.Note that all elements between the end and the returned element will be consumed, including the returned element. This also means that calling
nth_back(0)multiple times on the same iterator will return different elements.nth_back()will returnNoneifnis greater than or equal to the length of the iterator.Examples
Basic usage:
let a = ; assert_eq!;Calling
nth_back()multiple times doesn't rewind the iterator:let a = ; let mut iter = a.iter; assert_eq!; assert_eq!;Returning
Noneif there are less thann + 1elements:let a = ; assert_eq!;fn try_rfold<B, F, R>(self: &mut Self, init: B, f: F) -> R where Self: Sized, F: FnMut(B, <Self as >::Item) -> R, R: Try<Output = B>This is the reverse version of [
Iterator::try_fold()]: it takes elements starting from the back of the iterator.Examples
Basic usage:
let a = ; let sum = a.iter .map .try_rfold; assert_eq!;Short-circuiting:
let a = ; let mut it = a.iter; let sum = it .by_ref .map .try_rfold; assert!; // Because it short-circuited, the remaining elements are still // available through the iterator. assert_eq!;fn rfold<B, F>(self: Self, init: B, f: F) -> B where Self: Sized, F: FnMut(B, <Self as >::Item) -> BAn iterator method that reduces the iterator's elements to a single, final value, starting from the back.
This is the reverse version of [
Iterator::fold()]: it takes elements starting from the back of the iterator.rfold()takes two arguments: an initial value, and a closure with two arguments: an 'accumulator', and an element. The closure returns the value that the accumulator should have for the next iteration.The initial value is the value the accumulator will have on the first call.
After applying this closure to every element of the iterator,
rfold()returns the accumulator.This operation is sometimes called 'reduce' or 'inject'.
Folding is useful whenever you have a collection of something, and want to produce a single value from it.
Note:
rfold()combines elements in a right-associative fashion. For associative operators like+, the order the elements are combined in is not important, but for non-associative operators like-the order will affect the final result. For a left-associative version ofrfold(), see [Iterator::fold()].Examples
Basic usage:
let a = ; // the sum of all of the elements of a let sum = a.iter .rfold; assert_eq!;This example demonstrates the right-associative nature of
rfold(): it builds a string, starting with an initial value and continuing with each element from the back until the front:let numbers = ; let zero = "0".to_string; let result = numbers.iter.rfold; assert_eq!;fn rfind<P>(self: &mut Self, predicate: P) -> Option<<Self as >::Item> where Self: Sized, P: FnMut(&<Self as >::Item) -> boolSearches for an element of an iterator from the back that satisfies a predicate.
rfind()takes a closure that returnstrueorfalse. It applies this closure to each element of the iterator, starting at the end, and if any of them returntrue, thenrfind()returnsSome(element). If they all returnfalse, it returnsNone.rfind()is short-circuiting; in other words, it will stop processing as soon as the closure returnstrue.Because
rfind()takes a reference, and many iterators iterate over references, this leads to a possibly confusing situation where the argument is a double reference. You can see this effect in the examples below, with&&x.Examples
Basic usage:
let a = ; assert_eq!; assert_eq!;Iterating over references:
let a = ; // `iter()` yields references i.e. `&i32` and `rfind()` takes a // reference to each element. assert_eq!; assert_eq!;Stopping at the first
true:let a = ; let mut iter = a.iter; assert_eq!; // we can still use `iter`, as there are more elements. assert_eq!;
Implementors
impl<'a, T, N: usize> DoubleEndedIterator for ArrayWindows<'a, T, N>impl<T, N: usize> DoubleEndedIterator for IntoIter<T, N>impl<'a, T, P> DoubleEndedIterator for SplitInclusive<'a, T, P>impl<'a, T: 'a, P> DoubleEndedIterator for ChunkBy<'a, T, P>impl<'a, I, T> DoubleEndedIterator for Cloned<I>impl<'a, T> DoubleEndedIterator for Chunks<'a, T>impl<T: Clone> DoubleEndedIterator for Take<Repeat<T>>impl<'a, P> DoubleEndedIterator for RMatches<'a, P>impl<'a> DoubleEndedIterator for EscapeAscii<'a>impl DoubleEndedIterator for ToLowercaseimpl<'a, T, P> DoubleEndedIterator for SplitMut<'a, T, P>impl<'a, P> DoubleEndedIterator for RSplit<'a, P>impl<'a, T: 'a, P> DoubleEndedIterator for ChunkByMut<'a, T, P>impl<I: DoubleEndedIterator, F> DoubleEndedIterator for Inspect<I, F>impl<A: Step> DoubleEndedIterator for RangeIter<A>impl<'a, T> DoubleEndedIterator for RChunks<'a, T>impl<I, U> DoubleEndedIterator for Flatten<I>impl<I: DoubleEndedIterator> DoubleEndedIterator for ByRefSized<'_, I>impl<'a> DoubleEndedIterator for Lines<'a>impl<A, B> DoubleEndedIterator for Zip<A, B>impl<B, I: DoubleEndedIterator, F> DoubleEndedIterator for Map<I, F>impl<I: DoubleEndedIterator, P> DoubleEndedIterator for Filter<I, P>impl<A: Step> DoubleEndedIterator for RangeInclusive<A>impl<'a, I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for &'a mut Iimpl<'a, T> DoubleEndedIterator for ChunksMut<'a, T>impl<'a, T, P> DoubleEndedIterator for SplitInclusiveMut<'a, T, P>impl<'a, P> DoubleEndedIterator for SplitTerminator<'a, P>impl<A: Step> DoubleEndedIterator for Range<A>impl<'a> DoubleEndedIterator for Chars<'a>impl<A: Clone> DoubleEndedIterator for Repeat<A>impl<'a> DoubleEndedIterator for LinesAny<'a>impl<I> DoubleEndedIterator for Take<I>impl<'a, T> DoubleEndedIterator for Iter<'a, T>impl<'a, T> DoubleEndedIterator for RChunksMut<'a, T>impl<I> DoubleEndedIterator for Skip<I>impl<'a, A> DoubleEndedIterator for Iter<'a, A>impl<B, I: DoubleEndedIterator, F> DoubleEndedIterator for FilterMap<I, F>impl<'a, T, P> DoubleEndedIterator for RSplit<'a, T, P>impl<'a, P> DoubleEndedIterator for RSplitTerminator<'a, P>impl<'a> DoubleEndedIterator for CharIndices<'a>impl<A: Step> DoubleEndedIterator for RangeInclusiveIter<A>impl<'a, T> DoubleEndedIterator for ChunksExact<'a, T>impl<'a> DoubleEndedIterator for SplitWhitespace<'a>impl<'a, A> DoubleEndedIterator for IterMut<'a, A>impl DoubleEndedIterator for EscapeDefaultimpl<'a, T, P> DoubleEndedIterator for RSplitMut<'a, T, P>impl<'a, P> DoubleEndedIterator for MatchIndices<'a, P>impl<'a, T> DoubleEndedIterator for Iter<'a, T>impl<'a, T> DoubleEndedIterator for RChunksExact<'a, T>impl<'a> DoubleEndedIterator for SplitAsciiWhitespace<'a>impl<I> DoubleEndedIterator for Rev<I>impl<I> DoubleEndedIterator for Enumerate<I>impl DoubleEndedIterator for ToUppercaseimpl<'a, T> DoubleEndedIterator for IterMut<'a, T>impl<A> DoubleEndedIterator for IntoIter<A>impl<'a, T> DoubleEndedIterator for ChunksExactMut<'a, T>impl<T> DoubleEndedIterator for Empty<T>impl<A: Clone> DoubleEndedIterator for RepeatN<A>impl<'a, P> DoubleEndedIterator for RMatchIndices<'a, P>impl<A, F: FnOnce() -> A> DoubleEndedIterator for OnceWith<F>impl<I: DoubleEndedIterator, U, F> DoubleEndedIterator for FlatMap<I, U, F>impl DoubleEndedIterator for Bytes<'_>impl<I, N: usize> DoubleEndedIterator for ArrayChunks<I, N>impl<'a, T> DoubleEndedIterator for IterMut<'a, T>impl<T> DoubleEndedIterator for Once<T>impl<I> DoubleEndedIterator for StepBy<I>impl<'a, P: Pattern<Searcher<'a>: DoubleEndedSearcher<'a>>> DoubleEndedIterator for SplitInclusive<'a, P>impl<'a, T> DoubleEndedIterator for Windows<'a, T>impl<'a, I, T> DoubleEndedIterator for Copied<I>impl<I> DoubleEndedIterator for Fuse<I>impl<'a, T, P> DoubleEndedIterator for Split<'a, T, P>impl<'a, T> DoubleEndedIterator for RChunksExactMut<'a, T>impl<I> DoubleEndedIterator for Peekable<I>impl<A: DoubleEndedIterator> DoubleEndedIterator for OptionFlatten<A>impl<'a, P> DoubleEndedIterator for Matches<'a, P>impl<A, B> DoubleEndedIterator for Chain<A, B>impl<'a, P> DoubleEndedIterator for Split<'a, P>impl<T> DoubleEndedIterator for IntoIter<T>