Question : rust dictionary
Answered by : ahmad-khaefi
// Rust does not include dict in its std library.
// You can use `hashmap`
use std::collections::Hashmap;
fn main() { let x = Hashmap::from(["a", "foo"], ["b", "bar"]); println!("{}", x["a"]); // If you don't like the above syntax you can create a macro: macro_rules! dict { {$($key:ident => $value:expr),*} => { { let mut temp = Hashmap::new(); $( temp.insert(stringify!($key), $value); )* temp } }; } let y = dict! { a => "foo", b => "bar" }; println!("{}", y["a"]);
}
Source : | Last Update : Thu, 07 Jul 22