Macro stry_common::hashmap[][src]

macro_rules! hashmap {
    (@single $($x:tt)*) => { ... };
    (@count $($rest:expr),*) => { ... };
    ( hasher = $hasher:expr, strict, data = { $( $key:expr => $value:expr, )+ } $( , )? ) => { ... };
    ( hasher = $hasher:expr, strict, data = { $( $key:expr => $value:expr ),* } $( , )? ) => { ... };
    ( strict, data = { $( $key:expr => $value:expr, )+ } $( , )? ) => { ... };
    ( strict, data = { $( $key:expr => $value:expr ),* } $( , )? ) => { ... };
    ( hasher = $hasher:expr, data = { $( $key:expr => $value:expr, )+ } $( , )? ) => { ... };
    ( hasher = $hasher:expr, data = { $( $key:expr => $value:expr ),* } $( , )? ) => { ... };
    ( $( $key:expr => $value:expr, )+ ) => { ... };
    ( $( $key:expr => $value:expr ),* ) => { ... };
    () => { ... };
}

Create a HashMap from a list of key-value pairs.

While based off of maplit, this is an extended version with the ability to set the hasher and enable a strict mode.

Examples

let map = fenn::hashmap! {
    "a" => 1,
    "b" => 2,
};

assert_eq!(map["a"], 1);
assert_eq!(map["b"], 2);
assert_eq!(map.get("c"), None);

When strict mode is active, a duplicate key will cause a panic.

let map = fenn::hashmap! {
    strict,
    data = {
        "a" => 1,
        "a" => 2, // panics
    }
};

To set the default hasher, pass in a hasher expression with the hasher, parameter.

use std::collections::hash_map::RandomState;

let map = fenn::hashmap! {
    hasher = RandomState::new(),
    data = {
        "a" => 1,
        "b" => 2,
    }
};

assert_eq!(map["a"], 1);
assert_eq!(map["b"], 2);
assert_eq!(map.get("c"), None);

A custom hasher and strict mode can be used at the same time, the only requirement is that the hasher comes first.

use std::collections::hash_map::RandomState;

let map = fenn::hashmap! {
    hasher = RandomState::new(),
    strict,
    data = {
        "a" => 1,
        "a" => 2, // panics
    }
};