Vectors and arrays both store lists of values, but they work differently. Knowing when to use each helps you write better Rust programs.
Vector vs array comparison in Rust
fn main() {
// Array: fixed size, elements of the same type
let fixed_array: [i32; 3] = [10, 20, 30];
// Vector: growable, elements of the same type
let mut growable_vector: Vec<i32> = Vec::new();
growable_vector.push(10);
growable_vector.push(20);
growable_vector.push(30);
}Arrays have a fixed size known at compile time.
Vectors can grow or shrink during runtime.
let empty_array: [i32; 0] = [];let single_element_array = [42];let mut vector = Vec::new(); vector.push(5); vector.push(10);
let vector_with_values = vec![1, 2, 3];
This program shows how arrays and vectors differ. Arrays have fixed size and cannot be changed after creation. Vectors can grow and their elements can be changed.
fn main() {
// Create a fixed-size array
let fixed_array: [i32; 3] = [100, 200, 300];
println!("Array before change: {:?}", fixed_array);
// Create a growable vector
let mut growable_vector: Vec<i32> = Vec::new();
growable_vector.push(100);
growable_vector.push(200);
growable_vector.push(300);
println!("Vector before change: {:?}", growable_vector);
// Change an element in the vector
growable_vector[1] = 250;
println!("Vector after change: {:?}", growable_vector);
// Uncommenting the next line will cause a compile error because arrays are fixed size
// fixed_array[1] = 250;
// Show length differences
println!("Array length: {}", fixed_array.len());
println!("Vector length: {}", growable_vector.len());
// Add an element to the vector
growable_vector.push(400);
println!("Vector after push: {:?}", growable_vector);
}Arrays have O(1) access time and fixed size, so they use less memory overhead.
Vectors have O(1) amortized access time and can grow, but may reallocate memory when growing.
Common mistake: Trying to add elements to an array causes a compile error because arrays cannot grow.
Use arrays when size is fixed and known at compile time; use vectors when size can change.
Arrays have fixed size and are simple to use for known-size lists.
Vectors are growable and flexible for lists that change size.
Choose arrays for fixed data and vectors for dynamic data.
Practice
Solution
Step 1: Understand array size behavior
Arrays in Rust have a fixed size that must be known when the program is compiled.Step 2: Understand vector size behavior
Vectors are dynamic and can grow or shrink during program execution.Final Answer:
Arrays have a fixed size known at compile time, while vectors can grow or shrink at runtime. -> Option AQuick Check:
Array fixed size, Vector dynamic size = C [OK]
- Confusing which type is fixed or dynamic
- Thinking vectors are fixed size
- Assuming arrays can grow dynamically
Solution
Step 1: Identify vector creation syntax
In Rust, vectors are created using the macrovec![...]with square brackets and exclamation mark.Step 2: Compare options
let v = vec![1, 2, 3]; usesvec![1, 2, 3], which is correct. let v = [1, 2, 3]; is an array, C and D are invalid syntax for vectors.Final Answer:
let v = vec![1, 2, 3]; -> Option DQuick Check:
Vector syntax uses vec! macro = A [OK]
- Using array syntax instead of vec! macro
- Missing exclamation mark in vec! macro
- Using parentheses instead of brackets
let arr = [10, 20, 30];
let vec = vec![10, 20, 30];
println!("{} {}", arr.len(), vec.len());Solution
Step 1: Check array length
The arrayarrhas 3 elements, soarr.len()returns 3.Step 2: Check vector length
The vectorvecalso has 3 elements, sovec.len()returns 3.Final Answer:
3 3 -> Option BQuick Check:
Both lengths are 3 = B [OK]
- Assuming vector length is zero initially
- Confusing array and vector length methods
- Expecting compilation error due to different types
let arr = [1, 2, 3]; arr.push(4);
Solution
Step 1: Check array methods
Arrays in Rust have fixed size and do not support thepushmethod.Step 2: Understand push method usage
Thepushmethod is only available for vectors, which are growable.Final Answer:
Arrays do not have a push method; only vectors do. -> Option AQuick Check:
push() only works on vectors = D [OK]
- Trying to push to arrays
- Thinking arrays can be resized
- Ignoring method availability on types
Solution
Step 1: Analyze data size requirement
The list of user scores can change size, so a fixed size container is not suitable.Step 2: Choose appropriate Rust type
Vectors are growable and flexible, making them ideal for dynamic lists like scores.Final Answer:
Use a vector because it can grow or shrink as scores are added or removed. -> Option CQuick Check:
Dynamic list needs vector = A [OK]
- Choosing array for dynamic data
- Confusing tuples with lists
- Using string for numeric data
Ready to prove it?
Pass the test and this pattern moves unknown
