map is lazy that can cause problems
examples/vectors/map1/src/main.rs
fn main() { let chars = vec!['a', 'b', 'c']; let mut c = 0; let pairs = chars.into_iter().map(|letter| { c += 1; (letter, c) }); for pair in pairs { println!("{pair:?}"); } }
('a', 1) ('b', 2) ('c', 3)
examples/vectors/map2/src/main.rs
fn main() { let chars = vec!['a', 'b', 'c']; let mut c = 0; let pairs = chars.into_iter().rev().map(|letter| { c += 1; (letter, c) }); for pair in pairs { println!("{pair:?}"); } }
('c', 1) ('b', 2) ('a', 3)
examples/vectors/map3/src/main.rs
// However this is also the same fn main() { let chars = vec!['a', 'b', 'c']; let mut c = 0; let pairs = chars .into_iter() .map(|letter| { c += 1; (letter, c) }) .rev(); for pair in pairs { println!("{pair:?}"); } }
('c', 1) ('b', 2) ('a', 3)