Rust Data Structures

🦀 Rust Data Structures

Data Structures in Rust are used to store, organize, and manage data efficiently.

Rust’s data structures are safe, fast, and tightly integrated with ownership & borrowing.


🔹 Classification of Rust Data Structures

Rust data structures can be divided into three main categories:

1️⃣ Primitive / Built-in
2️⃣ Standard Library Collections
3️⃣ User-Defined Data Structures


 1. Primitive / Built-in Data Structures

▶️ Arrays ([T; N])

Fixed-size, same data type.

let numbers: [i32; 3] = [1, 2, 3];

✔ Stored on stack
❌ Size cannot change


▶️ Tuples (( ))

Fixed-size, different data types.

let person = ("Sanjit", 22, true);
println!("{}", person.0);

✔ Lightweight
✔ Useful for returning multiple values


 2. Standard Library Collections (Most Important)

These are stored on the heap and can grow or shrink.


▶️ Vector (Vec<T>)

Dynamic array (most used).

let mut v = Vec::new();
v.push(10);
v.push(20);

Or:

let v = vec![1, 2, 3];

✔ Growable
✔ Fast
✔ Indexing + iteration


▶️ String (String)

Growable UTF-8 text data.

let mut s = String::from("Rust");
s.push_str(" Lang");

✔ Heap allocated
✔ UTF-8 safe


▶️ HashMap (HashMap<K, V>)

Key-value storage.

use std::collections::HashMap;

let mut scores = HashMap::new();
scores.insert(“Math”, 90);
scores.insert(“Science”, 95);

✔ Fast lookup
✔ No duplicate keys


▶️ HashSet (HashSet<T>)

Stores unique values only.

use std::collections::HashSet;

let mut set = HashSet::new();
set.insert(1);
set.insert(1); // duplicate ignored


▶️ VecDeque (VecDeque<T>)

Double-ended queue.

use std::collections::VecDeque;

let mut dq = VecDeque::new();
dq.push_front(1);
dq.push_back(2);


 3. User-Defined Data Structures

▶️ Struct

Used to group related data.

struct User {
name: String,
age: u8,
}
let user = User {
name: String::from(“Sanjit”),
age: 22,
};

✔ Similar to C structs
✔ Very common in Rust


▶️ Enum

Used to represent multiple states.

enum Status {
Active,
Inactive,
}

✔ Extremely powerful
✔ Used with match


▶️ Struct + Enum (Real-World Rust Style)

enum Role {
Admin,
User,
}
struct Account {
username: String,
role: Role,
}

🔹 Stack vs Heap (Data Structure Perspective)

Stack Heap
Fixed size Dynamic size
Faster Slightly slower
Arrays, tuples Vec, String, HashMap

🔹 Ownership with Data Structures

let v1 = vec![1, 2, 3];
let v2 = v1; // ownership moved
// println!(“{:?}”, v1); ❌
println!(“{:?}”, v2);

✔ Use references to avoid move:

let v = vec![1, 2, 3];
print_vec(&v);
fn print_vec(v: &Vec<i32>) {
println!(“{:?}”, v);
}

🔹 Common Rust Data Structures (Quick Table)

Structure Use Case
Array Fixed-size data
Tuple Mixed data
Vec Dynamic list
String Text
HashMap Key-value
HashSet Unique values
Struct Custom objects
Enum State modeling

🧠 Key Takeaways

  • Rust data structures are memory-safe

  • Heap structures follow ownership rules

  • Vec and HashMap are most used

  • struct + enum = idiomatic Rust

  • Compiler prevents invalid access

You may also like...