std-method

Vec::push

Append an owned value at the end. The vector takes ownership of a non-Copy value; growth may reallocate. The method returns unit. Expected stdout (not executed): ["Ada", "Lin"] Common mistake: Keeping a reference to an element live across a push on the same vector.

Formal Syntax & Structure

rust syntaxrust
pub fn push(&mut self, value: T)

Working Example

Example Snippetrust
fn main() {    let mut names = vec![String::from("Ada")];    let next = String::from("Lin");    names.push(next);    println!("{names:?}");}
Rust Reference