How I can create a HashMap literal in Rust? In Python I can do it so:
hashmap = {
\'element0\': {
\'name\': \'My New Element\',
\'childs\':
I recommend the maplit crate.
To quote from the documentation:
Macros for container literals with specific type.
use maplit::hashmap;
let map = hashmap!{
"a" => 1,
"b" => 2,
};
The maplit crate uses
=>syntax for the mapping macros. It is not possible to use:as separator due to syntactic the restrictions in regularmacro_rules!macros.Note that rust macros are flexible in which brackets you use for the invocation. You can use them as
hashmap!{}orhashmap![]orhashmap!(). This crate suggests{}as the convention for the map&set macros, it matches their Debug output.Macros
btreemapCreate aBTreeMapfrom a list of key-value pairsbtreesetCreate aBTreeSetfrom a list of elements.hashmapCreate aHashMapfrom a list of key-value pairshashsetCreate aHashSetfrom a list of elements.