19 releases

0.5.7 Oct 12, 2022
0.5.6 Jul 16, 2022
0.5.5 May 5, 2022
0.5.3 Feb 7, 2022
0.1.0 Jan 4, 2017

#145 in Data structures

Download history 11357/week @ 2024-01-06 12010/week @ 2024-01-13 13819/week @ 2024-01-20 16542/week @ 2024-01-27 13792/week @ 2024-02-03 15039/week @ 2024-02-10 14181/week @ 2024-02-17 12226/week @ 2024-02-24 15342/week @ 2024-03-02 14166/week @ 2024-03-09 14938/week @ 2024-03-16 13275/week @ 2024-03-23 14450/week @ 2024-03-30 12750/week @ 2024-04-06 13316/week @ 2024-04-13 12992/week @ 2024-04-20

55,661 downloads per month
Used in 72 crates (18 directly)

MIT license

814 lines


Counter counts recurrent elements of iterables. It is based on the Python implementation.

The struct Counter is the entry-point type for this module.

Math Underpinnings

Mathematically, a Counter implements a hash-based version of a multiset, or bag. This is simply an extension of the notion of a set to the idea that we care not only about whether an entity exists within the set, but the number of occurrences within the set. Normal set operations such as intersection, union, etc. are of course still supported.


Just count an iterable

use counter::Counter;
let char_counts = "barefoot".chars().collect::<Counter<_>>();
let counts_counts = char_counts.values().collect::<Counter<_>>();

Update a count

let mut counts = "aaa".chars().collect::<Counter<_>>();
counts[&'a'] += 1;
counts[&'b'] += 1;
let mut counts = "able babble table babble rabble table able fable scrabble"
// add or subtract an iterable of the same type
counts += "cain and abel fable table cable".split_whitespace();
// or add or subtract from another Counter of the same type
let other_counts = "scrabble cabbie fable babble"
let difference = counts - other_counts;

Extend a Counter with another Counter:

let mut counter = "abbccc".chars().collect::<Counter<_>>();
let another = "bccddd".chars().collect::<Counter<_>>();
let expect = [('a', 1), ('b', 3), ('c', 5), ('d', 3)].iter()
    .cloned().collect::<HashMap<_, _>>();
assert_eq!(counter.into_map(), expect);

Get items with keys

let counts = "aaa".chars().collect::<Counter<_>>();
assert_eq!(counts[&'a'], 3);
assert_eq!(counts[&'b'], 0);

Get the most common items

most_common_ordered() uses the natural ordering of keys which are Ord.

let by_common = "eaddbbccc".chars().collect::<Counter<_>>().most_common_ordered();
let expected = vec![('c', 3), ('b', 2), ('d', 2), ('a', 1), ('e', 1)];
assert!(by_common == expected);

k_most_common_ordered() takes an argument k of type usize and returns the top k most common items. This is functionally equivalent to calling most_common_ordered() and then truncating the result to length k. However, if k is smaller than the length of the counter then k_most_common_ordered() can be more efficient, often much more so.

let by_common = "eaddbbccc".chars().collect::<Counter<_>>().k_most_common_ordered(2);
let expected = vec![('c', 3), ('b', 2)];
assert!(by_common == expected);

Get the most common items using your own ordering

For example, here we break ties reverse alphabetically.

let counter = "eaddbbccc".chars().collect::<Counter<_>>();
let by_common = counter.most_common_tiebreaker(|&a, &b| b.cmp(&a));
let expected = vec![('c', 3), ('d', 2), ('b', 2), ('e', 1), ('a', 1)];
assert!(by_common == expected);

Test counters against another

Counters are multi-sets and so can be sub- or supersets of each other.

A counter is a subset of another if for all its elements, the other counter has an equal or higher count. Test for this with is_subset():

let counter = "aaabb".chars().collect::<Counter<_>>();
let superset = "aaabbbc".chars().collect::<Counter<_>>();
let not_a_superset = "aaae".chars().collect::<Counter<_>>();

Testing for a superset is the inverse, is_superset() is true if the counter can contain another counter in its entirety:

let counter = "aaabbbc".chars().collect::<Counter<_>>();
let subset = "aabbb".chars().collect::<Counter<_>>();
let not_a_subset = "aaae".chars().collect::<Counter<_>>();

These relationships continue to work when using a signed integer type for the counter: all values in the subset must be equal or lower to the values in the superset. Negative values are interpreted as 'missing' those values, and the subset would need to miss those same elements, or be short more, to still be a subset:

let mut subset = "aaabb".chars().collect::<Counter<_, i8>>();
subset.insert('e', -2);  // short 2 'e's
subset.insert('f', -1);  // and 1 'f'
let mut superset = "aaaabbb".chars().collect::<Counter<_, i8>>();
superset.insert('e', -1);  // short 1 'e'

Counter intersection and union

You can intersect two counters, giving you the minimal counts of their combined elements using the & bitwise and operator, and produce their union with the maximum counts using | bitwise or:

let a = "aaabb".chars().collect::<Counter<_>>();
let b = "aabbbbe".chars().collect::<Counter<_>>();

let intersection = a & b;
let expected_intersection = "aabb".chars().collect::<Counter<_>>();
assert_eq!(intersection, expected_intersection);

let c = "aaabb".chars().collect::<Counter<_>>();
let d = "aabbbbe".chars().collect::<Counter<_>>();

let union = c | d;
let expected_union = "aaabbbbe".chars().collect::<Counter<_>>();
assert_eq!(union, expected_union)

The in-place &= and |= operations are also supported.

Treat it like a HashMap

Counter<T, N> implements Deref<Target=HashMap<T, N>> and DerefMut<Target=HashMap<T, N>>, which means that you can perform any operations on it which are valid for a HashMap.

let mut counter = "aa-bb-cc".chars().collect::<Counter<_>>();
assert!(counter == "aabbcc".chars().collect::<Counter<_>>());

Note that Counter<T, N> itself implements Index. Counter::index returns a reference to a Zero::zero value for missing keys.

let counter = "aaa".chars().collect::<Counter<_>>();
assert_eq!(counter[&'b'], 0);
// panics
// assert_eq!((*counter)[&'b'], 0);

Advanced Usage

Count any iterable which is Hash + Eq

You can't use the most_common* functions unless T is also Clone, but simple counting works fine on a minimal data type.

#[derive(Debug, Hash, PartialEq, Eq)]
struct Inty {
    i: usize,

impl Inty {
    pub fn new(i: usize) -> Inty {
        Inty { i: i }

// <https://en.wikipedia.org/wiki/867-5309/Jenny>
let intys = vec![

let inty_counts = intys.iter().collect::<Counter<_>>();
println!("{:?}", inty_counts);
// {Inty { i: 8 }: 2, Inty { i: 0 }: 3, Inty { i: 9 }: 1, Inty { i: 3 }: 1,
//  Inty { i: 7 }: 1, Inty { i: 6 }: 1, Inty { i: 5 }: 1}
assert!(inty_counts.get(&Inty { i: 8 }) == Some(&2));
assert!(inty_counts.get(&Inty { i: 0 }) == Some(&3));
assert!(inty_counts.get(&Inty { i: 6 }) == Some(&1));

Use your own type for the count

Sometimes usize just isn't enough. If you find yourself overflowing your machine's native size, you can use your own type. Here, we use an i8, but you can use most numeric types, including bignums, as necessary.

let counter: Counter<_, i8> = "abbccc".chars().collect();
let expected: HashMap<char, i8> = [('a', 1), ('b', 2), ('c', 3)].iter().cloned().collect();
assert!(counter.into_map() == expected);

License: MIT