Bird
Raised Fist0
Rustprogramming~20 mins

Adding elements to vectors in Rust - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Vector Mastery in Rust
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Rust code adding elements to a vector?

Consider the following Rust code that adds elements to a vector. What will be printed?

Rust
fn main() {
    let mut v = Vec::new();
    v.push(10);
    v.push(20);
    v.push(30);
    println!("{:?}", v);
}
A[10, 20]
B[30, 20, 10]
C[10, 20, 30]
DCompilation error
Attempts:
2 left
💡 Hint

Remember that push adds elements to the end of the vector.

❓ Predict Output
intermediate
2:00remaining
What happens if you try to push to an immutable vector?

What error or output results from this Rust code?

Rust
fn main() {
    let v = Vec::new();
    v.push(5);
    println!("{:?}", v);
}
A[]
B[5]
CRuntime panic
DCompilation error: cannot borrow immutable variable `v` as mutable
Attempts:
2 left
💡 Hint

Think about whether the vector is mutable or not.

🔧 Debug
advanced
3:00remaining
Why does this Rust code cause a borrow checker error when adding elements?

Examine the code below. Why does it fail to compile?

Rust
fn main() {
    let mut v = vec![1, 2, 3];
    let first = &v[0];
    v.push(4);
    println!("first element: {}", first);
}
ACannot borrow `v` as mutable because it is also borrowed as immutable
BIndex out of bounds error at runtime
CCompilation error: missing semicolon
DNo error, prints 'first element: 1'
Attempts:
2 left
💡 Hint

Think about Rust's borrowing rules when mixing mutable and immutable references.

❓ Predict Output
advanced
2:00remaining
What is the length of the vector after these operations?

Given the following Rust code, what is the length of the vector v after execution?

Rust
fn main() {
    let mut v = Vec::with_capacity(5);
    v.push(1);
    v.push(2);
    v.push(3);
    v.pop();
    v.push(4);
    println!("{}", v.len());
}
A2
B3
C5
D4
Attempts:
2 left
💡 Hint

Remember that pop removes one element from the vector.

🧠 Conceptual
expert
3:00remaining
Which option correctly explains why vectors grow dynamically in Rust?

Why can vectors in Rust grow dynamically when elements are added with push?

AVectors allocate extra capacity and reallocate memory when needed to accommodate new elements
BVectors are fixed size arrays and cannot grow dynamically
CVectors use linked lists internally to add elements without reallocating
DVectors store elements on the stack which allows dynamic resizing
Attempts:
2 left
💡 Hint

Think about how memory allocation works for vectors in Rust.

Practice

(1/5)
1. What does the push method do when used on a Rust vector?
easy
A. Removes the last element from the vector
B. Adds a new element to the end of the vector
C. Creates a new vector with one element
D. Sorts the elements in the vector

Solution

  1. Step 1: Understand the purpose of push

    The push method is used to add elements to a vector in Rust.
  2. Step 2: Identify what push does specifically

    It adds a new element to the end of the vector, increasing its size by one.
  3. Final Answer:

    Adds a new element to the end of the vector -> Option B
  4. Quick Check:

    push adds element [OK]
Hint: Remember: push always adds at the end [OK]
Common Mistakes:
  • Confusing push with pop (which removes elements)
  • Thinking push creates a new vector
  • Assuming push sorts the vector
2. Which of the following is the correct way to declare a mutable vector and add the number 10 to it in Rust?
easy
A. let v = Vec::new(); v.add(10);
B. let v = Vec::new(); v.push(10);
C. let mut v = Vec::new(); v.add(10);
D. let mut v = Vec::new(); v.push(10);

Solution

  1. Step 1: Check vector mutability

    To add elements, the vector must be mutable, so let mut v is required.
  2. Step 2: Use correct method to add element

    The method to add an element is push, not add.
  3. Final Answer:

    let mut v = Vec::new(); v.push(10); -> Option D
  4. Quick Check:

    Mutable vector + push method [OK]
Hint: Mutable vector + push method adds elements [OK]
Common Mistakes:
  • Forgetting to make the vector mutable
  • Using a non-existent method like add
  • Trying to push on an immutable vector
3. What will be the output of this Rust code?
let mut v = vec![1, 2];
v.push(3);
println!("{:?}", v);
medium
A. [1, 2]
B. Compilation error
C. [1, 2, 3]
D. [3, 1, 2]

Solution

  1. Step 1: Understand initial vector content

    The vector starts with elements [1, 2].
  2. Step 2: Apply the push method

    The push(3) adds 3 at the end, so vector becomes [1, 2, 3].
  3. Final Answer:

    [1, 2, 3] -> Option C
  4. Quick Check:

    push adds element at end [OK]
Hint: push adds element at the end of vector [OK]
Common Mistakes:
  • Expecting push to add at the start
  • Thinking push replaces elements
  • Assuming code causes error
4. Identify the error in this Rust code snippet:
let v = Vec::new();
v.push(5);
medium
A. Vector is not mutable, so push cannot be called
B. push method does not exist for vectors
C. Vec::new() cannot create an empty vector
D. Missing semicolon after Vec::new()

Solution

  1. Step 1: Check vector mutability

    The vector is declared as immutable with let v, so it cannot be changed.
  2. Step 2: Understand push method requirements

    The push method requires the vector to be mutable to add elements.
  3. Final Answer:

    Vector is not mutable, so push cannot be called -> Option A
  4. Quick Check:

    push needs mutable vector [OK]
Hint: Make vector mutable to use push [OK]
Common Mistakes:
  • Assuming push works on immutable vectors
  • Thinking Vec::new() is invalid
  • Ignoring mut keyword necessity
5. You want to add multiple elements [4, 5, 6] to an existing mutable vector v. Which code correctly adds all these elements efficiently?
hard
A. v.extend([4, 5, 6]);
B. v.push(4, 5, 6);
C. v.add([4, 5, 6]);
D. v.push([4, 5, 6]);

Solution

  1. Step 1: Understand adding multiple elements

    Using push adds one element at a time, so multiple calls are needed for multiple elements.
  2. Step 2: Use the efficient method for multiple elements

    The extend method adds all elements from an iterable at once, which is more efficient.
  3. Final Answer:

    v.extend([4, 5, 6]); -> Option A
  4. Quick Check:

    Use extend for multiple elements [OK]
Hint: Use extend to add many elements at once [OK]
Common Mistakes:
  • Using push with a whole array (causes error)
  • Using non-existent add method
  • Calling push multiple times instead of extend
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