Optional

enum Optional<Wrapped>

A value that is either present or absent without an error payload.

Use Optional when absence is expected and needs no explanation; use Result when callers must distinguish failure reasons. Mapping and defaults are consuming so an owned payload moves exactly once.

Cases

case some(Wrapped)

Contains a present value.

case none

Represents the absence of a value.

Methods

consuming func map<T>(transform: (Wrapped) -> T) -> T?

Applies transform only to a present payload.

The optional and payload are consumed; none does not invoke the callback.

View Source
pub consuming func map<T>(transform: (Wrapped) -> T) -> T? {
	match self {
		.some(t) -> .some(transform(t)),
		.none -> .none
	}
}

consuming func or(default: () -> Wrapped) -> Wrapped

Moves out a present payload or invokes default for none.

The fallback is lazy and is never evaluated when a value is present.

View Source
pub consuming func or(default: () -> Wrapped) -> Wrapped {
	match self {
		.some(t) -> t,
		.none -> default()
	}
}

consuming func or_err<E>(default: () -> E) -> Result<Wrapped, E>

Converts some to ok and none to a lazily produced error.

The error callback is skipped when the optional contains a value.

View Source
pub consuming func or_err<E>(default: () -> E) -> Result<Wrapped, E> {
	match self {
		.some(t) -> .ok(t),
		.none -> .error(default())
	}
}