Extract the value or short-circuit the calling function.
When expression is suffixed with ?
, this is de-sugared to an invocation of try
macro on the
expression.
Expands to the following expression
if $.is_some() {
$.unwrap()
} else {
return Option::none()
}
Example
fn maybe_sum(maybe_a: Option<i32>, maybe_b: Option<i32>) -> Option<i32> {
use std::option::try;
let a = maybe_a?; // `return Option::none()` when `a` is empty.
let b = maybe_b?; // `return Option::none()` when `a` is empty.
Option::some(a + b)
}
Run this example