Skip to content

Option

1 min read

No import is needed.

Read the usage guide for examples and common tasks. This reference lists the public declarations for this part of the library. See Reading API signatures for parameter and result notation.

Option

Option<T> - a value that is either present (Some) or absent (None).

public enum Option<T>

Some

Contains a value of type T.

Some(T)

None

No value is present.

None

is_some

True when this is Some.

public fun is_some(): bool

is_none

True when this is None.

public fun is_none(): bool

unwrap_or

The contained value when Some, otherwise fallback.

public fun unwrap_or(fallback: T): T

unwrap

The contained value, or panics if None.

public fun unwrap(): T

expect

The contained value, or panics with msg if None.

public fun expect(borrow msg: string): T

unwrap_or_else

The contained value, or f() when None.

public fun unwrap_or_else(borrow f: fun(): T): T

filter

Some when the value is present and pred is true, otherwise None.

public fun filter(borrow pred: fun(T): bool): Option<T>

ok_or

Ok when Some, otherwise Err(err).

public fun ok_or<E>(err: E): Result<T, E>

flatten

Collapses Option<Option<U>>.

public static fun flatten<U>(opt: Option<Option<U>>): Option<U>

map

Maps the contained value when Some, otherwise None.

public fun map<U>(borrow f: fun(T): U): Option<U>

and_then

Chains into another Option when Some, otherwise None.

public fun and_then<U>(borrow f: fun(T): Option<U>): Option<U>

or

This option when Some, otherwise fallback.

public fun or(fallback: Option<T>): Option<T>