[][src]Macro col_macros::hash_map

macro_rules! hash_map {
    (for $map:expr, ins $( $k:expr => $v:expr ),*) => { ... };
    (for $map:tt, ins $( $k:tt => $v:tt ,)*) => { ... };
    ( $( $k:expr => $v:expr ),*) => { ... };
    ( $( $k:tt => $v:tt ,)*) => { ... };
}

Construct or update a map of type Map<K, V> which can be and is an alias to HashMap<K, V> in the types module.

It does so by simply creating an array from key-value tuples,set the capacity of the map at they length,read each tuple,insert the keys to values and mem::forget the array.

Maps with a capacity and the insert method are accepted,BTreeMap not.

Examples

use col_macros::hash_map;
use col_macros::types::Map;
 
let mut hash = hash_map!["key1" => 1, "key2" => 2];
let mut hash2 = {
    let mut temp = Map::with_capacity(2);
    temp.insert("key1", 1);
    temp.insert("key2", 2);
    temp
};
 
assert_eq!(hash["key1"], 1);
assert_eq!(hash["key2"], 2);
 
assert_eq!(hash2["key1"], 1);
assert_eq!(hash2["key2"], 2);
 
hash_map![for &mut hash, ins "key3" => 3, "key4" => 4];
 
assert_eq!(hash["key3"], 3);
assert_eq!(hash["key4"], 4);