Challenge - 5 Problems
Vector Mastery in Rust
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Rust code creating a vector?
Consider this Rust code that creates a vector and prints it. What will be printed?
Rust
fn main() {
let v = vec![1, 2, 3, 4];
println!("{:?}", v);
}Attempts:
2 left
💡 Hint
Look at how the vector is printed with {:?} in println!
✗ Incorrect
Using {:?} in println! prints the vector in debug format with square brackets and commas.
❓ Predict Output
intermediateWhat is the length of the vector after this code runs?
Look at this Rust code that creates a vector and pushes elements. What is the length of the vector at the end?
Rust
fn main() {
let mut v = Vec::new();
v.push(10);
v.push(20);
v.push(30);
println!("{}", v.len());
}Attempts:
2 left
💡 Hint
Count how many times push is called.
✗ Incorrect
Each push adds one element, so after three pushes, length is 3.
🔧 Debug
advancedWhat error does this Rust code produce?
This Rust code tries to create a vector with a fixed size but fails. What error will it produce?
Rust
fn main() {
let v = vec![0;];
println!("{:?}", v);
}Attempts:
2 left
💡 Hint
Look carefully at the vec! macro syntax with semicolons.
✗ Incorrect
The vec![0;] macro expects a number after the semicolon to specify size, missing here causes syntax error.
❓ Predict Output
advancedWhat is the output of this Rust code using vector initialization?
What will this Rust program print?
Rust
fn main() {
let v = vec![5; 3];
println!("{:?}", v);
}Attempts:
2 left
💡 Hint
vec![value; count] creates a vector with count copies of value.
✗ Incorrect
vec![5; 3] creates a vector with three 5s: [5, 5, 5].
🧠 Conceptual
expertHow many elements does this vector contain after execution?
Analyze this Rust code snippet. How many elements does the vector contain after the loop finishes?
Rust
fn main() {
let mut v = Vec::new();
for i in 0..5 {
if i % 2 == 0 {
v.push(i);
}
}
println!("{}", v.len());
}Attempts:
2 left
💡 Hint
Count how many numbers from 0 to 4 are even.
✗ Incorrect
The loop pushes only even numbers: 0, 2, 4, so length is 3.
