macro_rules! hash {
    () => { ... };
    ($(map)? of $key_type:ty => $value_type:ty) => { ... };
    ($($key:expr => $value:expr),* $(,)?) => { ... };
    ($(map)? of $key_type:ty => $value_type:ty { $($key:expr => $value:expr),* $(,)? }) => { ... };
    ($(set)? of $value_type:ty) => { ... };
    ($($value:expr),* $(,)?) => { ... };
    ($(set)? of $value_type:ty { $($value:expr),* $(,)? }) => { ... };
}
Expand description

Macro for initializing both HashMaps and HashSets. It can infer both type of collection and types of entries but you can provide explicit type annotations.

use std::collections::{HashMap, HashSet};
use collection_literals::hash;

let set = hash! { set of &str { "Hi", "Hoi" } };
let map = hash! { map of u8 => char {
    0 => '0',
    1 => '1',
    2 => '2',
}};

assert_eq!(set, HashSet::from(["Hi", "Hoi"]));
assert_eq!(map, HashMap::from([(0, '0'), (1, '1'), (2, '2')]));


assert_eq!(hash! { 88, 99 }, hash! { set of i32 { 88, 99 } });
assert_eq!(hash! { 88 => 99 }, hash! { map of i32 => i32 { 88 => 99 } });