Using HashMap and AutoHashMap
Key-value storage from std.
Using HashMap and AutoHashMap is a free Zig Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why a Hash Map
A hash map stores key-value pairs and looks them up in near-constant time. Zig ships one in the standard library, so you rarely write your own. 🗺️
Reach for AutoHashMap First
std.AutoHashMap picks hashing and equality automatically for common key types like integers and enums. Give it a key type and a value type.
const Map = std.AutoHashMap(u32, []const u8);It Needs an Allocator
Like other growable containers, the map borrows memory. Pass an allocator to init and the map grows as you add entries.
var map = std.AutoHashMap(u32, u32).init(allocator);
defer map.deinit();Insert with put
put adds or overwrites a key. It can allocate while growing, so it returns an error union and you use try.
try map.put(1, 100);Look Up with get
get returns an optional value: the stored value if the key exists, or null otherwise. No crash on a missing key.
const v = map.get(1); // ?u32Unwrap the Result
Because get is optional, handle the absent case with orelse or an if capture before using the value.
const v = map.get(1) orelse 0;Check and Remove
Ask contains for a yes-or-no answer, and call remove to delete a key. Remove returns true if the key was present.
if (map.contains(1)) _ = map.remove(1);When Auto Cannot Decide
For keys like slices, automatic hashing will not compile. Then you use the lower-level std.HashMap and supply a context.
StringHashMap for Text Keys
String keys are common, so std offers a ready helper: std.StringHashMap already knows how to hash a []const u8.
var m = std.StringHashMap(i32).init(allocator);
try m.put("score", 42);Iterate Over Entries
Get an iterator and loop to visit every pair. Each entry exposes a key pointer and a value pointer.
var it = map.iterator();
while (it.next()) |e| {
std.debug.print("{d}={d}\n", .{ e.key_ptr.*, e.value_ptr.* });
}Always deinit the Map
The map owns its internal buffers, so call deinit when you are done. The testing allocator flags it if you forget.
defer map.deinit();Quick Check
You need a hash map whose keys are integers. Which standard type is the simplest fit?
Recap
Use AutoHashMap for simple keys and StringHashMap for text. Init with an allocator, put and get pairs, iterate, then deinit. 🎯
Frequently asked questions
Is the “Using HashMap and AutoHashMap” lesson free?
Yes — the full text of “Using HashMap and AutoHashMap” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.
What will I learn in “Using HashMap and AutoHashMap”?
Key-value storage from std. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Zig Academy?
No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Using HashMap and AutoHashMap” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Zig Academy lesson?
Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- A Generic Stack from Scratch
- A Singly Linked List
- Using HashMap and AutoHashMap
- Profiling and Safety Trade-offs