A vector in Rust is a growable, heap-allocated list that can store multiple values of the same type. It is like a resizable array.
Click to reveal answer
beginner
How do you create an empty vector in Rust?
You can create an empty vector using <code>Vec::new()</code>. For example: <code>let v: Vec<i32> = Vec::new();</code>
Click to reveal answer
beginner
How to create a vector with initial values in Rust?
Use the <code>vec!</code> macro with values inside brackets. Example: <code>let v = vec![1, 2, 3];</code> creates a vector with three integers.
Click to reveal answer
beginner
What method do you use to add elements to a vector after creation?
Use the push() method to add elements to the end of a vector. Example: v.push(4);
Click to reveal answer
beginner
Why is a vector preferred over an array when the size can change?
Vectors can grow or shrink dynamically at runtime, while arrays have a fixed size known at compile time. Vectors offer flexibility for unknown or changing data sizes.
Click to reveal answer
Which Rust code creates an empty vector of integers?
Alet v: Vec<i32> = Vec::new();
Blet v = vec![];
Clet v = [1, 2, 3];
Dlet v = Vec::empty();
✗ Incorrect
The correct way to create an empty vector is using Vec::new(). The vec![] macro also creates an empty vector but requires type inference or annotation.
How do you create a vector with initial values 5, 10, 15?
Alet v = [5, 10, 15];
Blet v = Vec::new(5, 10, 15);
Clet v = vector(5, 10, 15);
Dlet v = vec![5, 10, 15];
✗ Incorrect
The vec! macro creates a vector with the given initial values.
Which method adds an element to the end of a vector?
Aappend()
Binsert()
Cpush()
Dadd()
✗ Incorrect
The push() method adds an element to the end of a vector.
What is the main difference between arrays and vectors in Rust?
AVectors can grow dynamically, arrays have fixed size.
BVectors have fixed size, arrays do not.
CArrays can grow dynamically, vectors cannot.
DArrays and vectors are exactly the same.
✗ Incorrect
Vectors can grow or shrink dynamically, arrays have fixed size known at compile time.
Which of these is NOT a valid way to create a vector?
Alet v = vec![1, 2, 3];
Blet v = Vec::empty();
Clet mut v = Vec::new(); v.push(4);
Dlet v: Vec<i32> = Vec::new();
✗ Incorrect
Vec::empty() is not a valid method in Rust. Use Vec::new() instead.
Explain how to create a vector in Rust and add elements to it.
Think about starting empty or with values, and how to add more later.
You got /4 concepts.
Describe the difference between arrays and vectors in Rust.
Consider when you know the size ahead or need flexibility.
You got /4 concepts.
Practice
(1/5)
1. Which of the following correctly creates an empty vector of integers in Rust?
easy
A. let v: Vec<i32> = Vec::new();
B. let v = vec!();
C. let v = Vector::new();
D. let v = vec![0];
Solution
Step 1: Understand how to create an empty vector
In Rust, an empty vector is created using Vec::new() with the type specified if needed.
Step 2: Check each option
let v: Vec<i32> = Vec::new(); uses Vec::new() with type i32, which is correct. let v = vec!(); uses vec!() which is invalid syntax. let v = Vector::new(); uses Vector::new() which is not a Rust type. let v = vec![0]; creates a vector with one element 0, not empty.
Final Answer:
let v: Vec<i32> = Vec::new(); -> Option A
Quick Check:
Empty vector = Vec::new() [OK]
Hint: Empty vector uses Vec::new() with type [OK]
Common Mistakes:
Using vec!() with empty parentheses
Using Vector instead of Vec
Creating vector with one element instead of empty
2. Which syntax correctly creates a vector with the values 1, 2, and 3 in Rust?
easy
A. let v = vec{1, 2, 3};
B. let v = Vec[1, 2, 3];
C. let v = vec(1, 2, 3);
D. let v = vec![1, 2, 3];
Solution
Step 1: Recall vector creation with values
Rust uses the macro vec![...] with square brackets and exclamation mark to create vectors with initial values.
Step 2: Evaluate each option
let v = vec![1, 2, 3]; uses correct syntax vec![1, 2, 3]. let v = Vec[1, 2, 3]; uses square brackets without exclamation mark, invalid. let v = vec(1, 2, 3); uses parentheses, invalid. let v = vec{1, 2, 3}; uses curly braces, invalid.
Final Answer:
let v = vec![1, 2, 3]; -> Option D
Quick Check:
Vector with values = vec![...] [OK]
Hint: Use vec! macro with square brackets for values [OK]
Common Mistakes:
Missing exclamation mark after vec
Using parentheses or curly braces instead of brackets
Using Vec[...] which is invalid syntax
3. What is the output of this Rust code?
let mut v = Vec::new();
v.push(10);
v.push(20);
println!("{:?}", v);
medium
A. [10, 20]
B. [20, 10]
C. 10 20
D. Error: cannot push to vector
Solution
Step 1: Understand vector push operations
The vector starts empty. push(10) adds 10 at the end, then push(20) adds 20 after 10.
Step 2: Check the printed output
The println!("{:?}", v) prints the vector in debug format showing all elements in order: [10, 20].
Final Answer:
[10, 20] -> Option A
Quick Check:
Push adds elements in order [OK]
Hint: push adds elements at the end, print shows all [OK]
Common Mistakes:
Assuming push adds at the front
Printing vector without debug format
Confusing order of elements
4. Identify the error in this Rust code snippet:
let v = Vec::new();
v.push(5);
medium
A. push method does not exist for vectors
B. Vec::new() does not create a vector
C. Cannot push to an immutable vector
D. Missing semicolon after Vec::new()
Solution
Step 1: Check vector mutability
The vector v is declared without mut, so it is immutable by default.
Step 2: Understand push method requirements
The push method requires the vector to be mutable to add elements. Trying to push to an immutable vector causes a compile error.
Final Answer:
Cannot push to an immutable vector -> Option C
Quick Check:
push needs mutable vector [OK]
Hint: Add mut to vector to allow push [OK]
Common Mistakes:
Forgetting mut keyword
Thinking Vec::new() returns non-vector
Assuming push works on immutable vectors
5. You want to create a vector of strings from an array of &str slices and then add a new string. Which code correctly does this?
hard
A. let v = vec!["a", "b", "c"]; v.push("d");
B. let mut v: Vec<String> = ["a", "b", "c"].iter().map(|&s| s.to_string()).collect(); v.push("d".to_string());
C. let mut v = Vec::new(); v.push(["a", "b", "c"]); v.push("d");
D. let mut v = vec![String::from("a"), String::from("b"), String::from("c")]; v.push('d');
Solution
Step 1: Convert &str slices to String vector
The array of &str slices needs to be converted to a vector of String using iter(), map() with to_string(), then collect().
Step 2: Add a new String element
After creating a mutable vector, use push with "d".to_string() to add a new string.
Step 3: Check other options
let v = vec!["a", "b", "c"]; v.push("d"); tries to push to an immutable vector of &str slices, invalid. let mut v = Vec::new(); v.push(["a", "b", "c"]); v.push("d"); tries to push an array inside vector, invalid. let mut v = vec![String::from("a"), String::from("b"), String::from("c")]; v.push('d'); pushes a char instead of String, invalid.
Final Answer:
let mut v: Vec<String> = ["a", "b", "c"].iter().map(|&s| s.to_string()).collect(); v.push("d".to_string()); -> Option B
Quick Check:
Convert slices to String vector, then push String [OK]
Hint: Map &str to String, collect to Vec<String>, then push [OK]
Common Mistakes:
Trying to push to immutable vector
Pushing wrong types (char instead of String)
Not converting &str slices to String
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.