I can not find a way to collect the values of a HashMap
into a Vec
in the documentation. I have score_table: HashMap
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::>();