Skip to content

HashMap and HashSet: resources

Beside A first HashMap and A first HashSet · companions: every error · lints

Level: reference · the reading list

One line: Where the rest of the Rust world explains hash maps and sets: the Book's chapter and its six sections, what three books on the shelf say and where, the std pages that matter (HashMap, HashSet, Entry, the which collection table, RandomState and the HashDoS note, Borrow), the crate underneath (hashbrown) and the design it follows, the error codes, the lints, and three places to read with care — including §5.5–5.6 of Rust: The Practical Guide, which prints its maps unsorted and says contains_key returns an Option.

Every entry was checked on 2026-09-23 and every URL again on 2026-09-24: each answered 200 to curl (crates.io and the two publishers' sites refuse scripted requests, so hashbrown is linked at docs.rs and GitHub, and the book sections come from the books themselves), and each #anchor named here exists on its page. Book sections are copied from each book's own table of contents, read on this machine. The video's title was checked; it was not watched, so no timestamps are given. "Read with care" names a sentence that fails when run on rustc 1.98.0, and says what ran.

If you read only four

Books

  • Rust in Action (Tim McNamara, Manning 2021), ch. 7 "Files and storage" → "Working with keys and values with HashMap and BTreeMap" (p. 241): "Creating a HashMap and populating it with values", "Retrieving values from HashMap and BTreeMap", "How to decide between HashMap and BTreeMap", and "Adding a database index to actionkv v2.0". A map used for something — the index of a key-value store — rather than shown for its own sake. The library's Rust in Action, run pages check other chapters listing by listing.
  • Effective Rust (David Drysdale, O'Reilly 2024), ch. 2 "Traits" → Item 10 "Familiarize yourself with standard traits" → the "PartialEq and Eq" and "Hash" subsections: what deriving each one promises, and why the two must agree — the contract every key on these pages signs. Free online ↗.
  • Rust: The Practical Guide (Nouman Azam, Rheinwerk 2025), ch. 5 → §5.5 "HashMaps" and §5.6 "HashSets". Two short sections, the reason A first HashMap and A first HashSet exist, and the book's home page here runs every sentence of them. Two to read with care.
  • Rust for Rustaceans (Jon Gjengset, No Starch 2021) has no section on hash maps — HashMap appears 22 times in the text, in passing — so it is not on this list; the library's chapter map says what it does cover.

std, the Reference and the crate underneath

Rust by Example

  • Std library types: HashMap ↗: insert, get, remove and iter on a phone book, with match on get. It says a key can be any type that implements Eq and Hash, and it prints the map by iterating it — so its shown output is one of the possible orders.
  • Alternate/custom key types ↗: #[derive(PartialEq, Eq, Hash)] on a struct key, the fix in entry 17.
  • HashSet ↗: the four operations, collected and printed.

The error index

Each code met on the errors page, with rustc's own long explanation, also printed by rustc --explain:

  • E0425 ↗ and E0433 ↗ — the missing use
  • E0282 ↗ — type annotations needed
  • E0308 ↗ — mismatched types, for an Option used as its value
  • E0369 ↗ — a binary operator with no implementation, for get(k) + 1
  • E0277 ↗ — a trait bound not satisfied, for &u32 == 5 and for Borrow
  • E0507 ↗ — moving out of map[k]
  • E0594 ↗ and E0596 ↗ — writing through &, and borrowing &mut from a & or a non-mut binding
  • E0368 ↗ — += on a reference
  • E0382 ↗ and E0502 ↗ — the map moved into its loop, and mutated inside it
  • E0599 ↗ — a method whose bounds the key type does not meet
  • E0597 ↗ — &str keys outliving their text

Lints

  • Clippy's lint list ↗: every lint with its group, level and the version it arrived in; the page's own search box finds map_entry, iter_over_hash_type and the rest of the lints page. The levels and groups quoted there were read from clippy-driver -Whelp on the pinned toolchain rather than from this page, since lints move between groups across releases.
  • rust-clippy on GitHub ↗: where a lint's source and its discussion live, for the cases the list's one paragraph does not settle — such as why map_entry stays quiet for &str keys.

Python, ABAP and Java, for the bridges

Read with care

  • Rust: The Practical Guide, §5.5, on contains_key. "The function returns an Option wrapping the value inside Some if the key exists, and None, otherwise." Run on rustc 1.98.0, word_counts.contains_key("Programming") is true and type_name_of_val of it prints bool; the method that returns Option<&V> is get. Recorded by first_hashmap_claims.rs.
  • Rust: The Practical Guide, §5.5 and §5.6, printing the map and the set. Both sections read their result off println!("{:?}", word_counts) and println!("{:?}", words). The count shown is right; the order is not a property of the program. The book's four-entry map, compiled once and run five times, printed five different orders. Sort, or use a BTreeMap, before reading anything off a printout.
  • Any page that says "constant time" without "expected". std's own table writes O(1)~, with the tilde, because a lookup that hits a long probe sequence or a table resize costs more; the average is constant, the worst case is not. The Book's and RBE's pages avoid the claim; the Practical Guide's "extremely fast" is not wrong, just unmeasured, and it is marked not checked on the lesson page.

See also

Po polsku

Lista lektur do HashMap i HashSet, sprawdzona 2026-09-23 i ponownie 2026-09-24. Najkrócej: rozdział 8.3 The Book (sześć sekcji, od HashMap::new po funkcje mieszające), strona std::collections::HashMap (uwaga o RandomState i HashDoS, kontrakt Eq i Hash), tabela When Should You Use Which Collection? w std::collections (koszty operacji, sekcja o entry, przykład zliczania znaków) i Programming Rust rozdział 16 „Collections” z sekcjami o HashMap/BTreeMap, wpisach (entries) i HashSet/BTreeSet. Dalej Rust in Action (rozdział 7, mapa jako indeks bazy klucz–wartość), Effective Rust (Item 10: PartialEq/Eq i Hash), strony std dla HashSet, Entry, Borrow, BTreeMap, crate hashbrown i projekt Swiss Tables, na którym się opiera, Rust by Example, indeks błędów dla każdego kodu ze strony błędów i lista lintów clippy.

Czytaj uważnie: Rust: The Practical Guide pisze, że contains_key zwraca Option — na rustc 1.98.0 zwraca bool — i odczytuje wyniki z wydruku {:?} mapy, którego kolejność zmienia się przy każdym uruchomieniu (pięć uruchomień, pięć kolejności). „Stały czas” wyszukiwania to w tabeli std O(1)~, z tyldą: średnio, nie w najgorszym przypadku.

Szukaj po polsku: tablica mieszająca lektury · rust HashMap dokumentacja · rust std collections which collection · hashbrown Swiss Tables · rust HashDoS RandomState · Rust: The Practical Guide HashMap