Introduction
Real lookups do not always find a value. Rust represents that possibility with Option<T>: Some(value) means a value exists, while None means it does not. This makes absence part of the type instead of hiding it behind a made-up number or risking a panic.
You will complete a small inventory reporter that handles found and missing stock, distinguishes an existing zero-stock record from no record, and uses if let when only the present case needs an action.
Match Present and Missing Values
In this step, you will replace a placeholder report with an exhaustive match over an optional lookup result.
Enter the prepared Cargo project:
cd /home/labex/project/option-inventory
Open its source:
nano src/main.rs
The prepared find_stock function returns Option<u32>. For now, read the angle brackets as “an Option that may contain a u32”; defining reusable types with angle-bracket parameters is taught later. Some(12) contains a stock count, while None contains no count at all.
At the bottom of the prepared lookup, _ => None uses the wildcard pattern _. It means “any code not matched by an earlier arm.” The wildcard does not create a usable variable; it deliberately ignores the unmatched value and returns None.
Find the placeholder println! inside report and replace that one line with this match:
match find_stock(code) {
Some(count) => println!("{code}: {count} in stock"),
None => println!("{code}: not found"),
}
Some(count) both recognizes a present value and gives its inner number the local name count. The None arm handles absence explicitly, so the match covers every possible Option<u32> value.
Save with Ctrl+O, press Enter, and exit with Ctrl+X. Check the program before running it:
cargo check
Finished confirms that both Option cases are covered. Now run the reporter:
cargo run --quiet
You should see:
A1: 12 in stock
B2: 0 in stock
Z9: not found
The final line comes from None; the first two come from Some, including the value Some(0).
Distinguish Zero from Absence
In this step, you will give an existing zero-stock record its own behavior instead of treating zero as missing.
Some(0) and None mean different things. Some(0) says the lookup found a record whose count is zero. None says no record exists for that code. This distinction is why a placeholder such as 0 is not a safe substitute for absence.
Open the source again:
nano src/main.rs
Inside the match in report, add this arm immediately above the existing Some(count) arm:
Some(0) => println!("{code}: out of stock"),
Match arms are checked from top to bottom. The specific Some(0) pattern must come before Some(count), because the broader pattern also matches zero.
Save and exit, then run the program:
cargo run --quiet
The output should now be:
A1: 12 in stock
B2: out of stock
Z9: not found
Each line now represents a distinct state: available, present but empty, or missing.
Act Only When a Value Exists
In this step, you will use if let for a task that needs an action only when a lookup succeeds.
An exhaustive match is best when every case needs different behavior. if let PATTERN = VALUE is a concise conditional pattern for the other common situation: run one block when a pattern matches and do nothing otherwise.
Open the source:
nano src/main.rs
Add this function immediately above main:
fn highlight_available(code: &str) {
if let Some(count) = find_stock(code) {
println!("{code}: {count} available for promotion");
}
}
The pattern succeeds only for Some, so count is available inside the braces. There is no else block because this helper intentionally has nothing to print for None.
In main, remove the leading // from the prepared highlight_available("C3"); line. Save and exit, then check and run:
cargo check
cargo run --quiet
The complete output should be:
A1: 12 in stock
B2: out of stock
Z9: not found
C3: 5 available for promotion
The fourth line proves that if let extracted the value inside Some(5) without inventing behavior for a missing value.
Summary
You matched Some and None, preserved the difference between Some(0) and absence, and used if let when only the present case required an action.


