count_by

Function count_by 

Source
pub fn count_by<T, K, F>(items: &[T], key_of: F) -> HashMap<K, usize>
where K: Hash + Eq, F: Fn(&T) -> K,
Expand description

按指定键函数统计元素出现次数/Count elements by key function

§参数/Arguments

  • items - 要统计的数组/The array to count
  • key_of - 生成统计键的函数/Function to generate key

§返回值/Returns

HashMap<K, usize> 键为统计键,值为出现次数/Key is the grouping key, value is count

§示例/Examples

use slice_reducer::count_by;
let words = ["hello", "world"];
let counts = count_by(&words, |s| s.len());
assert_eq!(counts[&5], 2);