Sort HashMap data by value

Is this idiomatic Rust?

There’s nothing particularly unidiomatic, except possibly for the unnecessary full type constraint on count_vec; you could just use

let mut count_vec: Vec<_> = count.iter().collect();

It’s not difficult from context to work out what the full type of count_vec is. You could also omit the type constraint for count entirely, but then you’d have to play shenanigans with your integer literals to have the correct value type inferred. That is to say, an explicit annotation is eminently reasonable in this case.

The other borderline change you could make if you feel like it would be to use |a, b| a.1.cmp(b.1).reverse() for the sort closure. The Ordering::reverse method just reverses the result so that less-than becomes greater-than, and vice versa. This makes it slightly more obvious that you meant what you wrote, as opposed to accidentally transposing two letters.

Can I construct the count_vec in a way so that it would consume the HashMaps data and owns it?

Not in any meaningful way. Just because HashMap is using memory doesn’t mean that memory is in any way compatible with Vec. You could use count.into_iter() to consume the HashMap and move the elements out (as opposed to iterating over pointers), but since both char and u32 are trivially copyable, this doesn’t really gain you anything.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)