88. Vec::push_mut — Push and Modify in One Step
Tired of pushing a default into a Vec and then grabbing a mutable reference to fill it in? Rust 1.95 stabilises push_mut, which returns &mut T to the element it just inserted.
The old dance
A common pattern is to push a placeholder value and then immediately mutate it. Before push_mut, you had to do an awkward two-step:
| |
That last_mut().unwrap() is boilerplate — you know the element is there because you just pushed it. Worse, in more complex code the compiler can’t always prove the reference is safe, forcing you into index gymnastics.
Enter push_mut
push_mut pushes the value and hands you back a mutable reference in one shot:
| |
No unwraps, no indexing, no second lookup. The borrow checker is happy because there’s a clear chain of ownership.
Building structs in-place
This really shines when you’re constructing complex values piece by piece:
| |
Instead of building the struct fully before pushing, you push a default and fill it in. This can be handy when the final field values depend on context you only have after insertion.
Also works for insert_mut
Need to insert at a specific index? insert_mut follows the same pattern:
| |
Both methods are also available on VecDeque (push_front_mut, push_back_mut, insert_mut) and LinkedList (push_front_mut, push_back_mut).
When to reach for it
Use push_mut whenever you’d otherwise write push followed by last_mut().unwrap(). It’s one less unwrap, one fewer line, and clearer intent: push this, then let me tweak it.
Stabilised in Rust 1.95 (April 2026) — update your toolchain and give it a spin.