We have Iterator::map , so you can:
some_vec.iter().map(|x| )
However, Iterator lazy, so it will not do anything on its own. You can .collect() at the end to create a new vector with new elements, if that is what you want:
let some_vec = vec![1, 2, 3]; let doubled: Vec<_> = some_vec.iter().map(|x| x * 2).collect(); println!("{:?}", doubled);
The standard way to do something without highlighting is to use a for loop:
let some_vec = vec![1, 2, 3]; for i in &some_vec { println!("{}", i); }
or if you want to change the values ββin place:
let mut some_vec = vec![1, 2, 3]; for i in &mut some_vec { *i *= 2; } println!("{:?}", some_vec);
If you really want a functional style, you can use the foreach method from the itertools box.
source share