Chapter 23: Rust Data Structures

Rust Data Structures.

This is a very natural next step. In Rust, when people say “data structures”, they usually mean two slightly different things:

  1. The built-in / primitive compound types that are part of the language syntax itself (tuples, arrays, slices)
  2. The powerful collections from the standard library that almost every real program uses (Vec, HashMap, String, HashSet, BTreeMap, etc.)

Rust deliberately keeps the language core very small — it gives you only a few built-in data structures — but the standard library collections are excellent, very safe thanks to ownership & borrowing, and usually what people actually reach for.

Today I’ll explain both layers like your personal teacher sitting next to you: slowly, with lots of copy-paste examples you can run right now (cargo run), clear analogies (Hyderabad traffic, biryani ingredients, phone contacts), comparisons to Python/Java/C++, and practical patterns you’ll see in 2026 Rust code.

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

These come with Rust syntax — no use or std:: needed.

A. Tuple — Fixed-size, mixed-type grouping

Rust
  • Fixed size at compile time
  • Heterogeneous (different types ok)
  • Use case: returning multiple values from functions, small fixed records

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

Rust
  • No heap allocation → very fast
  • Bounds checked at runtime (panic on out-of-range)
  • Cannot grow/shrink → use Vec when you need dynamic size

C. Slice — Dynamic view / reference into array / Vec / String

Rust
  • Type: &[T] or &mut [T]
  • Zero-cost view — no copying
  • Very common in function parameters (like &str is actually a UTF-8 guaranteed slice of [u8])

Part 2 — Standard Library Collections (What You Use 95% of the Time)

These live in std::collections or std::vec, std::string.

A. Vec<T> — Growable, resizable array (most common collection)

Rust
  • Heap allocated → can grow
  • Like Python list, Java ArrayList, C++ std::vector

B. HashMap<K, V> — Fast key-value lookup

Rust

C. HashSet<T> — Unique unordered elements

Rust

D. BTreeMap<K, V> — Sorted key-value (when order matters)

Rust

Quick Comparison Table (2026 Style)

Name Growable? Ordered? Access By Memory Best For
[T; N] No Yes Index Stack Fixed small data, performance critical
&[T] No Yes Index Borrow Function params, views, zero-copy
Vec<T> Yes Yes Index Heap Most dynamic lists / sequences
String Yes Yes Heap Mutable text
HashMap<K,V> Yes No Key Heap Fast lookup / cache / config
BTreeMap<K,V> Yes Yes Key Heap Sorted data, range queries, leaderboards
HashSet<T> Yes No Contains Heap Uniqueness, membership checks

Teacher Summary & Advice

In Rust you usually start with:

  • Small fixed data → array [T; N] or tuple
  • Dynamic list → Vec<T>
  • Text → String + &str
  • Key-value lookup → HashMap (fast) or BTreeMap (sorted)
  • Unique items → HashSet

Rust makes all of these memory-safe thanks to ownership & borrowing — no null pointer exceptions, no use-after-free, no data races.

Practice challenge for today:

  1. Create Vec<i32> of last 7 days temperatures → compute average
  2. Make HashMap<String, u32> city → population → print sorted by population (hint: collect to vec & sort)
  3. Use HashSet to store favorite foods & check if “Biryani” is there

Want to go deeper next?

  • Iterators + .map, .filter, .collect (huge in real Rust code)
  • Custom structs & enums as data structures
  • Ownership & borrowing inside collections (very important)
  • Or common crates like smallvec, indexmap, arrayvec?

Just tell me — your Rust teacher from Hyderabad is right here! 🦀🚀

You may also like...

Leave a Reply

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