Python Vectors & Growable Collections (Vec<T>)
Vec<T> (vector) is a growable, heap-allocated array that stores elements contiguously. Vectors allow dynamic resizing with .push() and .pop(), safe indexing with .get(), and iteration over elements.
"If a fixed array [T; N] is a rigid metal filing cabinet with fixed slots, a Vec<T> is an expandable accordion folder that automatically stretches to hold as many documents as you insert."
Deep Dive: How It Works
Creation: Vec::new() or convenient vec![1, 2, 3] macro.
Safe Indexing: v[index] panics on out of bounds. v.get(index) returns Option<&T> safely.
Iteration: for item in &v borrows elements immutably; for item in &mut v allows modification in-place.
Syntax Blueprint
let mut v = vec![10, 20]; v.push(30); let second = v.get(1); // Some(&20)
Vectors manage their own heap buffer, growing capacity automatically as needed.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Pushing to a vector while holding a reference to an element.Why it happens: Pushing can cause reallocation of the entire underlying buffer, invalidating existing pointers. Rust compiler prevents this!
How to fix: Finish using element references before performing mutating pushes.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Push items to vector and sum
Initialize let mut scores = Vec::new();
Push 40, 50, 60 to scores.
Calculate sum with let sum: i32 = scores.iter().sum();
Print "Total Score: 150".
Finished reading and practicing?
Mark this lesson as completed to update your course progress.