The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

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

TODO: Pull subtitle into page object
Code
fn main() {
  let mut words = vec!["bravo", "charlie", "delta"];
  words.insert(0, "alfa");
  dbg!(words);
}
Results
[neopolitan_code_run:4] words = [
    "alfa",
    "bravo",
    "charlie",
    "delta",
]

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

The first value passed to `.insert()`` is the index position to put the new element. All the rest of the elements are shifted to the right. By using `0`` the new element becomes the first one with the rest of the elements shifted down.

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

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

-- code
-- rust

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

-- results/

[neopolitan_code_run:4] words = [
    "alfa",
    "bravo",
    "charlie",
    "delta",
]

-- /results

-- p

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

The first value passed to `.insert()`` is the
index position to put the new element. All
the rest of the elements are shifted to 
the right. By using `0`` the new element
becomes the first one with the rest of 
the elements shifted down.


-- categories
-- Rust 

-- metadata
-- date: 2023-12-09 12:51:43
-- id: 2zjdq5t8
-- site: aws
-- type: post
-- status: published