Rust Tutorial: Vectors
Vec<T> — Rust's growable array. Create, modify, iterate, and transform vectors.
The workhorse collection
`Vec<T>` is a heap-allocated, growable array. It is the most common collection in Rust programs.
What you'll learn in this Rust vectors tutorial
This interactive Rust tutorial has 8 hands-on exercises. Estimated time: 18 minutes.
- Create a Vec — Use `vec![]` macro or `Vec::new()` + `push`.
- Access elements — Index with `[]` (panics if out of bounds) or `.get()` (returns Option).
- Iterate a Vec — `for x in &v` iterates by reference. `for x in v` consumes the Vec.
- push and pop — `push` adds to the end. `pop` removes the last element and returns Option.
- filter and map — `.iter().filter().map().collect()` transforms a Vec without a loop.
- sort and dedup — `sort()` sorts in place. `dedup()` removes consecutive duplicates.
- len, is_empty, contains — Common Vec predicates.
- Vec of structs — Vecs can hold any type, including your custom structs.
Rust Vectors concepts covered
- The workhorse collection