#111
Apr 30, 2026
111. Vec::insert_mut — Splice In and Edit Without Reindexing
You insert a placeholder, then index back to fix it up. Two lookups, two bounds checks, one wobble. Vec::insert_mut — stable in 1.95 — hands you the &mut T directly.
The classic dance:
| |
insert_mut returns the slot:
| |
This shines when the value is built in pieces — push a default, then fill it in based on where it landed:
| |
Rust 1.95 grew the whole _mut family alongside Vec::push_mut: Vec::insert_mut, VecDeque::push_front_mut, VecDeque::push_back_mut, VecDeque::insert_mut, LinkedList::push_front_mut, and LinkedList::push_back_mut. Same idea everywhere — the place-it method now returns a mutable reference to the slot it just placed.
| |
Quietly useful, no churn — just fewer indices floating around.