Problem overview
The function undo_last_add receives a shopping list items, a Vec<String>, and two words, first and second. It adds first to the end, adds second to the end, removes the last item as an "undo", and returns the list.
For items = ["milk", "eggs"], first = "bread" and second = "jam", the list becomes ["milk", "eggs", "bread", "jam"] after the adds, and the undo removes "jam". The result is ["milk", "eggs", "bread"].
Approach
A Vec grows and shrinks at the end cheaply, and Rust gives you a method for each direction: push appends an element and pop removes the last one. The task is just those two methods in order, so there is no searching and no index arithmetic.
The undo removes the last position, not "the word second". That matters when the same word is already in the list. With items = ["water"], first = "juice" and second = "water", the list becomes ["water", "juice", "water"], and pop removes the final "water". The result is ["water", "juice"], with the original "water" untouched.
Step by step
- Push
first. It goes after every item already in the vector. On an empty list it becomes the only item. - Push
second. It is now the last element. - Pop. This removes the last element, which is
second, wherever else the same text appears. - Return
itemsas the last expression, without a semicolon.
The solution in Rust
impl Solution {
fn undo_last_add(mut items: Vec<String>, first: String, second: String) -> Vec<String> {
items.push(first);
items.push(second);
items.pop();
items
}
}
A few Rust details are at work here:
mut itemsin the signature. Parameters are immutable by default. Writingmutbefore the name lets the function change its own vector. The caller's variable is not affected, because the vector was moved into the function.- Moving the words.
push(first)moves theStringinto the vector.firstcan't be used afterwards, and the code doesn't need to. popreturns anOption. It givesSome(value)for the removed element andNoneon an empty vector. Here the value is thrown away, so the statementitems.pop();just ends with a semicolon. The vector is never empty at that point, since two words were just pushed.- The final expression.
itemshas no semicolon, so it is the value the function returns.
What pop does on an empty vector
This function never pops from an empty vector, but it helps to know what would happen. Popping an empty Vec<String> doesn't panic: it returns None. After one push("x".to_string()), two pops in a row give Some("x") and then None. That is why pop returns an Option rather than a bare value, and why code that uses the removed element has to handle both cases.
Edge cases and common mistakes
- An empty list. With
items = [],first = "apples"andsecond = "pears"the function returns["apples"]. - Removing by value. Looking for
secondin the vector and removing the first match would delete the wrong"water"in the example above.popalways takes the end. - Forgetting the return. A function whose last line ends in
;returns(), and the compiler reports a type mismatch withVec<String>. - Swapping the pushes. If
secondis pushed first, the undo removesfirstand the result ends with the wrong word.
Testing
["milk", "eggs"],"bread","jam"gives["milk", "eggs", "bread"].[],"apples","pears"gives["apples"].["water"],"juice","water"gives["water", "juice"].["a"],"b","c"gives["a", "b"].
