Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add an element to the end of the vector.
DSA Rust
let mut numbers = vec![1, 2, 3]; numbers.[1](4);
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of push removes an element instead of adding.
Using insert requires an index, not just a value.
✗ Incorrect
The push method adds an element to the end of the vector.
2fill in blank
mediumComplete the code to print the last element after insertion.
DSA Rust
let mut nums = vec![10, 20, 30]; nums.push(40); println!("{}", nums.[1]().unwrap());
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop removes the element instead of just accessing it.
Using first accesses the first element, not the last.
✗ Incorrect
The last method returns a reference to the last element in the vector.
3fill in blank
hardFix the error in the code to insert 50 at the end of the vector.
DSA Rust
let mut vals = vec![5, 15, 25]; vals.[1](50);
Attempts:
3 left
💡 Hint
Common Mistakes
Using insert with arguments in wrong order causes error.
Using append incorrectly to add a single element.
✗ Incorrect
push takes only one argument and adds the element at the end. insert requires index first, then value.
4fill in blank
hardFill both blanks to create a vector and add 100 at the end.
DSA Rust
let mut data = [1]![10, 20, 30]; data.[2](100);
Attempts:
3 left
💡 Hint
Common Mistakes
Using array instead of vec! macro causes type errors.
Using insert instead of push requires an index.
✗ Incorrect
vec! macro creates a vector; push adds an element at the end.
5fill in blank
hardFill all three blanks to create a vector, add 5, and print the last element.
DSA Rust
let mut numbers = [1]![1, 2, 3]; numbers.[2](5); println!("{}", numbers.[3]().unwrap());
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop removes the last element instead of accessing it.
Forgetting to unwrap the Option returned by last.
✗ Incorrect
vec! creates vector, push adds element, last returns reference to last element.
