Option type operations. auto-imported.
Transform the inner value using a function. If none, returns none.
option.map(some(2), (x) => x * 10) // => some(20)
Chain an Option-returning function on the inner value. Flattens nested Options.
option.flat_map(some(5), (x) => if x > 0 then some(x) else none)
Flatten a nested Option. some(some(x)) becomes some(x), some(none) becomes none.
option.flatten(some(some(42))) // => some(42)
Get the inner value, or return a default if none.
option.unwrap_or(none, 0) // => 0
Get the inner value, or compute a default using a function.
option.unwrap_or_else(none, () => 42) // => 42
Check if the Option contains a value.
option.is_some(some(42)) // => true
Check if the Option is none.
option.is_none(none) // => true
Convert some to ok, none to err with the given error message.
option.to_result(some(42), "missing") // => ok(42)
Keep the value if it satisfies the predicate, otherwise return none.
option.filter(some(5), (x) => x > 3) // => some(5)
Combine two Options into an Option of a tuple. None if either is none.
option.zip(some(1), some(2)) // => some((1, 2))
Return the Option if some, otherwise call the function to produce an alternative.
option.or_else(none, () => some(42)) // => some(42)
Convert some(x) to [x], none to [].
option.to_list(some(42)) // => [42]