Storing Keys with Their Values: Hash Maps in Rust (Tutorial 19/100)
Introduction
Storing Keys with Their Values: Hash Maps in Rust (Tutorial 19/100)

Introduction
Hash maps, also known as hash tables, are a crucial data structure in programming that allow for efficient storage and retrieval of data. In Rust, the standard library provides a robust implementation of hash maps through the HashMap type. This article will delve into the intricacies of using hash maps in Rust, exploring their creation, insertion, retrieval, and deletion, along with some practical examples.
Understanding Hash Maps
A hash map is a data structure that stores data in key-value pairs. It uses a hash function to map keys to indices of an underlying array, enabling constant-time O(1) average complexity for insert, search, and delete operations. This makes hash maps an ideal choice when dealing with large datasets where quick data access is crucial.
Creating a HashMap in Rust
To create a HashMap in Rust, you need to import the HashMap type from the std::collections module. Here's a simple example of creating an empty HashMap:
use std::collections::HashMap;
let mut map: HashMap<String, i32> = HashMap::new();
In this example, String is the type of keys, and i32 is the type of values.
Inserting Data into a HashMap
To insert data into a HashMap, you can use the insert method. Here's an example:
map.insert("one".to_string(), 1);
map.insert("two".to_string(), 2);
map.insert("three".to_string(), 3);
Retrieving Data from a HashMap
To retrieve data from a HashMap, you can use the get method, which returns an Option type. Here's an example:
let value = map.get("one");
match value {
Some(v) => println!("The value is: {}", v),
None => println!("No value found for key"),
}
Deleting Data from a HashMap
To delete data from a HashMap, you can use the remove method. Here's an example:
map.remove("one");
Iterating over a HashMap
To iterate over all key-value pairs in a HashMap, you can use a for loop. Here's an example:
for (key, value) in &map {
println!("Key: {}, Value: {}", key, value);
}
Hash Maps and Ownership
In Rust, the HashMap type takes ownership of its keys and values. This means that when you insert a key-value pair into a HashMap, the ownership of the key and value is transferred to the HashMap. If you try to use the key or value after inserting it into the HashMap, Rust will throw a compile-time error.
Practical Example: Word Count
Let’s look at a practical example of using a HashMap in Rust. We'll create a program that reads a string and counts the occurrences of each word.
use std::collections::HashMap;
fn main() {
let text = "one fish two fish red fish blue fish";
let mut word_count: HashMap<String, i32> = HashMap::new();
for word in text.split_whitespace() {
let count = word_count.entry(word.to_string()).or_insert(0);
*count += 1;
}
for (word, count) in &word_count {
println!("{}: {}", word, count);
}
}
In this example, we use the entry method to get a mutable reference to the count for a word. If the word is not in the HashMap, entry will insert a new key-value pair with the value set to 0. Then, we increment the count.
Conclusion
Hash maps are a powerful and versatile data structure that can greatly enhance the efficiency of your Rust programs. They offer constant-time complexity for insertion, retrieval, and deletion, making them an excellent choice for many use cases.
References
Follow me for more thought-provoking content. Stay tuned for what’s next.
메타데이터
- post_id
- 868ca1eceb03
- slug
- storing-keys-with-their-values-hash-maps-in-rust-tutorial-19-100-868ca1eceb03
- url
- https://towardsdev.com/storing-keys-with-their-values-hash-maps-in-rust-tutorial-19-100-868ca1eceb03
- canonical_url
- https://towardsdev.com/storing-keys-with-their-values-hash-maps-in-rust-tutorial-19-100-868ca1eceb03
- author_url
- https://medium.com/@giorgio.martinez1926
- status
- ok
- fetched_at
- 2026-07-27 12:31:45