Documentation ¶
Overview ¶
Package option provides the Option type which represents an optional value.
Index ¶
- func MapOr[T any, E any](o Option[T], defaultVal E, f func(v T) E) E
- func MapOrElse[T any, E any](o Option[T], defaultFn func() E, f func(v T) E) E
- type Option
- func (o *Option[T]) AsPtr() *T
- func (o Option[T]) Compare(other Option[T], cmpData func(a, b T) int) int
- func (o Option[T]) Equal(other Option[T], eqData func(a, b T) bool) bool
- func (o Option[T]) Hash(h hash.Hash, hashData func(data T, h hash.Hash))
- func (o Option[T]) IsNone() bool
- func (o Option[T]) IsSome() bool
- func (o *Option[T]) Take() Option[T]
- func (o Option[T]) Unwrap() T
- func (o Option[T]) UnwrapOr(defaultVal T) T
- func (o Option[T]) UnwrapOrElse(f func() T) T
- func (o Option[T]) UnwrapTo(dest *T) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Option ¶
type Option[T any] struct { // contains filtered or unexported fields }
Option represents an optional value.
Type Option represents an optional value: every Option is either Some and contains a value, or None, and does not. Option types are very common in Rust code, as they have a number of uses.
Option in this package is a port of subset of Rust Option type.
One import usage of Options is an optional argument type of filters, tests, or functions.
func AndThen ¶
AndThen returns None if the option is None, otherwise calls `f` with the wrapped value and returns the result.
Some languages call this operation flatmap.
func Map ¶
Map maps an Option[T] to Option[U] by applying a function to a contained value (if Some) or returns None (if None).
func (*Option[T]) AsPtr ¶
func (o *Option[T]) AsPtr() *T
AsPtr returns the pointer to the contained data if the options is a Some value. It returns nil if the option is a None value.
func (Option[T]) Compare ¶
Compare compares o and other and returns:
-1 if o < other 0 if o == other +1 if o > other
where None < Some and for case both options are Some, the contained data are compared with cmpData.
func (Option[T]) Equal ¶
Equal returns true if o and other are both Some values and and those values are equal or both None. It returns false otherwise.
func (Option[T]) Hash ¶
Hash returns the hashed value of the option.
If the option is a Some value, the contained data are hashed with hashData.
func (Option[T]) Unwrap ¶
func (o Option[T]) Unwrap() T
Unwrap returns the contained Some value if the options is a Some value. It panics if the the option is a None value.
func (Option[T]) UnwrapOr ¶
func (o Option[T]) UnwrapOr(defaultVal T) T
UnwrapOr returns the contained Some value or a provided default.
Arguments passed to UnwrapOr are eagerly evaluated; if you are passing the result of a function call, it is recommended to use Option.UnwrapOrElse, which is lazily evaluated.
func (Option[T]) UnwrapOrElse ¶
func (o Option[T]) UnwrapOrElse(f func() T) T
UnwrapOrElse returns the contained Some value or computes it from a closure.
func (Option[T]) UnwrapTo ¶
UnwrapTo sets the contained Some value to dest and returns true if the options is a Some value. It does nothing and returns false if the option is a None value.
This can be used with alternative of Rust's if let Some(dest) = o { ... } or while let Some(dest) = o { ... }
An example with if statement:
if v := (Value{}); o.UnwrapTo(&v) { // do something with v }
An example with for statement and an Iterator.
for v := (Value{}); iter.Next().UnwrapTo(&v); { // do something with v }