Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Mutable vector of numbers, append (push) values

  • mut

  • push

  • append

  • We can make a vector mutable by using the mut keyword.

  • We can append an element to the end of the vector using the push method.

fn main() {
    let mut numbers = vec![10, 11, 12];
    println!("{}", numbers.len());
    println!("{:?}", numbers);

    numbers.push(23);

    println!("{}", numbers.len());
    println!("{:?}", numbers);
}
3
[10, 11, 12]
4
[10, 11, 12, 23]