std-method

Iterator::map

Transform each requested item into a new value, possibly of a different type. Expected stdout (not executed): [200, 500, 800] Common mistake: Using map only for side effects without consuming it.

Formal Syntax & Structure

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

Working Example

Example Snippetrust
fn main() {    let cents: Vec<_> = [2, 5, 8].into_iter().map(|dollars| dollars * 100).collect();    println!("{cents:?}");}
Rust Reference