Functions and Errors
Option
A Value That May Be Missing
Option<T> is Some(value) or None. Iterator searches return Option, and match handles both cases.
Program
Play the program to find the first value over 15.
option.rs
fn main() {
let nums = vec![10, 20, 30];
let found = nums.iter().find(|&&n| n > 15);
let value = match found {
Some(n) => *n,
None => 0,
};
println!("{value}");
}
Option
`Option<T>` models a value that may be absent.
find
`find` returns `Some` for the first match, else `None`.
match
Handling `Some` and `None` is required to read the value.