pub struct Map<I, F> { /* private fields */ }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:
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.
Trait Implementations§
1.0.0 · Source§impl<B, I: Iterator, F> Iterator for Map<I, F>
impl<B, I: Iterator, F> Iterator for Map<I, F>
Source§fn size_hint(&self) -> (usize, Option<usize>)
fn size_hint(&self) -> (usize, Option<usize>)
Source§fn try_fold<Acc, G, R>(&mut self, init: Acc, g: G) -> R
fn try_fold<Acc, G, R>(&mut self, init: Acc, g: G) -> R
Source§fn fold<Acc, G>(self, init: Acc, g: G) -> Acc
fn fold<Acc, G>(self, init: Acc, g: G) -> Acc
Source§fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>>
fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>>
iter_advance_by #77404)n elements. Read more1.0.0 · Source§fn nth(&mut self, n: usize) -> Option<Self::Item>
fn nth(&mut self, n: usize) -> Option<Self::Item>
nth element of the iterator. Read more1.28.0 · Source§fn step_by(self, step: usize) -> StepBy<Self>where
Self: Sized,
fn step_by(self, step: usize) -> StepBy<Self>where
Self: Sized,
1.0.0 · Source§fn zip<U>(self, other: U) -> Zip<Self, U::IntoIter>where
Self: Sized,
U: IntoIterator,
fn zip<U>(self, other: U) -> Zip<Self, U::IntoIter>where
Self: Sized,
U: IntoIterator,
1.0.0 · Source§fn map<B, F>(self, f: F) -> Map<Self, F>
fn map<B, F>(self, f: F) -> Map<Self, F>
1.0.0 · Source§fn filter<P>(self, predicate: P) -> Filter<Self, P>
fn filter<P>(self, predicate: P) -> Filter<Self, P>
1.0.0 · Source§fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
1.0.0 · Source§fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
n elements. Read more1.0.0 · Source§fn take(self, n: usize) -> Take<Self>where
Self: Sized,
fn take(self, n: usize) -> Take<Self>where
Self: Sized,
n elements, or fewer
if the underlying iterator ends sooner. Read more