snippetrustModerate
Sort HashMap data by value
Viewed 0 times
datahashmapsortvalue
Problem
I want to sort HashMap data by value in Rust (e.g., when counting character frequency in a string).
The Python equivalent of what I’m trying to do is:
My corresponding Rust code looks like this:
Is this idiomatic Rust? Can I construct the
The Python equivalent of what I’m trying to do is:
count = {}
for c in text:
count[c] = count.get('c', 0) + 1
sorted_data = sorted(count.items(), key=lambda item: -item[1])
print('Most frequent character in text:', sorted_data[0][0])My corresponding Rust code looks like this:
// Count the frequency of each letter
let mut count: HashMap = HashMap::new();
for c in text.to_lowercase().chars() {
*count.entry(c).or_insert(0) += 1;
}
// Get a sorted (by field 0 ("count") in reversed order) list of the
// most frequently used characters:
let mut count_vec: Vec = count.iter().collect();
count_vec.sort_by(|a, b| b.1.cmp(a.1));
println!("Most frequent character in text: {}", count_vec[0].0);Is this idiomatic Rust? Can I construct the
count_vec in a way so that it would consume the HashMaps data and owns it (e.g., using map())? Would this be more idomatic?Solution
Is this idiomatic Rust?
There's nothing particularly unidiomatic, except possibly for the unnecessary full type constraint on
It's not difficult from context to work out what the full type of
The other borderline change you could make if you feel like it would be to use
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
There's nothing particularly unidiomatic, except possibly for the unnecessary full type constraint on
count_vec; you could just uselet 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.Context
Stack Overflow Q#34555837, score: 42
Revisions (0)
No revisions yet.