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
- Macro function destroy
- Macro function do
- Macro function do_ref
- Macro function do_mut
- Macro function or
- Macro function and
- Macro function and_ref
- Macro function map
- Macro function map_ref
- Macro function filter
- Macro function is_some_and
- Macro function extract_or
- Macro function destroy_or
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 Option<Element> has copy, drop, store
Click to open
Fields
-
vec: vector<Element>
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 none<Element>(): std::option::Option<Element>
Click to open
Implementation
public fun none<Element>(): Option<Element> {
Option { vec: vector::empty() }
}
Function some
Return an
Option containing e
public fun some<Element>(e: Element): std::option::Option<Element>
Click to open
Implementation
public fun some<Element>(e: Element): Option<Element> {
Option { vec: vector::singleton(e) }
}
Function is_none
Return true if t does not hold a value
public fun is_none<Element>(t: &std::option::Option<Element>): bool
Click to open
Function is_some
Return true if t holds a value
public fun is_some<Element>(t: &std::option::Option<Element>): bool
Click to open
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 contains<Element>(t: &std::option::Option<Element>, e_ref: &Element): bool
Click to open
Function borrow
Return an immutable reference to the value inside t
Aborts if t does not hold a value
public fun borrow<Element>(t: &std::option::Option<Element>): &Element
Click to open
Implementation
public fun borrow<Element>(t: &Option<Element>): &Element {
assert!(t.is_some(), EOPTION_NOT_SET);
&t.vec[0]
}
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_default<Element>(t: &std::option::Option<Element>, default_ref: &Element): &Element
Click to open
Implementation
public fun borrow_with_default<Element>(t: &Option<Element>, default_ref: &Element): &Element {
let vec_ref = &t.vec;
if (vec_ref.is_empty()) default_ref else &vec_ref[0]
}