Module std::option
This module defines the Option type and its methods to represent and handle an optional value.
- Struct
Option
- Constants
- Function
none
- Function
some
- 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
use std::vector;
Struct Option
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.
public struct OptionElement has copy, drop, store
Fields
Constants
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;
Function none
Return an empty Option
public fun noneElement(): std::option::Option<Element>
Implementation
Function some
Return an Option containing e
public fun someElement(e: Element): std::option::Option<Element>
Implementation
Function is_none
Return true if t does not hold a value
public fun is_noneElement(t: &std::option::Option<Element>): bool
Implementation
Function is_some
Return true if t holds a value
public fun is_someElement(t: &std::option::Option<Element>): bool
Implementation
Function contains
Return true if the value in t is equal to e_ref Always returns false if t does not hold a value
public fun containsElement(t: &std::option::Option<Element>, e_ref: &Element): bool
Implementation
Function borrow
Return an immutable reference to the value inside t Aborts if t does not hold a value
public fun borrowElement(t: &std::option::Option<Element>): &Element
Implementation
Function borrow_with_default
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_defaultElement(t: &std::option::Option<Element>, default_ref: &Element): &Element
Implementation
Function get_with_default
Return the value inside t if it holds one Return default if t does not hold a value
public fun get_with_defaultElement(t: &std::option::Option<Element>, default: Element): Element
Implementation
Function fill
Convert the none option t to a some option by adding e. Aborts if t already holds a value
public fun fillElement(t: &mut std::option::Option<Element>, e: Element)
Implementation
Function extract
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 extractElement(t: &mut std::option::Option<Element>): Element
Implementation
Function borrow_mut
Return a mutable reference to the value inside t Aborts if t does not hold a value
public fun borrow_mutElement(t: &mut std::option::Option<Element>): &mut Element
Implementation
Function swap
Swap the old value inside t with e and return the old value Aborts if t does not hold a value
public fun swapElement(t: &mut std::option::Option<Element>, e: Element): Element
Implementation
Function swap_or_fill
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_fillElement(t: &mut std::option::Option<Element>, e: Element): std::option::Option<Element>
Implementation
Function destroy_with_default
Destroys t. If t holds a value, return it. Returns default otherwise
public fun destroy_with_defaultElement(t: std::option::Option<Element>, default: Element): Element
Implementation
Function destroy_some
Unpack t and return its contents Aborts if t does not hold a value
public fun destroy_someElement(t: std::option::Option<Element>): Element
Implementation
Function destroy_none
Unpack t Aborts if t holds a value
public fun destroy_noneElement(t: std::option::Option<Element>)
Implementation
Function to_vec
Convert t into a vector of length 1 if it is Some, and an empty vector otherwise
public fun to_vecElement(t: std::option::Option<Element>): vector<Element>