Created
November 8, 2021 15:15
-
-
Save mdrokz/3b487f924fd41ef43e97bf80a6f20df8 to your computer and use it in GitHub Desktop.
Rust macro for creating hashmaps using js object syntax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro_rules! map { | |
($($v: literal => $s: expr),*) => {{ | |
let mut h = HashMap::new(); | |
$( | |
h.insert($v,$s); | |
)* | |
h | |
}} | |
} | |
fn main() { | |
// Created HashMap using simple object syntax | |
let v = map! { | |
"loop" => 300, | |
"meow" => 200, | |
"keow" => 100 | |
}; | |
let s = v.get("loop"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment