Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print each element of the array.
DSA Rust
let arr = [10, 20, 30, 40]; for [1] in arr.iter() { println!("{}", [1]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not consistent inside the loop.
Trying to use the index instead of the element.
✗ Incorrect
The variable 'i' is used to represent each element when iterating over the array.
2fill in blank
mediumComplete the code to sum all elements in the array.
DSA Rust
let arr = [1, 2, 3, 4, 5]; let mut sum = 0; for &[1] in arr.iter() { sum += [1]; } println!("Sum: {}", sum);
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to dereference the element with '&'.
Using a variable name inconsistent inside the loop.
✗ Incorrect
The variable 'num' holds each element's value during iteration and is used to add to sum.
3fill in blank
hardFix the error in the code to print elements with their indices.
DSA Rust
let arr = [5, 10, 15]; for ([1], val) in arr.iter().enumerate() { println!("Index: {}, Value: {}", [1], val); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the loop and println! macro.
Not using enumerate() to get indices.
✗ Incorrect
The variable 'idx' is used to hold the index from enumerate and must be consistent in the println! macro.
4fill in blank
hardFill both blanks to create a vector of squares of even numbers from the array.
DSA Rust
let arr = [1, 2, 3, 4, 5, 6]; let evens_squared: Vec<i32> = arr.iter() .filter(|&&[1]| [1] % 2 == 0) .map(|&[2]| [2] * [2]) .collect();
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in filter and map.
Forgetting to dereference elements properly.
✗ Incorrect
The variable 'num' is used consistently in both filter and map closures to represent each element.
5fill in blank
hardFill all three blanks to create a hashmap counting occurrences of each number in the array.
DSA Rust
use std::collections::HashMap; let arr = [1, 2, 2, 3, 3, 3]; let mut counts = HashMap::new(); for &[1] in arr.iter() { let counter = counts.entry([2]).or_insert([3]); *counter += 1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for the element and hashmap key.
Initializing the count to 1 instead of 0.
✗ Incorrect
The variable 'num' is used for the element in the loop and as the key in the hashmap. The initial count is set to 0 before incrementing.
