title: How can I convert a collection of values into a HashMap that counts them?
body: <p>I have a collection of items, with repetitions, and I want to create a <code>HashMap</code> that has the items as keys, and the count of how many times they appear in the original collection.</p>
  
  <p>The specific example I'm working on is a string where I want to count how many times each character appears.</p>
  
  <p>I can do something like this:</p>
  
  <pre><code>fn into_character_map(word: &amp;str) -&gt; HashMap&lt;char, i32&gt; {
      let mut characters = HashMap::new();
  
      for c in word.chars() {
          let entry = characters.entry(c).or_insert(0);
          *entry += 1;
      }
  
      characters
  }
  </code></pre>
  
  <p>But I was wondering if there's a more elegant solution. I was thinking of using <code>collect()</code>, but it doesn't maintain state between items, so doesn't seem to support what I need.</p>
  
  <p>This came up as I was writing my solution to the <a href="http://exercism.io/languages/rust" rel="noreferrer">'Anagram' problem on Exercism</a>.</p>