Rust Programming: Accessing Values in a Hash Map

0

Hash maps are a type of data structure that store key-value pairs. The key is used to identify the value, and the value is the actual data that is stored. Hash maps are very efficient at accessing values, because they can quickly find the value associated with a given key.

To access a value in a hash map, we can use the get() method. The get() method takes a key as an argument and returns an Option<&V>, where V is the type of the values stored in the hash map. If the key is found in the hash map, the get() method will return a reference to the value associated with that key. Otherwise, the get() method will return None.

Here is an example of how to access a value in a hash map:

Rust
use std::collections::HashMap;

let mut 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).copied().unwrap_or(0);

println!("The score for the Blue team is {}", score);

This code will print the following output:

The score for the Blue team is 10

If we try to access a key that is not in the hash map, the get() method will return None. We can handle this by using a match expression, or by using the unwrap_or() method to provide a default value.

Here is an example of how to handle a missing key in a hash map using a match expression:

Rust
use std::collections::HashMap;

let mut scores = HashMap::new();

scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Yellow"), 50);

let team_name = String::from("Green");
let score = match scores.get(&team_name) {
    Some(&score) => score,
    None => 0,
};

println!("The score for the {} team is {}", team_name, score);

This code will print the following output:

The score for the Green team is 0

Here is an example of how to handle a missing key in a hash map using the unwrap_or() method:

Rust
use std::collections::HashMap;

let mut scores = HashMap::new();

scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Yellow"), 50);

let team_name = String::from("Green");
let score = scores.get(&team_name).copied().unwrap_or(0);

println!("The score for the {} team is {}", team_name, score);

This code will print the following output:

The score for the Green team is 0

Iterating Over a Hash Map

We can iterate over the key/value pairs in a hash map using a for loop. To do this, we can use the iter() method to get an iterator over the hash map. The iter() method returns an iterator that yields tuples, where each tuple contains a key and a value.

Here is an example of how to iterate over a hash map:

Rust
use std::collections::HashMap;

let mut scores = HashMap::new();

scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Yellow"), 50);

for (key, value) in &scores {
    println!("{key}: {value}");
}

This code will print the following output:

Yellow: 50
Blue: 10


Post a Comment

0Comments
Post a Comment (0)