What is a Hash Map?
Part of the Logic & Flow section of Coddy's Rust journey — lesson 27 of 66.
Imagine you have a physical dictionary where you can quickly look up any word to find its definition. A hash map works in a similar way - it's a data structure that stores information in key-value pairs, allowing you to quickly find a value by providing its associated key.
In Rust, a hash map is represented by the HashMap<K, V> type, where K is the type of the keys and V is the type of the values. For example, you might have a hash map that stores student names as keys and their test scores as values, or country names as keys and their capital cities as values.
// Example concept: student names → scores
// "Alice" → 95
// "Bob" → 87
// "Carol" → 92The main advantage of hash maps is their speed - they provide very fast lookups, insertions, and deletions. When you need to find a value, you don't have to search through every item like you would with a vector. Instead, the hash map uses the key to quickly locate the exact position where the value is stored.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Cheat sheet
A hash map is a data structure that stores information in key-value pairs, allowing quick lookups by key.
In Rust, hash maps are represented by the HashMap<K, V> type, where K is the key type and V is the value type.
// Example: student names → scores
// "Alice" → 95
// "Bob" → 87
// "Carol" → 92Hash maps provide fast lookups, insertions, and deletions without needing to search through every item.
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Control Flow
The 'match' ExpressionMatching Multiple ValuesMatching RangesThe 'if let' ExpressionLoops as ExpressionsRecap - Simple Command Parser4Grouping Data with Structs
What is a Struct?Structs OverviewAccessing Struct FieldsMutable StructsStructs as Function ParametersTuple StructsRecap - Create a Book Struct7Handling Errors with 'Result'
What is a 'Result'?Using 'match' with 'Result'is_ok() and is_err()Shortcuts: unwrap and expectThe Question Mark Operator '?'Parsing Strings to NumbersRecap - Safe Division Function10Closures & Anonymous Functions
What is a Closure?Defining a Simple ClosureClosures with ParametersCapturing the EnvironmentRecap - Simple Adder Closure2Introduction to Vectors
What is a Vector?Creating a VectorAdding Elements with pushAccessing Vector ElementsIterating Over a VectorMutable IterationRemoving ElementsRecap - Basic Score Tracker5Key-Value Pairs with Hash Maps
What is a Hash Map?Creating a Hash MapInserting Key-Value PairsAccessing ValuesIterating Over a Hash MapUpdating a ValueRemoving a PairRecap - Word Counter8Project: Simple Item Inventory
Project SetupAdding an ItemChecking StockSelling an ItemPutting it all together