58

I can not find a way to collect the values of a HashMap into a Vec in the documentation. I have score_table: HashMap<Id, Score> and I want to get all the Scores into all_scores: Vec<Score>.

I was tempted to use the values method (all_scores = score_table.values()), but it does not work since values is not a Vec.

I know that Values implements the ExactSizeIterator trait, but I do not know how to collect all values of an iterator into a vector without manually writing a for loop and pushing the values in the vector one after one.

I also tried to use std::iter::FromIterator; but ended with something like:

all_scores = Vec::from_iter(score_table.values());
expected type `std::vec::Vec<Score>`
   found type `std::vec::Vec<&Score>`

Thanks to Hash map macro refuses to type-check, failing with a misleading (and seemingly buggy) error message?, I changed it to:

all_scores = Vec::from_iter(score_table.values().cloned());

and it does not produce errors to cargo check.

Is this a good way to do it?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Hugo Trentesaux
  • 1,584
  • 1
  • 16
  • 30
  • 2
    If you want to collect, why don't you use [`collect`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect)? – mcarton Jun 23 '19 at 13:10
  • 2
    _"Is this a good way to do it?"_ — One of the things about Rust (and C/C++) is that you need to think about ownership of your data. Data is moved by default, which changes the owner. If you don't want the owner to change then you can pass a reference, or you can clone the original data. Which way is "good" depends on your use case: If the hashmap is the owner of the data, the perhaps use references everywhere else (ie `Vec<&Score>`). But cloning is usually the right thing to do, when the types in question are small. – Peter Hall Jun 23 '19 at 13:12

4 Answers4

59

The method Iterator.collect is designed for this specific task. You're right in that you need .cloned() if you want a vector of actual values instead of references (unless the stored type implements Copy, like primitives), so the code looks like this:

all_scores = score_table.values().cloned().collect();

Internally, collect() just uses FromIterator, but it also infers the type of the output. Sometimes there isn't enough information to infer the type, so you may need to explicitly specify the type you want, like so:

all_scores = score_table.values().cloned().collect::<Vec<Score>>();
apetranzilla
  • 5,331
  • 27
  • 34
25

If you don't need score_table anymore, you can transfer the ownership of Score values to all_scores by:

let all_scores: Vec<Score> = score_table.into_iter()
                                        .map(|(_id, score)| score)
                                        .collect();

This approach will be faster and consume less memory than the clone approach by @apetranzilla. It also supports any struct, not only structs that implement Clone.

Daniel
  • 1,358
  • 11
  • 15
17

There are three useful methods on HashMaps, which all return iterators:

  • values() borrows the collection and returns references (&T).
  • values_mut() gives mutable references &mut T which is useful to modify elements of the collection without destroying score_table.
  • into_values() gives you the elements directly: T! The iterator takes ownership of all the elements. This means that score_table no longer owns them, so you can't use score_table anymore!

In your example, you call values() to get &T references, then convert them to owned values T via a clone(), which creates potentially expensive copies.

Instead, if we have an iterator of owned values, then we can convert it to a Vec using Iterator::collect():

let all_scores: Vec<Score> = score_table.into_values().collect();

Sometimes, you may need to specify the collecting type:

let all_scores = score_table.into_values().collect::<Vec<Score>>();
xy2
  • 6,040
  • 3
  • 14
  • 34
-2

tldr:

all_values = score_table.into_values().collect();

with clone:

all_values = score_table.values().cloned().collect();
German Khokhlov
  • 1,724
  • 16
  • 15