Skip to content

Collect key-value iterators into a map with a collection of values at each key.

License

Notifications You must be signed in to change notification settings

junbl/aggregate-map

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Collect key-value iterators into a map with a collection of values at each key.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages