Crate multimap [] [src]

A MultiMap implementation which is just a wrapper around std::collections::HashMap. See HashMap's documentation for more details.

Some of the methods are just thin wrappers, some methods does change a little semantics and some methods are new (doesn't have an equivalent in HashMap.)

The MultiMap is generic for the key (K) and the value (V). Internally the values are stored in a generic Vector.


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]));



An iterator over the entries of a HashMap.


A mutable iterator over the entries of a HashMap.


A view into a single occupied location in a MultiMap.


A view into a single empty location in a MultiMap.



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