W3cubDocs

/Rust

Struct std::iter::Map

#[must_use = "iterators are lazy and do nothing unless consumed"]pub struct Map<I, F> { /* fields omitted */ }

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> = vec![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 vec!['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 vec!['a', 'b', 'c'].into_iter()
                               .map(|letter| { c += 1; (letter, c) })
                               .rev() {
    println!("{:?}", pair);
}

Trait Implementations

impl<I, F> Clone for Map<I, F> where
    F: Clone,
    I: Clone
[src]

impl<I, F> Debug for Map<I, F> where
    I: Debug
[src]1.9.0

impl<B, I, F> DoubleEndedIterator for Map<I, F> where
    F: FnMut(<I as Iterator>::Item) -> B,
    I: DoubleEndedIterator
[src]

impl<B, I, F> ExactSizeIterator for Map<I, F> where
    F: FnMut(<I as Iterator>::Item) -> B,
    I: ExactSizeIterator
[src]

impl<B, I, F> FusedIterator for Map<I, F> where
    F: FnMut(<I as Iterator>::Item) -> B,
    I: FusedIterator
[src]1.26.0

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

type Item = B

The type of the elements being iterated over.

impl<B, I, F> TrustedLen for Map<I, F> where
    F: FnMut(<I as Iterator>::Item) -> B,
    I: TrustedLen
[src]

Auto Trait Implementations

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

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

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

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

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

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/iter/struct.Map.html