Skip to main content

indexmap_with_default

Macro indexmap_with_default 

Source
macro_rules! indexmap_with_default {
    () => { ... };
    ($H:ty $(;)?) => { ... };
    ($H:ty; $($key:expr => $value:expr),+ $(,)?) => { ... };
}
Expand description

Create an IndexMap from a list of key-value pairs and a BuildHasherDefault-wrapped custom hasher.

ยงExample

use indexmap::indexmap_with_default;
use fnv::FnvHasher;

let map = indexmap_with_default!{
    FnvHasher;
    "a" => 1,
    "b" => 2,
};
assert_eq!(map["a"], 1);
assert_eq!(map["b"], 2);
assert_eq!(map.get("c"), None);

// "a" is the first key
assert_eq!(map.keys().next(), Some(&"a"));

This can also be initialized in const contexts:

use indexmap::{IndexMap, indexmap_with_default};
use fnv::FnvBuildHasher; // = BuildHasherDefault<FnvHasher>
use std::sync::Mutex;

static GLOBAL: Mutex<IndexMap<String, i32, FnvBuildHasher>> =
    Mutex::new(indexmap_with_default!());

if let Ok(mut map) = GLOBAL.lock() {
    map.insert("a".into(), 1);
    map.insert("b".into(), 2);
}

assert_eq!(GLOBAL.lock().unwrap()["a"], 1);
assert_eq!(GLOBAL.lock().unwrap()["b"], 2);