Struct Map

#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct Map<I, F> { pub(crate) iter: I, pub(in ::iter::adapters::map) f: 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:?}");
}

Fields

iter: I
f: F

Implementations

impl<I, F> Map<I, F>

const fn new(iter: I, f: F) -> Map<I, F>
fn into_inner(self) -> I

Trait Implementations

impl<B, I, F> TrustedLen for Map<I, F> where I: TrustedLen, F: FnMut(I::Item) -> B,

impl<B, I: DoubleEndedIterator, F> DoubleEndedIterator for Map<I, F> where F: FnMut(I::Item) -> B,

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

impl<B, I: ExactSizeIterator, F> ExactSizeIterator for Map<I, F> where F: FnMut(I::Item) -> B,

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

impl<B, I: FusedIterator, F> FusedIterator for Map<I, F> where F: FnMut(I::Item) -> B,

impl<B, I: Iterator, F> Iterator for Map<I, F> where F: FnMut(I::Item) -> B,

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

impl<I, F> SourceIter for Map<I, F> where I: SourceIter,

type Source = <I as SourceIter>::Source;
unsafe fn as_inner(&mut self) -> &mut I::Source

impl<I, F> TrustedRandomAccess for Map<I, F> where I: TrustedRandomAccess,

impl<I, F> TrustedRandomAccessNoCoerce for Map<I, F> where I: TrustedRandomAccessNoCoerce,

const MAY_HAVE_SIDE_EFFECT: bool = true;

impl<I: Clone, F: Clone> Clone for Map<I, F>

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

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

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

impl<I: InPlaceIterable, F> InPlaceIterable for Map<I, F>

const EXPAND_BY: Option<NonZero<usize>> = I::EXPAND_BY;
const MERGE_BY: Option<NonZero<usize>> = I::MERGE_BY;

impl<I: OneShot, F> OneShot for Map<I, F>

impl<I: TrustedFused, F> TrustedFused for Map<I, F>

Auto Trait Implementations

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

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

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

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

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

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

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

Blanket Implementations

impl<I> IntoIterator for Map<I, F> where I: Iterator,

type Item = <I as Iterator>::Item;
type IntoIter = I;
fn into_iter(self) -> I

impl<T> Any for Map<I, F> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Map<I, F> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Map<I, F> where T: ?Sized,

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

impl<T> CloneToUninit for Map<I, F> where T: Clone,

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

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

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> SizeHint for Map<I, F> where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>

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

impl<T, U> Into<U> for Map<I, F> where U: From<T>,

fn into(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<U> for Map<I, F> where U: Into<T>,

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

impl<T, U> TryInto<U> for Map<I, F> where U: TryFrom<T>,

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