Skip to content
Justice Tech School

Vectors

Vec<T> is a growable array. Use push, pop, len, get and iteration.

Example
fn main() {
    let mut v = vec![1, 2, 3];
    v.push(4);
    let last = v.pop();
    println!("{:?} {:?} {:?}", v, last, v.get(10));
    v.extend([7, 8]);
    println!("{} {:?}", v.len(), v);
}

Result

[1, 2, 3] Some(4) None
5 [1, 2, 3, 7, 8]

Exercise

Add an item.

v.(5)

Quiz Test

What does get(10) return?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.