dict

Macro dict 

Source
macro_rules! dict {
    ( $container: expr, {}) => { ... };
    ( $container: expr, { $( $k:expr => $v:expr ),+ } ) => { ... };
    ( $container: expr, { $( $k:expr => $v:expr ),+, } ) => { ... };
}
Expand description

A generic syntax for dict-like structures.

Works for HashMap but also for e.g. serde_json or serde_yaml maps.


use std::collections::HashMap;

let empty: HashMap<u8, u8> = dict!(HashMap::new(), {});
assert_eq!(empty.len(), 0);

let map: HashMap<u8, u8> = dict!(HashMap::new(), {
   0 => 255,
   1 => 254,
   2 => 253,
});
assert_eq!(map.len(), 3);
assert!(matches!(map.get(&0), Some(255)));
assert!(matches!(map.get(&1), Some(254)));
assert!(matches!(map.get(&2), Some(253)));