Just like Vectors, HashMaps also save their values on Heap.
All Keys must have same type, and all values must have same type.
Creating HashMap through iterators:
#![allow(unused)]fnmain() {
use std::collections::HashMap;
let teams = vec![String::from("Blue"), String::from("Yellow")];
let initial_scores = vec![10, 50];
letmut scores: HashMap<_, _> =
teams.into_iter()
.zip(initial_scores.into_iter()) // creates a tuple, example ("Blue", 10)
.collect(); // Converts tuple into HashMap}
HashMap and ownership: Types that implement Copy trait will be copied else moved. For Example, i32 will be copied but String will be moved.
#![allow(unused)]fnmain() {
use std::collections::HashMap;
let field_name = String::from("Favorite color");
let field_value = String::from("Blue");
letmut map = HashMap::new();
map.insert(field_name, field_value);
// You can't use field_name or field_value now, as they've been moved}
Accessing value in HashMap, the get method returns Option<&V>:
#![allow(unused)]fnmain() {
use std::collections::HashMap;
letmut scores = HashMap::new();
scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Yellow"), 50);
let team_name = String::from("Blue");
let score = scores.get(&team_name); // This is how we access value for a certain Key// The score variable will contain - Some(&10)}
Iterating over a HashMap:
#![allow(unused)]fnmain() {
use std::collections::HashMap;
letmut scores = HashMap::new();
scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Yellow"), 50);
for (key, value) in &scores {
println!("{}: {}", key, value);
}
}
#![allow(unused)]fnmain() {
scores.insert(String::from("Blue"), 10);
// We'll need to use entry to use or_insert
scores.entry(String::from("Yellow")).or_insert(50); // This will add 50
scores.entry(String::from("Blue")).or_insert(50); // This won't replace 10 with 50// Output - scores = {"Yellow": 50, "Blue": 10}}
Updating a value based on the Old Value:
#![allow(unused)]fnmain() {
use std::collections::HashMap;
let text = "hello world wonderful world";
letmut map = HashMap::new();
for word in text.split_whitespace() {
let count = map.entry(word).or_insert(0); // or_insert returns mutable reference to the Value, &mut V
*count += 1;
}
println!("{:?}", map);
// Output - map = {"world": 2, "hello": 1, "wonderful": 1}}
The hashing function that Rust uses is SipHash. You can replace the hashing function. Please refer here for more.