Reverse an iterator's direction.
The rev() adapter reverses an iterator's direction, yielding items from the end to the beginning. It is only available on iterators that implement DoubleEndedIterator, which includes most standard collection iterators such as those from Vec, slice, VecDeque, range, and string slices.
Like most iterator adapters, rev() is lazy and zero-cost: it simply swaps which end of the underlying iterator is consumed on each call to next(). It does not allocate new memory or copy data.
This is the idiomatic way to iterate backwards in Rust, replacing the C-style reverse for loop. Combine with chain(), zip(), enumerate(), and other adapters as needed; note that calling rev() on already-reversed iterators returns the original direction.
let v = vec![1, 2, 3];
for x in v.iter().rev() {
print!("{} ", x);
}
// 3 2 1
let collected: Vec<_> = (0..5).rev().collect();
// vec![4, 3, 2, 1, 0]