What is a Vector?
Part of the Logic & Flow section of Coddy's Rust journey — lesson 7 of 66.
In Rust, you have two main ways to store collections of data: arrays and vectors. While arrays have a fixed size that must be known at compile time, vectors are dynamic collections that can grow and shrink during program execution.
A vector, written as Vec<T>, is Rust's growable array type. The T represents the type of elements the vector will hold - for example, Vec<i32> for integers or Vec<String> for strings. Unlike arrays where you must specify the exact number of elements upfront, vectors can start empty and have elements added or removed as your program runs.
Here's when you'd choose a vector over an array:
// Array - fixed size, known at compile time
let scores = [85, 92, 78, 90]; // always exactly 4 elements
// Vector - dynamic size, can change during runtime
let mut grades = Vec::new(); // starts empty, can growVectors are perfect when you don't know how many items you'll need in advance, such as storing user input, reading data from files, or building lists that change based on program logic. They provide the flexibility that arrays cannot offer while maintaining efficient access to elements.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Cheat sheet
Vectors (Vec<T>) are dynamic collections that can grow and shrink during program execution, unlike arrays which have a fixed size known at compile time.
The T represents the type of elements the vector will hold:
Vec<i32> // vector of integers
Vec<String> // vector of stringsCreating a vector:
let mut grades = Vec::new(); // starts empty, can growComparison with arrays:
// Array - fixed size
let scores = [85, 92, 78, 90]; // always exactly 4 elements
// Vector - dynamic size
let mut grades = Vec::new(); // can change during runtimeUse vectors when you don't know how many items you'll need in advance, such as storing user input, reading data from files, or building lists that change based on program logic.
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Control Flow
The 'match' ExpressionMatching Multiple ValuesMatching RangesThe 'if let' ExpressionLoops as ExpressionsRecap - Simple Command Parser4Grouping Data with Structs
What is a Struct?Structs OverviewAccessing Struct FieldsMutable StructsStructs as Function ParametersTuple StructsRecap - Create a Book Struct7Handling Errors with 'Result'
What is a 'Result'?Using 'match' with 'Result'is_ok() and is_err()Shortcuts: unwrap and expectThe Question Mark Operator '?'Parsing Strings to NumbersRecap - Safe Division Function10Closures & Anonymous Functions
What is a Closure?Defining a Simple ClosureClosures with ParametersCapturing the EnvironmentRecap - Simple Adder Closure2Introduction to Vectors
What is a Vector?Creating a VectorAdding Elements with pushAccessing Vector ElementsIterating Over a VectorMutable IterationRemoving ElementsRecap - Basic Score Tracker5Key-Value Pairs with Hash Maps
What is a Hash Map?Creating a Hash MapInserting Key-Value PairsAccessing ValuesIterating Over a Hash MapUpdating a ValueRemoving a PairRecap - Word Counter8Project: Simple Item Inventory
Project SetupAdding an ItemChecking StockSelling an ItemPutting it all together