Chapter 22: Rust Data Structures

Rust Data Structures.

When people say “Rust Data Structures” in learning context, they usually mean two overlapping things:

  1. The built-in / primitive compound types that come with the language (tuples, arrays, slices)
  2. The collections from the standard library that almost every real Rust program uses (Vec, String, HashMap, HashSet, BTreeMap, etc.)

Rust is deliberately minimal in the language itself — it gives you very few built-in data structures, but the standard library collections are excellent, safe, and performant. Most of the time when you need a “data structure”, you reach for std::vec, std::collections, or crates like hashbrown, arrayvec, smallvec, etc.

Today I’ll teach both layers like your patient teacher sitting next to you: slowly, with lots of runnable examples, clear analogies, comparisons to other languages, and practical patterns you’ll see in real 2025–2026 Rust code.

Layer 1 — Built-in / Primitive Data Structures (Language Level)

These are part of the Rust syntax — no use needed.

1. Tuple — Fixed-size, heterogeneous grouping

Rust

Use case: returning multiple values from functions, fixed config pairs.

2. Array — Fixed-size, same-type, stack-allocated

Rust
  • Fast, no heap allocation
  • Size part of the type — [i32; 5] ≠ [i32; 6]
  • Cannot grow/shrink → use Vec when size is dynamic

3. Slice — View / reference into array / Vec / String

Rust
  • Type: &[T] or &mut [T]
  • Very common in function parameters (like &str is actually &[u8] with UTF-8 guarantee)

Layer 2 — Standard Library Collections (What You Use Every Day)

You need use std::collections::*; or specific imports.

1. Vec<T> — Growable, dynamic array (most used collection)

Rust
  • Heap-allocated, grows automatically
  • Like Python list / Java ArrayList / C++ vector

2. String (already covered, but recap as collection)

Rust

3. HashMap<K, V> — Hash table / dictionary

Rust
  • Keys must implement Eq + Hash
  • Common keys: String, &str (with .to_string() or String::from)

4. HashSet<T> — Unique unordered collection

Rust

5. BTreeMap<K, V> & BTreeSet<T> — Sorted / ordered versions

Use when you need keys sorted (e.g. leaderboard).

Rust

Quick Comparison Table (2026 Style)

Structure Growable? Ordered? Access Best For Heap?
[T; N] No Yes Index Fixed small data, stack fast No
&[T] No Yes Index Function params, views No
Vec<T> Yes Yes Index Most dynamic lists Yes
String Yes Yes Mutable text Yes
HashMap<K,V> Yes No Key Fast lookup by key Yes
BTreeMap<K,V> Yes Yes Key Sorted keys, range queries Yes
HashSet<T> Yes No Contains Uniqueness, membership Yes

Practice Project Suggestion

Bash

Try to build:

  • Vec<u32> of temperatures → find max/min/average
  • HashMap<String, u32> of city → population
  • HashSet<String> of favorite foods (add duplicates → see uniqueness)
  • Function that takes slice &[i32] and returns sum

Want next?

  • Iterators in depth (.map, .filter, .collect — huge in Rust)?
  • Custom structs + how they work with ownership/borrowing?
  • Enums + pattern matching on data structures?
  • Or common crates for more advanced structures (smallvec, arrayvec, indexmap)?

Just tell me — your Rust class from Hyderabad is going strong! 🦀🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *