111

Although vectors are best suited for procedural programming, I would like to use a map function on them. The following snippet works:

fn map<A, B>(u: &Vec<A>, f: &Fn(&A) -> B) -> Vec<B> {
    let mut res: Vec<B> = Vec::with_capacity(u.len());
    for x in u.iter() {
        res.push(f(x));
    }
    res
}

fn f(x: &i32) -> i32 {
    *x + 1
}

fn main() {
    let u = vec![1, 2, 3];
    let v = map(&u, &f);
    println!("{} {} {}", v[0], v[1], v[2]);
}

Why isn't there any such function in the standard library (and also in std::collections::LinkedList)? Is there another way to deal with it?

nbro
  • 15,395
  • 32
  • 113
  • 196
user19018
  • 2,199
  • 5
  • 16
  • 19

2 Answers2

172

Rust likes to be more general than that; mapping is done over iterators, rather than over solely vectors or slices.

A couple of demonstrations:

let u = vec![1, 2, 3];
let v: Vec<_> = u.iter().map(f).collect();
let u = vec![1, 2, 3];
let v = u.iter().map(|&x| x + 1).collect::<Vec<_>>();

.collect() is probably the most magic part of it, and allows you to collect all the elements of the iterator into a large variety of different types, as shown by the implementors of FromIterator. For example, an iterator of Ts can be collected to Vec<T>, of chars can be collected to a String, of (K, V) pairs to a HashMap<K, V>, and so forth.

This way of working with iterators also means that you often won’t even need to create intermediate vectors where in other languages or with other techniques you would; this is more efficient and typically just as natural.

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
24

As pointed out by bluss, you can also use the mutable iterator to mutate the value in place, without changing the type:

let mut nums = nums;
for num in &mut nums { *num += 1 }
println!("{:p} - {:?}", &nums, nums);

The function Vec::map_in_place was deprecated in Rust 1.3 and is no longer present in Rust 1.4.

Chris Morgan's answer is the best solution 99% of the time. However, there is a specialized function called Vec::map_in_place. This has the benefit of not requiring any additional memory allocations, but it requires that the input and output type are the same size (thanks Levans) and is currently unstable:

fn map_in_place<U, F>(self, f: F) -> Vec<U> 
    where F: FnMut(T) -> U

An example:

#![feature(collections)]

fn main() {
    let nums = vec![1,2,3];
    println!("{:p} - {:?}", &nums, nums);

    let nums = nums.map_in_place(|v| v + 1);
    println!("{:p} - {:?}", &nums, nums);
}
Community
  • 1
  • 1
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • 4
    If you don't need map_in_place's magical type change, you can just use the mutable iterator. `for elt in &mut v { *elt = *elt + 1; }` – bluss May 04 '15 at 15:37
  • 2
    Note that it also require the input and output types to be of the same size, which is certainly not always the case. – Levans May 04 '15 at 16:09
  • 4
    Note that `map_in_place` has been deprecated since 1.3. I guess we use `.into_iter().map(…).collect()` now? – kennytm Mar 19 '16 at 13:46
  • 3
    Anyone know why `Vec::map_in_place` was deprecated? The docs say "unclear that the API is strong enough and did not proven itself" but I don't really know what that's supposed to mean... – Addison Jul 31 '19 at 18:27
  • 3
    Found through https://github.com/rust-lang/rust/issues/75087 , https://github.com/rust-lang/rust/pull/70793 adds optimisations for into_iter()…collect() chains; see the bench_in_place benchmarks in https://github.com/rust-lang/rust/blob/master/library/alloc/benches/vec.rs . – Tobu Oct 18 '21 at 11:09
  • Perhaps `nums.iter_mut().for_each(|v| *v = *v + 1)` – Tom Leys Nov 05 '21 at 19:36