option

package
v1.20.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 31, 2025 License: MIT Imports: 3 Imported by: 11

Documentation

Overview

Package option provides helper functions for working with Option types.

Option represents an optional value: every Option is either Some (which contains a value), or None (which does not). Option types are very common in Rust code and provide a type-safe alternative to nil pointers and (T, bool) tuples.

Examples

// Create an Option
someValue := option.Some(42)
noneValue := option.None[int]()

// Check and unwrap
if someValue.IsSome() {
	fmt.Println(someValue.Unwrap()) // Output: 42
}

// Chain operations
result := someValue.
	Map(func(x int) int { return x * 2 }).
	UnwrapOr(0) // Output: 84

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains[T comparable](o Option[T], x T) bool

Contains returns `true` if the option is a [`Some`] value containing the given value.

func MapOr

func MapOr[T any, U any](o Option[T], defaultSome U, f func(T) U) U

MapOr returns the provided default value (if none), or applies a function to the contained value (if any).

func MapOrElse

func MapOrElse[T any, U any](o Option[T], defaultFn func() U, f func(T) U) U

MapOrElse computes a default function value (if none), or applies a different function to the contained value (if any).

func SafeAssert

func SafeAssert[T any, U any](o Option[T]) result.Result[Option[U]]

SafeAssert asserts Option[T] as result.Result[Option[U]]. NOTE:

If the assertion fails, return error.

func Unzip

func Unzip[T any, U any](p Option[pair.Pair[T, U]]) pair.Pair[Option[T], Option[U]]

Unzip unzips an option containing a `Pair` of two values.

func XSafeAssert

func XSafeAssert[U any](o Option[any]) result.Result[Option[U]]

XSafeAssert asserts Option[any] as result.Result[Option[U]]. NOTE:

If the assertion fails, return error.

Types

type Option

type Option[T any] = core.Option[T]

Option is an alias for core.Option[T]. This allows using option.Option[T] instead of core.Option[T].

Option represents an optional value: every Option is either Some (which is non-none T), or None (which is none). Option types are very common in Rust code, as they have a number of uses:

  • Initial values
  • Return values for functions that are not defined over their entire input range (partial functions)
  • Return value for otherwise reporting simple errors, where None is returned on error
  • Optional struct fields
  • Optional function arguments
  • Nullable pointers
  • Swapping things out of difficult situations

func And

func And[T any, U any](o Option[T], optb Option[U]) Option[U]

And returns [`None`] if the option is [`None`], otherwise returns `optb`.

func AndThen

func AndThen[T any, U any](o Option[T], f func(T) Option[U]) Option[U]

AndThen returns [`None`] if the option is [`None`], otherwise calls `f` with the wrapped value.

func AssertOpt

func AssertOpt[T any](v any) Option[T]

AssertOpt returns the Option[T] of asserting `i` to type `T`

func BoolAssertOpt

func BoolAssertOpt[T any](i any, ok bool) Option[T]

BoolAssertOpt wraps a value as an Option. NOTE:

`ok=true` is wrapped as Some,
and `ok=false` is wrapped as None.

func BoolOpt

func BoolOpt[T any](v T, ok bool) Option[T]

BoolOpt wraps a value as an Option. NOTE:

`ok=true` is wrapped as Some,
and `ok=false` is wrapped as None.

func ElemOpt

func ElemOpt[T any](ptr *T) Option[T]

ElemOpt wraps a value from pointer. NOTE:

`non-nil pointer` is wrapped as Some,
and `nil pointer` is wrapped as None.

func FuzzyAssert

func FuzzyAssert[T any, U any](o Option[T]) Option[U]

FuzzyAssert asserts Option[T] as Option[U]. NOTE:

If the assertion fails, return none.

func Map

func Map[T any, U any](o Option[T], f func(T) U) Option[U]

Map maps an `Option[T]` to `Option[U]` by applying a function to a contained value.

func None

func None[T any]() Option[T]

None returns a none. NOTE:

Option[T].IsNone() returns true,
and Option[T].IsSome() returns false.

func PtrOpt

func PtrOpt[U any, T *U](ptr T) Option[T]

PtrOpt wraps a pointer value. NOTE:

`non-nil pointer` is wrapped as Some,
and `nil pointer` is wrapped as None.

func RetAnyOpt

func RetAnyOpt[T any](v any, err error) Option[any]

RetAnyOpt wraps a value as an `Option[any]`. NOTE:

`err != nil` or `value`==nil is wrapped as None,
and `err == nil` and `value != nil` is wrapped as Some.

func RetOpt

func RetOpt[T any](v T, err error) Option[T]

RetOpt wraps a value as an `Option[T]`. NOTE:

`err != nil` is wrapped as None,
and `err == nil` is wrapped as Some.

func Some

func Some[T any](value T) Option[T]

Some wraps a non-none value. NOTE:

Option[T].IsSome() returns true.
and Option[T].IsNone() returns false.

func XFuzzyAssert

func XFuzzyAssert[U any](o Option[any]) Option[U]

XFuzzyAssert asserts Option[any] as Option[U]. NOTE:

If the assertion fails, return none.

func ZeroOpt

func ZeroOpt[T comparable](v T) Option[T]

ZeroOpt wraps a value as an Option. NOTE:

`non-zero T` is wrapped as Some,
and `zero T` is wrapped as None.

func Zip

func Zip[A any, B any](a Option[A], b Option[B]) Option[pair.Pair[A, B]]

Zip zips `a` with b `Option`.

If `a` is `Some(s)` and `b` is `Some(o)`, this method returns `Some(Pair{A:s, B:o})`. Otherwise, `None` is returned.

func ZipWith

func ZipWith[T any, U any, R any](some Option[T], other Option[U], f func(T, U) R) Option[R]

ZipWith zips `value` and another `Option` with function `f`.

If `value` is `Some(s)` and `other` is `Some(o)`, this method returns `Some(f(s, o))`. Otherwise, `None` is returned.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL