Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to remove the last element from the vector.
DSA Rust
let mut numbers = vec![1, 2, 3, 4, 5]; numbers.[1](); println!("{:?}", numbers);
Attempts:
3 left
💡 Hint
Common Mistakes
Using push() instead of pop() adds elements instead of removing.
Using remove() requires an index, not suitable for just last element.
✗ Incorrect
The pop() method removes the last element from the vector.
2fill in blank
mediumComplete the code to safely remove the last element and print it.
DSA Rust
let mut letters = vec!["a", "b", "c"]; if let Some(last) = letters.[1]() { println!("Removed: {}", last); } println!("{:?}", letters);
Attempts:
3 left
💡 Hint
Common Mistakes
Using remove() without index causes error.
Using push() adds elements instead of removing.
✗ Incorrect
pop() returns an Option with the last element removed, allowing safe handling.
3fill in blank
hardFix the error in the code to remove the last element from the vector.
DSA Rust
let mut nums = vec![10, 20, 30]; nums.[1](nums.len() - 1); println!("{:?}", nums);
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() with an argument causes error.
Using remove() with index equal to length causes panic.
✗ Incorrect
remove() is the method that removes by index. nums.len() - 1 gives the index of the last element.
4fill in blank
hardFill both blanks to remove the last element by index safely.
DSA Rust
let mut data = vec![100, 200, 300]; if data.len() > 0 { data.[1](data.[2]() - 1); } println!("{:?}", data);
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() with index causes error.
Using len() without subtracting 1 causes out-of-bounds panic.
✗ Incorrect
remove() deletes element at given index; len() returns vector length, so len() - 1 is last index.
5fill in blank
hardFill the blank to remove the last element and print it if exists.
DSA Rust
let mut items = vec!["x", "y", "z"]; if let Some(removed) = items.[1]() { println!("Removed: {}", removed); } println!("Remaining: {:?}", items);
Attempts:
3 left
💡 Hint
Common Mistakes
Using remove() without index causes error.
Using push() or insert() adds elements instead of removing.
✗ Incorrect
pop() removes and returns the last element safely as an Option.
