- 01Setting up (what you'll need)
- 02Your first output (println!)
- 03Variables and mutability (let / mut)
- 04Type annotations and basic types
- 05Branching (the if statement)
- 06Repeating (the for loop)
- 07Writing your own function
- 08Repeating (while and loop)
- 09Ownership, the basics
- 10Borrowing (references, &)
- 11Working with vectors (Vec)
- 12Structs
- 13enum and the match expression
- 14The Option type (null safety)
- 15The Result type and error handling
- 16Traits
- 17Generics
- 18Closures
- 19Working with strings (String)
- 20[Project] Build a small inventory system
Working with vectors (Vec)
This lesson covers the vector (Vec), an array whose length you can change freely.
Vec<type> is the growable array provided by Rust's standard library. push() adds an element and len() gives you the number of elements.
The sample code creates let mut fruits: Vec<String> = Vec::new();, adds fruit with push(), and then prints every element with a for loop.
A common early stumble is forgetting mut when you need to add elements. Any operation that changes the contents of a vector (such as push) requires the variable itself to be mut.
In professional work, the basic guideline is an array for a fixed number of items and a vector for data that grows and shrinks while the program runs.
๐งช This site can't compile or run Rust directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ nothing is sent anywhere).
