Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

Add An Element To The Front Of A Rust Vec With .insert()

rust
fn main() {
  let mut words = vec!["bravo", "charlie", "delta"];
  words.insert(0, "alfa");
  dbg!(words);
}
results start

This is like "unshift" in other languages that pushing a new element onto the front of a Vec.

The first value passed to [TODO: Code shorthand span ] is the index position to put the new element. All the rest of the elements are shifted to the right. By using [TODO: Code shorthand span ] the new element becomes the first one with the rest of the elements shifted down.