Start learning free

LESSON 95 OF 348 · ARRAYS I

Acessing vector elements in Rust in Rust

Lesson

We can access the data inside a Vec using indices and the bracket notation ([]), just like we do for static arrays.

Vectors also use zero-based indexing, so the first element in a vector has an index of 0.

let array = vec![50, 60, 70];

println!("{}", array[0]); // Output: 50

let data = array[1];
println!("{}", data); // Output: 60

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 3
    Ctrl + Enter to check

    Accessing Elements 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