Vectors
Rust is a systems programming language focused on speed, memory safety, and fearless concurrency — and the vector, Vec<T> , is its standard growable list.
Learn Vectors in our free Rust course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Rust course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll create vectors, push and pop elements, index safely, and iterate over them by shared and mutable reference.
What You'll Learn in This Lesson
1️⃣ Creating and Accessing Vectors
You make a vector with Vec::new() and grow it with push , or build it directly with the vec! macro. Access elements either with [i] (fast, but panics out of range) or .get(i) (returns an Option , never panics).
Notice .get(99) returned None instead of crashing — that's the safe way to access an index that might not exist, building on the Option you learned last lesson.
2️⃣ Iterating and Modifying
Loop over a vector with for n in &vec to read, or for n in &mut vec to modify — using *n to write through the mutable reference. Methods like pop , contains , and is_empty round out everyday use.
The *n *= 10 line is the key detail: in a &mut loop each item is a reference, so you dereference with * to change the underlying value.
Your turn. Fill in the blanks marked ___ , then run it.
Compute the count, sum, and max of a vector. Run it with cargo run and check the output.
📋 Quick Reference — Vectors
Practice quiz
What is a Vec<T> in Rust?
- A fixed-size array
- A tuple
- A growable list stored on the heap
- A trait
Answer: A growable list stored on the heap. Vec<T> is a heap-allocated, resizable list of same-typed values.
Which method adds an element to the end of a vector?
- .push(x)
- .add(x)
- .append_one(x)
- .insert_end(x)
Answer: .push(x). push appends a single element to the end of the vector.
What does .pop() return?
- The first element
- The vector length
- Nothing
- An Option holding the last element
Answer: An Option holding the last element. pop removes and returns the last element as Some(value), or None if empty.
Which macro builds a vector with initial values?
The vec! macro is the common shortcut for building a vector with values.
What happens when you index a vector out of range with v[i]?
- The program panics
- It returns None
- It returns 0
- It grows the vector
Answer: The program panics. Indexing with [i] panics if i is out of range.
What does v.get(99) return when index 99 does not exist?
- Some(0)
- A panic
- None
- The last element
Answer: None. .get(i) returns an Option, giving None safely instead of panicking.
In 'for n in &mut vec', why write *n to change an element?
- * prints the value
- n is a mutable reference; * writes through it
- * removes the element
- It is optional styling
Answer: n is a mutable reference; * writes through it. Each n is a &mut i32, so you dereference with * to assign to the underlying value.
What must a vector be declared as to push onto it?
- let
- const
- static
- let mut
Answer: let mut. Mutating or pushing requires the vector to be mutable (let mut).
Can a Vec<T> hold values of different types at once?
- Yes, any types
- No, all elements share type T
- Only numbers
- Only two types
Answer: No, all elements share type T. A Vec is single-type; use an enum or Vec<Box<dyn Trait>> for mixed data.
What does vec![4, 8, 15].iter().max() return?
- 15
- None
- Some(&15)
- Some(&4)
Answer: Some(&15). iter().max() returns an Option<&i32>, here Some(&15).