0Pricing
Learn Rust Coding · Lesson

Creating and Filling Vectors

Build Vecs and push items.

Creating and Filling 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 vector is a growable list of values, all of the same type. Unlike a fixed-size array, a vector can shrink or grow while your program runs.

In Rust the type is written Vec<T>, where T is the type of element it holds, like Vec<i32> for integers.

An Empty Vector

You can make a fresh, empty vector with Vec::new(). Because it has no values yet, Rust cannot guess the element type, so you usually annotate it.

Here we tell Rust this vector will hold i32 integers.

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

The vec! Macro

The quickest way to create a vector with starting values is the vec! macro. List the values inside square brackets.

Rust looks at the values to infer the element type, so no annotation is needed here.

fn main() {
    let nums = vec![10, 20, 30];
    println!("{:?}", nums);
}

Printing a Vector

A whole vector is printed with the debug formatter {:?}, not the normal {}. The debug form shows the values inside square brackets.

Use {:#?} for a pretty, multi-line layout when a vector is large.

fn main() {
    let names = vec!["Ann", "Bo", "Cy"];
    println!("{:?}", names);
}

Pushing Values

To add a value to the end of a vector, call push. The vector must be declared mut because pushing changes it.

Each push appends one item, growing the length by one.

fn main() {
    let mut v = Vec::new();
    v.push(1);
    v.push(2);
    v.push(3);
    println!("{:?}", v);
}

Type From the First Push

When you start with Vec::new() and no annotation, Rust waits for the first push to learn the element type.

Below, pushing 3.5 tells Rust this is a Vec<f64>. All later values must match that type.

fn main() {
    let mut prices = Vec::new();
    prices.push(3.5);
    prices.push(9.0);
    println!("{:?}", prices);
}

Filling With Repeats

The vec! macro can repeat a value. Write vec![value; count] to build a vector of that value repeated count times.

This is handy for setting up a list of zeros or default values.

fn main() {
    let zeros = vec![0; 5];
    println!("{:?}", zeros);
}

Filling in a Loop

You can fill a vector by pushing inside a loop. Here we add the squares of numbers 1 through 4.

Starting empty and pushing as you go is a common pattern when the values are computed.

fn main() {
    let mut squares = Vec::new();
    for n in 1..=4 {
        squares.push(n * n);
    }
    println!("{:?}", squares);
}

Capacity vs Length

Length is how many items a vector holds now. Capacity is how much room it has reserved before it needs to grow its memory.

If you know roughly how many items you will add, Vec::with_capacity(n) reserves space up front and avoids repeated reallocation.

fn main() {
    let mut v = Vec::with_capacity(10);
    v.push(1);
    println!("len {}, cap {}", v.len(), v.capacity());
}

From an Array

You can turn an array into a vector. One simple way is .to_vec(), which copies the array's elements into a new owned vector.

This is useful when you start with fixed data but need it to grow later.

fn main() {
    let arr = [1, 2, 3];
    let v = arr.to_vec();
    println!("{:?}", v);
}

Checking If Empty

Use is_empty() to check whether a vector has no elements. It returns a bool, which is clearer than comparing the length to zero.

This is a good guard before reading the first element.

fn main() {
    let v: Vec<i32> = Vec::new();
    if v.is_empty() {
        println!("nothing here yet");
    }
}

Quick Check

Test your understanding of creating and filling vectors.

Recap

You learned to create vectors with Vec::new() and the vec! macro, and to fill them using push, repeats, loops, and to_vec().

You also saw length versus capacity and how to check emptiness. Next you will read and loop over vector values.

Frequently asked questions

Is the “Creating and Filling Vectors” lesson free?

Yes — the full text of “Creating and Filling 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 “Creating and Filling Vectors”?

Build Vecs and push items. 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 “Creating and Filling 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. Creating and Filling Vectors
  2. Indexing and Iterating
  3. Growing and Shrinking
  4. Vectors of Structs
← Back to Learn Rust Coding