0Pricing
Learn Rust Coding · Lesson

Vectors

Growable arrays.

Vectors is a free Learn Rust Coding lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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);
}

Adding Elements

Use push to append an element to the end. The vector must be mut to change it.

fn main() {
    let mut v = Vec::new();
    v.push(10);
    v.push(20);
    v.push(30);
    println!("{:?}", v);
}

Removing Elements

pop removes and returns the last element wrapped in an Option. It returns None if the vector is empty.

fn main() {
    let mut v = vec![1, 2, 3];
    let last = v.pop();
    println!("removed {:?}, now {:?}", last, v);
}

Accessing by Index

Index a vector with square brackets, just like an array. An out-of-range index causes a panic.

fn main() {
    let v = vec!["a", "b", "c"];
    println!("first {}", v[0]);
    println!("third {}", v[2]);
}

Safe Access with get

The get method returns an Option instead of panicking, which is safer for unknown indices.

fn main() {
    let v = vec![10, 20, 30];
    match v.get(5) {
        Some(x) => println!("found {}", x),
        None => println!("no element there"),
    }
}

Iterating a Vector

Loop over a vector's elements with a for loop. Use & to borrow each element without taking ownership.

fn main() {
    let v = vec![100, 200, 300];
    for item in &v {
        println!("item {}", item);
    }
}

Mutating While Iterating

Iterate with &mut to modify each element in place. Dereference with * to change the value.

fn main() {
    let mut v = vec![1, 2, 3];
    for item in &mut v {
        *item *= 10;
    }
    println!("{:?}", v);
}

Vector Length and Emptiness

Use len() for the number of elements and is_empty() to check if there are none.

fn main() {
    let v = vec![5, 6];
    println!("len {} empty {}", v.len(), v.is_empty());
}

Common Vector Methods

Vectors offer many helpers:

  • contains — check membership.
  • first / last — get ends as Option.
  • sort — order the elements.
fn main() {
    let mut v = vec![3, 1, 2];
    v.sort();
    println!("{:?} contains 2: {}", v, v.contains(&2));
}

Summing with Iterators

Combine vectors with iterator methods to compute totals concisely.

fn main() {
    let v = vec![10, 20, 30, 40];
    let total: i32 = v.iter().sum();
    println!("total {}", total);
}

Quick Check

Recall the method that grows a vector.

Recap

Vectors in Rust:

  • Vec<T> is a growable, same-type list.
  • Create with Vec::new() or vec![...].
  • push adds, pop removes from the end.
  • Access with [] or safely with get.
  • Iterate with for x in &v.
fn main() {
    let mut scores = vec![80, 90];
    scores.push(100);
    let avg: i32 = scores.iter().sum::<i32>() / scores.len() as i32;
    println!("average {}", avg);
}

Frequently asked questions

Is the “Vectors” lesson free?

Yes — the full text of “Vectors” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.

What will I learn in “Vectors”?

Growable arrays. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn Rust Coding?

No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Vectors” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn Rust Coding lesson?

Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

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