Map

Struct Map 

1.6.0 · Source
pub struct Map<I, F> { /* private fields */ }
Available on crate feature ferrocene_certified only.
Expand description

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:?}");
}

Trait Implementations§

1.0.0 · Source§

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

Source§

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

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)
where Self:,

Performs copy-assignment from source. Read more
1.0.0 · Source§

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

Source§

type Item = B

The type of the elements being iterated over.
Source§

fn next(&mut self) -> Option<B>

Advances the iterator and returns the next value. Read more
Source§

fn size_hint(&self) -> (usize, Option<usize>)

Returns the bounds on the remaining length of the iterator. Read more
1.0.0 · Source§

fn map<B, F>(self, f: F) -> Map<Self, F>
where Self: Sized, F: FnMut(Self::Item) -> B,

Takes a closure and creates an iterator which calls that closure on each element. Read more
1.0.0 · Source§

fn cloned<'a, T>(self) -> Cloned<Self>
where T: Clone + 'a, Self: Sized + Iterator<Item = &'a T>,

Creates an iterator which clones all of its elements. Read more
Source§

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

Auto Trait Implementations§

§

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

§

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,

Blanket Implementations§

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

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

Source§

type Item = <I as Iterator>::Item

The type of the elements being iterated over.
Source§

type IntoIter = I

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> I

Creates an iterator from a value. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.