This module defines the Option type and its methods to represent and handle an optional value.
- Struct
Option - Constants
- Function
none - Function
some - Function
from_vec - Function
is_none - Function
is_some - Function
contains - Function
borrow - Function
borrow_with_default - Function
get_with_default - Function
fill - Function
extract - Function
borrow_mut - Function
swap - Function
swap_or_fill - Function
destroy_with_default - Function
destroy_some - Function
destroy_none - Function
to_vec - Function
for_each - Function
for_each_ref - Function
for_each_mut - Function
fold - Function
map - Function
map_ref - Function
filter - Function
any - Function
destroy - Module Specification
use 0x1::vector;
Abstraction of a value that may or may not be present. Implemented with a vector of size zero or one because Move bytecode does not have ADTs.
struct Option<Element> has copy, drop, store
The Option is in an invalid state for the operation attempted.
The Option is Some while it should be None.
const EOPTION_IS_SET: u64 = 262144;
The Option is in an invalid state for the operation attempted.
The Option is None while it should be Some.
const EOPTION_NOT_SET: u64 = 262145;
Cannot construct an option from a vector with 2 or more elements.
const EOPTION_VEC_TOO_LONG: u64 = 262146;
Return an empty Option
public fun none<Element>(): option::Option<Element>
Return an Option containing e
public fun some<Element>(e: Element): option::Option<Element>
public fun from_vec<Element>(vec: vector<Element>): option::Option<Element>
Return true if t does not hold a value
public fun is_none<Element>(t: &option::Option<Element>): bool
Return true if t holds a value
public fun is_some<Element>(t: &option::Option<Element>): bool
Return true if the value in t is equal to e_ref
Always returns false if t does not hold a value
public fun contains<Element>(t: &option::Option<Element>, e_ref: &Element): bool
Return an immutable reference to the value inside t
Aborts if t does not hold a value
public fun borrow<Element>(t: &option::Option<Element>): &Element
Return a reference to the value inside t if it holds one
Return default_ref if t does not hold a value
public fun borrow_with_default<Element>(t: &option::Option<Element>, default_ref: &Element): &Element
Return the value inside t if it holds one
Return default if t does not hold a value
public fun get_with_default<Element: copy, drop>(t: &option::Option<Element>, default: Element): Element
Convert the none option t to a some option by adding e.
Aborts if t already holds a value
public fun fill<Element>(t: &mut option::Option<Element>, e: Element)
Convert a some option to a none by removing and returning the value stored inside t
Aborts if t does not hold a value
public fun extract<Element>(t: &mut option::Option<Element>): Element
Return a mutable reference to the value inside t
Aborts if t does not hold a value
public fun borrow_mut<Element>(t: &mut option::Option<Element>): &mut Element
Swap the old value inside t with e and return the old value
Aborts if t does not hold a value
public fun swap<Element>(t: &mut option::Option<Element>, e: Element): Element
Swap the old value inside t with e and return the old value;
or if there is no old value, fill it with e.
Different from swap(), swap_or_fill() allows for t not holding a value.
public fun swap_or_fill<Element>(t: &mut option::Option<Element>, e: Element): option::Option<Element>
Destroys t. If t holds a value, return it. Returns default otherwise
public fun destroy_with_default<Element: drop>(t: option::Option<Element>, default: Element): Element
Unpack t and return its contents
Aborts if t does not hold a value
public fun destroy_some<Element>(t: option::Option<Element>): Element
Unpack t
Aborts if t holds a value
public fun destroy_none<Element>(t: option::Option<Element>)
Convert t into a vector of length 1 if it is Some,
and an empty vector otherwise
public fun to_vec<Element>(t: option::Option<Element>): vector<Element>
Apply the function to the optional element, consuming it. Does nothing if no value present.
public fun for_each<Element>(o: option::Option<Element>, f: |Element|())
Apply the function to the optional element reference. Does nothing if no value present.
public fun for_each_ref<Element>(o: &option::Option<Element>, f: |&Element|())
Apply the function to the optional element reference. Does nothing if no value present.
public fun for_each_mut<Element>(o: &mut option::Option<Element>, f: |&mut Element|())
Folds the function over the optional element.
public fun fold<Accumulator, Element>(o: option::Option<Element>, init: Accumulator, f: |(Accumulator, Element)|Accumulator): Accumulator
Maps the content of an option.
public fun map<Element, OtherElement>(o: option::Option<Element>, f: |Element|OtherElement): option::Option<OtherElement>
Maps the content of an option without destroying the original option.
public fun map_ref<Element, OtherElement>(o: &option::Option<Element>, f: |&Element|OtherElement): option::Option<OtherElement>
Filters the content of an option
public fun filter<Element: drop>(o: option::Option<Element>, f: |&Element|bool): option::Option<Element>
Returns true if the option contains an element which satisfies predicate.
public fun any<Element>(o: &option::Option<Element>, p: |&Element|bool): bool
Utility function to destroy an option that is not droppable.
public fun destroy<Element>(o: option::Option<Element>, d: |Element|())