Challenge - 5 Problems
Array Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Rust array initialization?
Consider the following Rust code snippet. What will be printed after execution?
DSA Rust
fn main() {
let arr = [5; 4];
for i in 0..arr.len() {
print!("{} ", arr[i]);
}
}Attempts:
2 left
💡 Hint
The syntax [value; count] creates an array with 'count' elements, each initialized to 'value'.
✗ Incorrect
The code creates an array of length 4 where each element is 5. The loop prints each element separated by space.
❓ Predict Output
intermediateWhat is the output after modifying an array element?
What will be printed by this Rust program?
DSA Rust
fn main() {
let mut arr = [1, 2, 3, 4];
arr[2] = 10;
for val in arr.iter() {
print!("{} ", val);
}
}Attempts:
2 left
💡 Hint
Arrays declared with 'mut' can have their elements changed.
✗ Incorrect
The third element (index 2) is changed from 3 to 10 before printing all elements.
❓ Predict Output
advancedWhat error does this Rust code produce?
What error will this Rust code cause when compiled?
DSA Rust
fn main() {
let arr = [1, 2, 3];
let x = arr[3];
println!("{}", x);
}Attempts:
2 left
💡 Hint
Array indices start at 0 and go up to length-1.
✗ Incorrect
Accessing arr[3] is out of bounds since arr has indices 0,1,2. Rust panics at runtime for this.
🧠 Conceptual
advancedHow many elements does this Rust array have?
Given the declaration: let arr = [0; 7]; How many elements are in arr?
Attempts:
2 left
💡 Hint
The syntax [value; count] creates an array with 'count' elements.
✗ Incorrect
The array has 7 elements, each initialized to 0.
❓ Predict Output
expertWhat is the output of this Rust array slice and iteration?
What will this Rust program print?
DSA Rust
fn main() {
let arr = [2, 4, 6, 8, 10];
let slice = &arr[1..4];
for val in slice.iter() {
print!("{} ", val);
}
}Attempts:
2 left
💡 Hint
Slices use start index inclusive and end index exclusive.
✗ Incorrect
The slice &arr[1..4] includes elements at indices 1, 2, and 3, which are 4, 6, and 8.
