std-method

Vec::insert

Place an element at the given position and shift the suffix right. Inserting at len appends; an index greater than len panics. Expected stdout (not executed): ["test", "review", "ship"] Common mistake: Using insert for overwriting an existing element rather than increasing length.

Formal Syntax & Structure

rust syntaxrust
pub fn insert(&mut self, index: usize, element: T)

Working Example

Example Snippetrust
fn main() {    let mut tasks = vec!["test", "ship"];    tasks.insert(1, "review");    println!("{tasks:?}");}
Rust Reference