Crate multimap

source ·
Expand description

A map implementation which allows storing multiple values per key.

The interface is roughly based on std::collections::HashMap, but is changed and extended to accomodate the multi-value use case. In fact, MultiMap is implemented mostly as a thin wrapper around std::collections::HashMap and stores its values as a std::Vec per key.

Values are guaranteed to be in insertion order as long as not manually changed. Keys are not ordered. Multiple idential key-value-pairs can exist in the MultiMap. A key can exist in the MultiMap with no associated value.


use multimap::MultiMap;

// create a new MultiMap. An explicit type signature can be omitted because of the
// type inference.
let mut queries = MultiMap::new();

// insert some queries.
queries.insert("urls", "");
queries.insert("urls", "");
queries.insert("urls", "");
queries.insert("id", "42");
queries.insert("name", "roger");

// check if there's any urls.
println!("Are there any urls in the multimap? {:?}.",
    if queries.contains_key("urls") {"Yes"} else {"No"} );

// get the first item in a key's vector.
assert_eq!(queries.get("urls"), Some(&""));

// get all the urls.
    Some(&vec!["", "", ""]));

// iterate over all keys and the first value in the key's vector.
for (key, value) in queries.iter() {
    println!("key: {:?}, val: {:?}", key, value);

// iterate over all keys and the key's vector.
for (key, values) in queries.iter_all() {
    println!("key: {:?}, values: {:?}", key, values);

// the different methods for getting value(s) from the multimap.
let mut map = MultiMap::new();

map.insert("key1", 42);
map.insert("key1", 1337);

assert_eq!(map["key1"], 42);
assert_eq!(map.get("key1"), Some(&42));
assert_eq!(map.get_vec("key1"), Some(&vec![42, 1337]));


  • Serde trait implementations for MultiMap


  • Create a MultiMap from a list of key value pairs



  • A view into a single location in a map, which may be vacant or occupied.