Click a card to flip it · 6 terms
Option is how Rust handles values that might not exist. Instead of null, you use Some(value) when a value exists and None when it doesn't.
Either Some(value) or None. Rust's way of saying a value might not exist.
Any value that could be absent. Replaces null from other languages.
fn find_user(id: u64) -> Option<String>
The Option variant that contains a value. Signals the value exists.
Returning or matching when a result is present.
Some(42)Some("Alice")Some(wallet)
The Option variant that means no value. Rust's replacement for null.
Returning or matching when there is nothing to give back.
None // no value// must be handled explicitly
Pulls the value out of Some. Panics if the Option is None.
Only when you are certain the value exists. Avoid in production code.
let val = option.unwrap();// panics if None!
Checks whether an Option has a value or not. Returns bool.
The clean way to check before acting, without destructuring.
if option.is_some() { // handle it}if option.is_none() { ... }
A one-pattern shortcut for match. Handles Some and ignores None.
When you only care about the value existing, and want to skip the None case.
if let Some(val) = result { println!("{}", val);}