They are enums from Rust’s standard library for handling values that may be absent or be errors. There is no null in Rust.
Option<T>
Represents an optional value: it contains a value of type T (Some(T)) or none (None).
let option: Option<i32> = Some(42); // Contains a value
let empty_option: Option<i32> = None; // Contains no valuePattern with match
match option {
Some(n) => println!("{}", n),
None => println!("No value"),
}See Control Flow.
Pattern with if let
if let Some(value) = option {
println!("The value is: {}", value);
} else {
println!("No value");
}Methods specific to Option
option.is_some(); // true if it contains a value
option.is_none(); // true if it contains no value
option.filter(|&x| x > 0); // Returns Some(x) if it satisfies the condition, or NoneExample function with Option
fn double(x: Option<i32>) -> Option<i32> {
let n = x?; // If x is None → return None. If it is Some(n), n takes the value
Some(n * 2)
}Note
The
?operator inside a function that returnsOptionorResultpropagatesNone/Errautomatically.
Result<T, E>
Represents the result of an operation that can fail: Ok(T) (success) or Err(E) (error).
let result: Result<i32, &str> = Ok(42); // Success
let error_result: Result<i32, &str> = Err("Error"); // ErrorPattern with match
match result {
Ok(n) => println!("The value is: {}", n),
Err(e) => println!("Error: {}", e),
}Pattern with if let
if let Ok(value) = result {
println!("The value is: {}", value);
} else if let Err(error) = result {
println!("Error: {}", error);
}Methods specific to Result
result.is_ok(); // true if it is Ok
result.is_err(); // true if it is Err
result.map_err(|e| format!("Error: {}", e)); // Transforms the Err, leaves Ok untouchedExample function with Result
fn double(r: Result<i32, &'static str>) -> Result<i32, &'static str> {
let n = r?; // If r is Err(e) → return Err(e). If it is Ok(n), n takes the value
Ok(n * 2)
}Common methods of Option and Result
Both types share these methods:
// Getting the value
option.unwrap(); // Returns the value or panics
option.unwrap_or(0); // Returns the value or the default value
option.unwrap_or_else(|| 0); // Returns the value or the result of the function
// Transforming the value
option.map(|x| x * 2); // Applies a function to the contained value
option.and_then(|x| Some(x * 2)); // Applies a function that returns Option/Result
// Information
option.expect("Error: message"); // Same as unwrap but with a custom message
option.as_ref(); // Returns Option<&T> / Result<&T, &E> (borrows without moving)Warning
unwrap()is dangerous in production code because it causes apanic!if the value isNone/Err. Preferunwrap_or(),unwrap_or_else(), or pattern matching withmatch/if let.
Tip
as_ref()is useful when you need a reference to the value without consuming the original Option/Result.
Next: Error Handling