What is the difference between a Vec and an array in Rust?
Understand Vec vs array in Rust: fixed compile-time arrays on the stack versus growable heap Vecs, capacity, reallocation, and slices. With code examples.
Expected Interview Answer
An array [T; N] has a fixed length known at compile time and lives on the stack, while a Vec<T> is a growable, heap-allocated list whose length can change at runtime.
An array's size is part of its type, so [i32; 3] and [i32; 4] are different types and the length can never change. A Vec<T> stores a pointer, length, and capacity; it allocates on the heap, can push and pop elements, and reallocates to a larger buffer when it outgrows its capacity. You reach for arrays when the count is small and fixed, and for Vec when the number of elements is dynamic or unknown ahead of time. Both can be borrowed uniformly as a slice &[T], which is how most functions accept either.
- Arrays avoid heap allocation for a fixed, known count
- Vec grows and shrinks dynamically at runtime
- Vec owns a heap buffer and manages its own capacity
- Both coerce to &[T] slices for shared function signatures
- Arrays give compile-time length guarantees in the type
AI Mentor Explanation
An array is like a fixed 11-slot team sheet: the number of players is decided before the match and cannot change mid-game no matter what. A Vec is like the full squad register the selectors keep — they can add new players, drop others, and expand the pool between matches as many times as the season demands.
Step-by-Step Explanation
Step 1
Declare an array
Write let a: [i32; 3] = [1, 2, 3]; the length 3 is part of the type and fixed at compile time.
Step 2
Create a Vec
Use vec![1, 2, 3] or Vec::new(); it allocates on the heap and tracks pointer, length, and capacity.
Step 3
Grow the Vec
Call push to append; when length reaches capacity the Vec reallocates a larger buffer automatically.
Step 4
Borrow as a slice
Pass &a[..] or &v[..] as &[T] so a function accepts either an array or a Vec uniformly.
Step 5
Choose the right one
Use an array for a small fixed count; use Vec when the size is dynamic or unknown at compile time.
What Interviewer Expects
- Fixed compile-time length vs runtime-growable length
- Stack storage for arrays vs heap allocation for Vec
- Vec's pointer/length/capacity and reallocation behavior
- That array length is part of its type
- How both coerce to a &[T] slice
Common Mistakes
- Thinking an array can grow with push
- Believing [i32; 3] and [i32; 4] are the same type
- Confusing a Vec's length with its capacity
- Assuming Vec never reallocates as it grows
- Not knowing both borrow as &[T] slices
Best Answer (HR Friendly)
“An array in Rust has a fixed size decided when you write the code, like an egg carton that holds exactly twelve. A Vec is a flexible list that can grow and shrink while the program runs, so you use it whenever you do not know the count ahead of time.”
Code Example
fn sum(items: &[i32]) -> i32 {
items.iter().sum()
}
fn main() {
let arr: [i32; 3] = [1, 2, 3]; // fixed length, on the stack
let mut v: Vec<i32> = Vec::new(); // heap-allocated, growable
v.push(1);
v.push(2);
v.push(3);
v.push(4); // arrays could never do this
println!("{}", sum(&arr)); // 6 — array borrows as &[i32]
println!("{}", sum(&v)); // 10 — Vec borrows as &[i32]
}Follow-up Questions
- What is the difference between a Vec's length and capacity?
- When does a Vec reallocate, and why does that matter?
- How does an array coerce to a slice?
- Why is array length part of the type?
- When would you prefer an array over a Vec for performance?
MCQ Practice
1. Which is true of an array [T; N] in Rust?
An array's length N is encoded in its type and fixed at compile time; it cannot grow and typically lives on the stack.
2. What three things does a Vec<T> store?
A Vec is a pointer to a heap buffer plus its current length and its capacity, which lets it grow and reallocate.
3. How can one function accept both an array and a Vec?
Both arrays and Vecs coerce to the slice type &[T], so a function taking &[T] accepts either.
Flash Cards
Array length in Rust? — Fixed at compile time and part of the type [T; N]; it can never change.
Where does a Vec allocate? — On the heap; it stores a pointer, length, and capacity and can grow at runtime.
Vec length vs capacity? — Length is how many elements are stored; capacity is how many fit before it must reallocate.
How do both accept the same function? — Both coerce to &[T] slices, so a &[T] parameter takes an array or a Vec.
When to pick an array? — When the element count is small and known at compile time and you want to avoid heap allocation.