[][src]Macro velcro::hash_map_from

hash_map_from!() { /* proc-macro */ }

An initializer for HashMap that works the same as hash_map! except that values can be of any type that can be converted into the collection's item type via an Into implementation.

The type of the item must be known at compile time, and usually this means an explicit type annotation is required.

Usage

use velcro::hash_map_from;

#[derive(Debug, PartialEq, Eq, Hash)]
struct Foo(u64);

impl From<u64> for Foo {
    fn from(other: u64) -> Self {
        Foo(other)
    }
}

let mut map1 = HashMap::new();
map1.insert('a', Foo(0));
map1.insert('b', Foo(1));
map1.insert('c', Foo(1));
map1.insert('d', Foo(1));
map1.insert('e', Foo(1));
map1.insert('f', Foo(2));

let map2: HashMap<char, Foo> = hash_map_from! {
    'a': 0,
    ..('b'..='e'): 1,
    'f': 2
};

assert_eq!(map1, map2);