Last active
November 12, 2015 18:50
-
-
Save waynenilsen/0c7a9e42fbc8581592c2 to your computer and use it in GitHub Desktop.
python-like dict syntax in rust thanks to macros
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
use std::collections::HashMap; | |
macro_rules! dict { | |
( $( $k:expr => $v:expr ),* ) => {{ | |
let mut m = HashMap::new(); | |
$( | |
m.insert($k, $v); | |
)* | |
m | |
}}; | |
} | |
fn main() { | |
let x = dict!( | |
"hello" => "world" | |
,"hello2" => "world2" | |
); | |
for (k, v) in x.iter() { | |
println!("{}: \"{}\"", *k, *v); | |
} | |
/** output | |
hello: "world" | |
hello2: "world2" | |
**/ | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment