Skip to content

Latest commit

 

History

History
28 lines (23 loc) · 1.04 KB

README.md

File metadata and controls

28 lines (23 loc) · 1.04 KB

aggregate-map

Easily collect a list of key-value pairs into a mapping of keys to collections of values in Rust.

If you have a set of data that you want to collect into a map, by default you'll only keep the last value in the data for that key. But what if you want instead to keep a collection of all the values for each key? Enter AggregateMap!

use std::collections::HashMap;
use aggregate_map::AggregateMap;
let data = [
    ("dog", "Terry"),
    ("dog", "Zamboni"),
    ("cat", "Jonathan"),
    ("dog", "Priscilla"),
];
let collected: AggregateMap<HashMap<_, Vec<_>>> = data.into_iter().collect();
let expected = HashMap::from([
    ("dog", vec!["Terry", "Zamboni", "Priscilla"]),
    ("cat", vec!["Jonathan"])
]);
assert_eq!(collected.into_inner(), expected);

See docs for more info and examples.