Exerpad

Word Count

Count how many times each word appears in the sentence "the cat and the dog". Print each word and its count in alphabetical order.

Expected output:

text
and: 1
cat: 1
dog: 1
the: 2

Hints:

  • Split the string with .split_whitespace()
  • Use .entry(word).or_insert(0) and then *count += 1 to count each word
  • Collect the keys into a vector, sort it, then loop through to print
rust
fn main() {
}