Growing and Shrinking
Insert, remove, and resize.
Growing and Shrinking is a free Learn Rust Coding lesson on CoddyKit — lesson 3 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.
Vectors Can Change Size
A key feature of vectors is that they change size at runtime. You can add elements when you have more data and remove them when you no longer need them.
Every size change requires the vector to be declared mut.
Growing With push
The push method adds a single element to the end, growing the vector by one. This is the most common way to grow.
Below the vector grows from length 0 to length 3.
fn main() {
let mut v = Vec::new();
v.push("a");
v.push("b");
v.push("c");
println!("len = {}", v.len());
}Removing With pop
The pop method removes and returns the last element wrapped in an Option. You get Some(value) normally, or None if the vector was already empty.
This shrinks the vector by one.
fn main() {
let mut v = vec![1, 2, 3];
let last = v.pop();
println!("popped {:?}", last);
println!("now {:?}", v);
}Inserting in the Middle
The insert method places a value at a given index and shifts later elements to the right. The index must be from 0 up to the current length.
Here we slip 99 into position 1.
fn main() {
let mut v = vec![10, 20, 30];
v.insert(1, 99);
println!("{:?}", v);
}Removing by Index
The remove method deletes the element at an index, returns it, and shifts later elements left to fill the gap.
Beware: removing an index that does not exist panics.
fn main() {
let mut v = vec![10, 20, 30];
let gone = v.remove(0);
println!("removed {}", gone);
println!("{:?}", v);
}Fast Removal With swap_remove
When order does not matter, swap_remove is faster. It moves the last element into the removed slot instead of shifting everything.
This changes the order but avoids the cost of shifting.
fn main() {
let mut v = vec![1, 2, 3, 4];
let x = v.swap_remove(0);
println!("took {}", x);
println!("{:?}", v);
}Clearing Everything
The clear method removes all elements at once, leaving an empty vector with length 0. The vector keeps its allocated capacity for reuse.
It is handy when you want to refill a vector from scratch.
fn main() {
let mut v = vec![1, 2, 3];
v.clear();
println!("empty? {}", v.is_empty());
}Truncating to a Length
The truncate method shortens a vector to a chosen length by dropping the extra elements at the end. If the vector is already shorter, nothing happens.
Here we keep only the first two elements.
fn main() {
let mut v = vec![1, 2, 3, 4, 5];
v.truncate(2);
println!("{:?}", v);
}Joining With extend
The extend method appends every item from another collection. It is like calling push for each element in one step.
Below a second list is added to the end of the first.
fn main() {
let mut v = vec![1, 2];
v.extend(vec![3, 4, 5]);
println!("{:?}", v);
}Keeping Only Some With retain
The retain method keeps only the elements that pass a test, removing the rest in place. You give it a closure that returns a bool.
Here we keep only the even numbers.
fn main() {
let mut v = vec![1, 2, 3, 4, 5, 6];
v.retain(|n| n % 2 == 0);
println!("{:?}", v);
}Sorting Elements
The sort method reorders elements from smallest to largest in place. It does not change the length, just the arrangement.
Sorting needs the vector to be mut.
fn main() {
let mut v = vec![3, 1, 4, 1, 5];
v.sort();
println!("{:?}", v);
}Quick Check
Test your understanding of growing and shrinking vectors.
Recap
You grew vectors with push, insert, and extend, and shrank them with pop, remove, swap_remove, clear, and truncate.
You also filtered with retain and ordered with sort. Next you will store structs inside vectors.
Frequently asked questions
Is the “Growing and Shrinking” lesson free?
Yes — the full text of “Growing and Shrinking” 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 “Growing and Shrinking”?
Insert, remove, and resize. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Growing and Shrinking” 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
- Creating and Filling Vectors
- Indexing and Iterating
- Growing and Shrinking
- Vectors of Structs