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.

Examples

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", "http://rust-lang.org");
queries.insert("urls", "http://mozilla.org");
queries.insert("urls", "http://wikipedia.org");
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(&"http://rust-lang.org"));

// get all the urls.
assert_eq!(queries.get_vec("urls"),
    Some(&vec!["http://rust-lang.org", "http://mozilla.org", "http://wikipedia.org"]));

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

Modules

  • Serde trait implementations for MultiMap

Macros

  • Create a MultiMap from a list of key value pairs

Structs

Enums

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