Struct Map

struct Map<I, F> { ... }

An iterator that maps the values of iter with f.

This struct is created by the map method on Iterator. See its documentation for more.

Notes about side effects

The map iterator implements DoubleEndedIterator, meaning that you can also map backwards:

let v: Vec<i32> = [1, 2, 3].into_iter().map(|x| x + 1).rev().collect();

assert_eq!(v, [4, 3, 2]);

But if your closure has state, iterating backwards may act in a way you do not expect. Let's go through an example. First, in the forward direction:

let mut c = 0;

for pair in ['a', 'b', 'c'].into_iter()
                               .map(|letter| { c += 1; (letter, c) }) {
    println!("{pair:?}");
}

This will print ('a', 1), ('b', 2), ('c', 3).

Now consider this twist where we add a call to rev. This version will print ('c', 1), ('b', 2), ('a', 3). Note that the letters are reversed, but the values of the counter still go in order. This is because map() is still being called lazily on each item, but we are popping items off the back of the vector now, instead of shifting them from the front.

let mut c = 0;

for pair in ['a', 'b', 'c'].into_iter()
                               .map(|letter| { c += 1; (letter, c) })
                               .rev() {
    println!("{pair:?}");
}

Implementations

impl<B, I, F> TrustedLen for Map<I, F>

impl<B, I: DoubleEndedIterator, F> DoubleEndedIterator for Map<I, F>

fn next_back(self: &mut Self) -> Option<B>
fn try_rfold<Acc, G, R>(self: &mut Self, init: Acc, g: G) -> R
where
    Self: Sized,
    G: FnMut(Acc, <Self as >::Item) -> R,
    R: Try<Output = Acc>
fn rfold<Acc, G>(self: Self, init: Acc, g: G) -> Acc
where
    G: FnMut(Acc, <Self as >::Item) -> Acc

impl<B, I: ExactSizeIterator, F> ExactSizeIterator for Map<I, F>

fn len(self: &Self) -> usize
fn is_empty(self: &Self) -> bool

impl<B, I: FusedIterator, F> FusedIterator for Map<I, F>

impl<B, I: Iterator, F> Iterator for Map<I, F>

fn next(self: &mut Self) -> Option<B>
fn size_hint(self: &Self) -> (usize, Option<usize>)
fn try_fold<Acc, G, R>(self: &mut Self, init: Acc, g: G) -> R
where
    Self: Sized,
    G: FnMut(Acc, <Self as >::Item) -> R,
    R: Try<Output = Acc>
fn fold<Acc, G>(self: Self, init: Acc, g: G) -> Acc
where
    G: FnMut(Acc, <Self as >::Item) -> Acc

impl<I> IntoIterator for Map<I, F>

fn into_iter(self: Self) -> I

impl<I, F> Freeze for Map<I, F>

impl<I, F> RefUnwindSafe for Map<I, F>

impl<I, F> Send for Map<I, F>

impl<I, F> Sync for Map<I, F>

impl<I, F> Unpin for Map<I, F>

impl<I, F> UnsafeUnpin for Map<I, F>

impl<I, F> UnwindSafe for Map<I, F>

impl<I: $crate::clone::Clone, F: $crate::clone::Clone> Clone for Map<I, F>

fn clone(self: &Self) -> Map<I, F>

impl<I: fmt::Debug, F> Debug for Map<I, F>

fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result

impl<T> Any for Map<I, F>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Map<I, F>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Map<I, F>

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> CloneToUninit for Map<I, F>

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for Map<I, F>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into for Map<I, F>

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for Map<I, F>

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for Map<I, F>

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>