Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR RUST

rust dictionary

// 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"]);
}
 
PREVIOUS NEXT
Tagged: #rust #dictionary
ADD COMMENT
Topic
Name
7+3 =