Advertisement
cwchen

[Rust] map demo

Aug 23rd, 2017
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.49 KB | None | 0 0
  1. // Call HashMap class in Rust standard library
  2. use std::collections::HashMap;
  3.  
  4. fn main() {
  5.     // Create an empty map
  6.     let mut hash = HashMap::new();  // HashMap<&str, &str>
  7.  
  8.     // Insert (key, value) pairs into the map
  9.     hash.insert("one", "eins");
  10.     hash.insert("two", "zwei");
  11.     hash.insert("three", "drei");
  12.  
  13.     // Get value by key
  14.     assert_eq!(hash.get("one"), Some(&"eins"));
  15.  
  16.     // Get None when (key, value) doesn't exist
  17.     assert_eq!(hash.get("four"), None);
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement