Bird
Raised Fist0
Rustprogramming~10 mins

Vector vs array comparison in Rust - Visual Side-by-Side Comparison

Choose your learning style10 modes available
Concept Flow - Vector vs array comparison
Start
↓
Define Array
↓
Define Vector
↓
Compare Size
↓
Compare Access
↓
Compare Mutability
↓
Compare Memory
↓
End
This flow shows the steps to compare arrays and vectors in Rust: defining them, then comparing their size, access, mutability, and memory behavior.
Execution Sample
Rust
let arr = [1, 2, 3];
let mut vec = vec![1, 2, 3];
vec.push(4);
println!("Array: {:?}, Vector: {:?}", arr, vec);
This code defines an array and a vector, adds an element to the vector, and prints both.
Execution Table
StepActionArray StateVector StateNotes
1Define array arr = [1, 2, 3][1, 2, 3]emptyArray fixed size 3, elements set
2Define mutable vector vec = vec![1, 2, 3][1, 2, 3][1, 2, 3]Vector created with 3 elements
3Push 4 into vector vec[1, 2, 3][1, 2, 3, 4]Vector size grows, array size fixed
4Print arr and vec[1, 2, 3][1, 2, 3, 4]Shows array unchanged, vector updated
5End[1, 2, 3][1, 2, 3, 4]Array size fixed, vector size dynamic
💡 Execution ends after printing both data structures showing their differences.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
arrundefined[1, 2, 3][1, 2, 3][1, 2, 3]
vecundefined[1, 2, 3][1, 2, 3, 4][1, 2, 3, 4]
Key Moments - 3 Insights
Why can't we push elements to an array like we do with a vector?
Arrays have a fixed size known at compile time, so their size cannot change after creation. Vectors are dynamic and can grow or shrink at runtime, as shown in execution_table step 3.
Does the array change when we push to the vector?
No, the array remains unchanged because it is a separate fixed-size data structure. The vector changes independently, as seen in execution_table steps 3 and 4.
Why do we need to declare the vector as mutable but not the array?
Vectors need to be mutable to allow operations like push that change their size. Arrays have fixed size and content, so mutability is not required for size changes, as shown in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the length of the vector?
A4
B3
CUndefined
DCannot push to vector
💡 Hint
Check the Vector State column at step 3 in the execution_table.
At which step does the array get defined?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the Action column in execution_table for array definition.
If we remove the 'mut' keyword from the vector declaration, what happens at step 3?
AVector can still push elements
BArray size changes instead
CCompilation error due to immutability
DNo change, code runs fine
💡 Hint
Recall Rust's mutability rules shown in key_moments about vector push.
Concept Snapshot
Array: fixed size, known at compile time
Vector: dynamic size, grows/shrinks at runtime
Array elements fixed, vector elements mutable
Vector needs 'mut' to change size
Arrays stored on stack, vectors on heap
Use vector for flexible collections
Full Transcript
This visual execution compares Rust arrays and vectors. We start by defining an array with fixed size 3 and a mutable vector with the same elements. Then we push a new element to the vector, which grows to size 4, while the array remains unchanged. The execution table tracks each step, showing array and vector states. Key moments clarify why arrays cannot grow and why vectors need mutability. The quiz tests understanding of size changes, definition steps, and mutability rules. The snapshot summarizes key differences: arrays are fixed size and stored on stack, vectors are dynamic and stored on heap, requiring mutability to change size.

Practice

(1/5)
1. Which of the following statements correctly describes the main difference between a Rust array and a Rust vector?
easy
A. Arrays have a fixed size known at compile time, while vectors can grow or shrink at runtime.
B. Vectors have a fixed size known at compile time, while arrays can grow or shrink at runtime.
C. Both arrays and vectors have fixed sizes and cannot change after creation.
D. Arrays are only used for strings, while vectors are used for numbers.

Solution

  1. Step 1: Understand array size behavior

    Arrays in Rust have a fixed size that must be known when the program is compiled.
  2. Step 2: Understand vector size behavior

    Vectors are dynamic and can grow or shrink during program execution.
  3. Final Answer:

    Arrays have a fixed size known at compile time, while vectors can grow or shrink at runtime. -> Option A
  4. Quick Check:

    Array fixed size, Vector dynamic size = C [OK]
Hint: Remember: arrays fixed size, vectors flexible size [OK]
Common Mistakes:
  • Confusing which type is fixed or dynamic
  • Thinking vectors are fixed size
  • Assuming arrays can grow dynamically
2. Which of the following is the correct syntax to declare a vector of integers in Rust?
easy
A. let v = (1, 2, 3);
B. let v = [1, 2, 3];
C. let v = vector(1, 2, 3);
D. let v = vec![1, 2, 3];

Solution

  1. Step 1: Identify vector creation syntax

    In Rust, vectors are created using the macro vec![...] with square brackets and exclamation mark.
  2. Step 2: Compare options

    let v = vec![1, 2, 3]; uses vec![1, 2, 3], which is correct. let v = [1, 2, 3]; is an array, C and D are invalid syntax for vectors.
  3. Final Answer:

    let v = vec![1, 2, 3]; -> Option D
  4. Quick Check:

    Vector syntax uses vec! macro = A [OK]
Hint: Vectors use vec! macro with square brackets [OK]
Common Mistakes:
  • Using array syntax instead of vec! macro
  • Missing exclamation mark in vec! macro
  • Using parentheses instead of brackets
3. What will be the output of the following Rust code?
let arr = [10, 20, 30];
let vec = vec![10, 20, 30];
println!("{} {}", arr.len(), vec.len());
medium
A. 3 0
B. 3 3
C. 0 3
D. Compilation error

Solution

  1. Step 1: Check array length

    The array arr has 3 elements, so arr.len() returns 3.
  2. Step 2: Check vector length

    The vector vec also has 3 elements, so vec.len() returns 3.
  3. Final Answer:

    3 3 -> Option B
  4. Quick Check:

    Both lengths are 3 = B [OK]
Hint: Both array and vector have length 3 here [OK]
Common Mistakes:
  • Assuming vector length is zero initially
  • Confusing array and vector length methods
  • Expecting compilation error due to different types
4. Identify the error in this Rust code snippet:
let arr = [1, 2, 3];
arr.push(4);
medium
A. Arrays do not have a push method; only vectors do.
B. The array is missing a semicolon after declaration.
C. The push method requires a mutable reference to the array.
D. The code is correct and will compile.

Solution

  1. Step 1: Check array methods

    Arrays in Rust have fixed size and do not support the push method.
  2. Step 2: Understand push method usage

    The push method is only available for vectors, which are growable.
  3. Final Answer:

    Arrays do not have a push method; only vectors do. -> Option A
  4. Quick Check:

    push() only works on vectors = D [OK]
Hint: Only vectors support push(), arrays do not [OK]
Common Mistakes:
  • Trying to push to arrays
  • Thinking arrays can be resized
  • Ignoring method availability on types
5. You want to store a list of user scores that can change size during the program. Which Rust type should you choose and why?
hard
A. Use a tuple because it can hold multiple types of scores.
B. Use an array because it is faster and fixed size is better for scores.
C. Use a vector because it can grow or shrink as scores are added or removed.
D. Use a string because scores are numbers stored as text.

Solution

  1. Step 1: Analyze data size requirement

    The list of user scores can change size, so a fixed size container is not suitable.
  2. Step 2: Choose appropriate Rust type

    Vectors are growable and flexible, making them ideal for dynamic lists like scores.
  3. Final Answer:

    Use a vector because it can grow or shrink as scores are added or removed. -> Option C
  4. Quick Check:

    Dynamic list needs vector = A [OK]
Hint: Dynamic size needs vector, fixed size needs array [OK]
Common Mistakes:
  • Choosing array for dynamic data
  • Confusing tuples with lists
  • Using string for numeric data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard