Creating a Hash Map
Part of the Logic & Flow section of Coddy's Rust journey — lesson 28 of 66.
Now that you understand what hash maps are, let's learn how to create one. To use hash maps in Rust, you first need to bring the HashMap type into scope with a use statement at the top of your file:
use std::collections::HashMap;Once you've imported HashMap, you can create a new, empty hash map using the HashMap::new() function:
let mut scores = HashMap::new();Notice that we declare the hash map as mut because we'll likely want to add data to it later. When you create an empty hash map this way, Rust doesn't know what types the keys and values will be until you start inserting data.
You can also specify the types explicitly if you prefer:
let mut scores: HashMap<String, i32> = HashMap::new();This creates a hash map that will store String keys (like student names) and i32 values (like test scores). The empty hash map is now ready to store your key-value pairs.
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
To use hash maps in Rust, import HashMap from the standard library:
use std::collections::HashMap;Create an empty hash map with implicit typing:
let mut scores = HashMap::new();Create an empty hash map with explicit type annotation:
let mut scores: HashMap<String, i32> = HashMap::new();Hash maps must be declared as mut if you plan to modify them. When using implicit typing, Rust infers the key and value types when you insert data.
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