Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to iterate forward over the vector and print each element.
DSA Rust
let numbers = vec![1, 2, 3, 4, 5]; for num in numbers.[1]() { println!("{}", num); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using into_iter() consumes the vector, which is not needed here.
Using iter_mut() allows mutation, but we only want to read.
Using drain() removes elements, which is not intended.
✗ Incorrect
Using iter() allows us to traverse the vector forward by borrowing each element immutably.
2fill in blank
mediumComplete the code to iterate backward over the vector and print each element.
DSA Rust
let numbers = vec![10, 20, 30, 40]; for num in numbers.iter().[1]() { println!("{}", num); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using next() advances forward one element only.
Using skip() skips elements but does not reverse.
Using take() limits elements but does not reverse.
✗ Incorrect
The rev() method reverses the iterator, allowing backward traversal.
3fill in blank
hardFix the error in the code to correctly iterate backward over a mutable vector and modify each element.
DSA Rust
let mut numbers = vec![1, 2, 3]; for num in numbers.[1]().[2]() { *num += 1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using iter() does not allow modification.
Using into_iter() consumes the vector and may not allow mutable references.
Using drain() removes elements.
✗ Incorrect
iter_mut() allows mutable access to elements, and rev() reverses the iterator for backward traversal.
4fill in blank
hardFill both blanks to create a vector of squares of numbers greater than 3, iterating forward.
DSA Rust
let numbers = vec![1, 2, 3, 4, 5]; let squares: Vec<_> = numbers.[1]() .filter(|&x| x [2] 3) .map(|x| x * x) .collect();
Attempts:
3 left
💡 Hint
Common Mistakes
Using iter_mut() when no mutation is needed.
Using < instead of > in the filter condition.
✗ Incorrect
iter() creates an iterator over references, and > filters numbers greater than 3.
5fill in blank
hardFill all three blanks to create a reversed vector of doubled numbers less than 5.
DSA Rust
let numbers = vec![1, 2, 3, 4, 5]; let result: Vec<_> = numbers.[1]() .filter(|&x| x [2] 5) .map(|x| x * 2) .[3]() .collect();
Attempts:
3 left
💡 Hint
Common Mistakes
Using iter_mut() when no mutation is needed.
Using > instead of < in the filter.
Forgetting to reverse with rev().
✗ Incorrect
iter() creates an iterator, < filters numbers less than 5, and rev() reverses the iterator.
