Skip to content
Playground

std::opt

opt 'a represents an optional value. It has two variants:

yulang
pub enum opt 'a = nil | just 'a

The prelude re-exports opt, just, and nil, so user code normally writes them unqualified.

Constructing

yulang
nil
just 42
just "hello"

Pattern-matching

yulang
case maybe_text:
    just text -> text.len
    nil       -> 0

The opt variants are exhaustive: every case on an opt 'a needs both arms (or a wildcard) for the inference to settle.

Common shapes

yulang
// Default value
case maybe_text:
    just text -> text
    nil       -> "(no file)"

// Chain through a fallible step
case parse_int s:
    just n  -> just (n * 2)
    nil     -> nil

For richer combinators on optional values, lift them into result (see std::result) or write the few helpers your project needs.

Quick reference

OperationSignature
nilopt 'a
just(x)'a -> opt 'a

See also

Yulang