By the end of this lesson
Store a sequence and handle an index that may be missing.
Before you start
- Rust 1.90.0 or later, a terminal and a plain-text editor. Examples were tested with Rust 1.90.0, Edition 2024 on Linux.
- Complete lesson 8, or be comfortable explaining and running its examples.
Three words to know
- Array
- A fixed-length collection with one element type.
- Vec
- A growable collection with one element type.
- Index
- A position in a sequence, starting at zero.
1. Understand the idea
Arrays have a fixed length in their type; Vec can grow. Direct indexing panics if the index is out of bounds. get returns Option instead, allowing you to handle a missing value. Choose indexing only when your own logic has established that the position exists. Elements of one vector share a type.
2. Save, run and observe
Save this complete program as lesson.rs in a fresh practice folder. It replaces that lesson file only; keep earlier examples separately. Compile it with rustc, then run the executable. Rust source goes in the editor; rustc and cargo commands go in the terminal.
fn main() {
let mut pages = vec![10, 20];
pages.push(30);
println!("length={}", pages.len());
println!("missing={}", pages.get(9).is_none());
}
What you should see · example output
length=3 missing=true
rustc --edition=2024 lesson.rs -o lesson
./lesson3. Explain the result
push changes the vector, so pages needs mut. get(9) does not find an element in a three-item vector and returns None. is_none checks this without dereferencing a missing value. Later lessons show how to use a present value with match.
YOUR TURN
A small check before you continue
Which API would you use for a user-supplied position: pages[index] or pages.get(index)?
Show a suggested answer
Use get and handle None. A user can supply a position outside the vector.
Sources and scope
Documentation and examples reviewed 2026-10-18. Provider screens, package versions and output wording can differ. Examples show expected outcomes; they are not a claim that OpenViST connected to your server or verified your setup.
