0Pricing
Learn Rust Coding · Lesson

Vectors

Growable arrays.

What Is a Vector?

A Vec<T> is a growable array. Unlike a fixed-size array, a vector can grow or shrink at runtime.

All elements must be the same type T.

fn main() {
    let v: Vec<i32> = Vec::new();
    println!("empty vector with length {}", v.len());
}

Creating with vec!

The vec! macro is the easiest way to create a vector with initial values.

fn main() {
    let numbers = vec![1, 2, 3, 4];
    println!("{:?}", numbers);
}

All lessons in this course

  1. Vectors
  2. Strings and &str
  3. HashMaps
  4. Iterating Collections
← Back to Learn Rust Coding