Start learning free

LESSON 101 OF 348 · ARRAYS I

Appending Item to Vec in Rust in Rust

Lesson

We can add elements to the end of a Vec using the .push() method.

.push() takes one parameter and "pushes" it onto the end of the Vec:

let mut arr1: Vec<i32> = vec![1, 2, 3];
arr1.push(4);

// arr1 is now [1, 2, 3, 4]

Assignment
Follow the Coding Tutorial and let's play with some arrays.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    Array Push in Rust

    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free