OpenViSTAI · Agents · Blockchain · Crypto
Menu

OPENVIST LEARN · FREE LESSONS

Option and missing values

Handle both Some and None without assuming a value exists.

Lesson 16 of 30 · About 20 minutes · Beginner · English

By the end of this lesson

Handle both Some and None without assuming a value exists.

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 15, or be comfortable explaining and running its examples.

Three words to know

Option
A value that is present as Some or absent as None.
Some
The variant containing a value.
None
The variant representing absence.

1. Understand the idea

Option<T> makes missing data explicit. It is an enum rather than a special null value shared by every type. Match both cases, or use a helper that reflects your intended fallback. unwrap panics on None; it is not a validation strategy for input you do not control. Think about whether absence is expected or should become an error.

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.

Choose a fallbackRUST · save as lesson.rs
fn main() {
    let values = [5, 8];
    match values.get(9) {
        Some(value) => println!("value={value}"),
        None => println!("no item"),
    }
    println!("fallback={}", values.get(9).copied().unwrap_or(0));
}
What you should see · example output
no item
fallback=0
Compile and run on macOS/LinuxTERMINAL · Bash or Windows PowerShell
rustc --edition=2024 lesson.rs -o lesson
./lesson

3. Explain the result

get returns Option<&i32>. copied converts a present integer reference into a copied integer, keeping None unchanged. unwrap_or supplies zero for absence; decide whether zero could be confused with real data before using that fallback in your own program.

YOUR TURN

A small check before you continue

For a required filename, is silently falling back to an empty path a good default?

Show a suggested answer

Usually not. Treat the missing argument as a validation error and explain the expected command.

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.