How to Use Vec<T> (Vectors) in Rust

The Complete Guide

Create a Rust vector with Vec::new() or vec![], add items with push(), and access them by index.

Use Vec::new() to create an empty vector or the vec! macro to initialize one with values, then use .push() to add items and indexing to access them.

fn main() {
    let mut v: Vec<i32> = Vec::new();
    v.push(1);
    v.push(2);
    let v2 = vec![1, 2, 3];
    println!("{}", v2[0]);
}