third_party/move/move-stdlib/docs/option.md
<a id="0x1_option"></a>
0x1::optionThis module defines the Option type and its methods to represent and handle an optional value.
Optionnonesomeis_noneis_somecontainsborrowborrow_with_defaultget_with_defaultfillextractborrow_mutswapswap_or_filldestroy_with_defaultdestroy_somedestroy_noneto_vecfor_eachfor_each_reffor_each_mutfoldmapfilter<a id="0x1_option_Option"></a>
OptionAbstraction 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.
<pre><code><b>struct</b> <a href="option.md#0x1_option_Option">Option</a><Element> <b>has</b> <b>copy</b>, drop, store </code></pre> <details> <summary>Fields</summary> <dl> <dt> <code>vec: <a href="vector.md#0x1_vector">vector</a><Element></code> </dt> <dd> </dd> </dl> </details> <details> <summary>Specification</summary>The size of vector is always less than equal to 1 because it's 0 for "none" or 1 for "some".
<pre><code><b>invariant</b> len(vec) <= 1; </code></pre> </details><a id="@Constants_0"></a>
<a id="0x1_option_EOPTION_IS_SET"></a>
The <code><a href="option.md#0x1_option_Option">Option</a></code> is in an invalid state for the operation attempted. The <code><a href="option.md#0x1_option_Option">Option</a></code> is <code>Some</code> while it should be <code>None</code>.
<pre><code><b>const</b> <a href="option.md#0x1_option_EOPTION_IS_SET">EOPTION_IS_SET</a>: u64 = 262144; </code></pre><a id="0x1_option_EOPTION_NOT_SET"></a>
The <code><a href="option.md#0x1_option_Option">Option</a></code> is in an invalid state for the operation attempted. The <code><a href="option.md#0x1_option_Option">Option</a></code> is <code>None</code> while it should be <code>Some</code>.
<pre><code><b>const</b> <a href="option.md#0x1_option_EOPTION_NOT_SET">EOPTION_NOT_SET</a>: u64 = 262145; </code></pre><a id="0x1_option_none"></a>
noneReturn an empty <code><a href="option.md#0x1_option_Option">Option</a></code>
<pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_none">none</a><Element>(): <a href="option.md#0x1_option_Option">option::Option</a><Element> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_none">none</a><Element>(): <a href="option.md#0x1_option_Option">Option</a><Element> { <a href="option.md#0x1_option_Option">Option</a> { vec: <a href="vector.md#0x1_vector_empty">vector::empty</a>() } } </code></pre> </details> <details> <summary>Specification</summary> <pre><code><b>pragma</b> opaque; <b>aborts_if</b> <b>false</b>; <b>ensures</b> result == <a href="option.md#0x1_option_spec_none">spec_none</a><Element>(); </code></pre><a id="0x1_option_spec_none"></a>
<pre><code><b>fun</b> <a href="option.md#0x1_option_spec_none">spec_none</a><Element>(): <a href="option.md#0x1_option_Option">Option</a><Element> { <a href="option.md#0x1_option_Option">Option</a>{ vec: vec() } } </code></pre> </details><a id="0x1_option_some"></a>
someReturn an <code><a href="option.md#0x1_option_Option">Option</a></code> containing <code>e</code>
<pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_some">some</a><Element>(e: Element): <a href="option.md#0x1_option_Option">option::Option</a><Element> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_some">some</a><Element>(e: Element): <a href="option.md#0x1_option_Option">Option</a><Element> { <a href="option.md#0x1_option_Option">Option</a> { vec: <a href="vector.md#0x1_vector_singleton">vector::singleton</a>(e) } } </code></pre> </details> <details> <summary>Specification</summary> <pre><code><b>pragma</b> opaque; <b>aborts_if</b> <b>false</b>; <b>ensures</b> result == <a href="option.md#0x1_option_spec_some">spec_some</a>(e); </code></pre><a id="0x1_option_spec_some"></a>
<pre><code><b>fun</b> <a href="option.md#0x1_option_spec_some">spec_some</a><Element>(e: Element): <a href="option.md#0x1_option_Option">Option</a><Element> { <a href="option.md#0x1_option_Option">Option</a>{ vec: vec(e) } } </code></pre> </details><a id="0x1_option_is_none"></a>
is_noneReturn true if <code>t</code> does not hold a value
<pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_is_none">is_none</a><Element>(t: &<a href="option.md#0x1_option_Option">option::Option</a><Element>): bool </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_is_none">is_none</a><Element>(t: &<a href="option.md#0x1_option_Option">Option</a><Element>): bool { <a href="vector.md#0x1_vector_is_empty">vector::is_empty</a>(&t.vec) } </code></pre> </details> <details> <summary>Specification</summary> <pre><code><b>pragma</b> opaque; <b>aborts_if</b> <b>false</b>; <b>ensures</b> result == <a href="option.md#0x1_option_is_none">is_none</a>(t); </code></pre> </details><a id="0x1_option_is_some"></a>
is_someReturn true if <code>t</code> holds a value
<pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_is_some">is_some</a><Element>(t: &<a href="option.md#0x1_option_Option">option::Option</a><Element>): bool </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_is_some">is_some</a><Element>(t: &<a href="option.md#0x1_option_Option">Option</a><Element>): bool { !<a href="vector.md#0x1_vector_is_empty">vector::is_empty</a>(&t.vec) } </code></pre> </details> <details> <summary>Specification</summary> <pre><code><b>pragma</b> opaque; <b>aborts_if</b> <b>false</b>; <b>ensures</b> result == <a href="option.md#0x1_option_is_some">is_some</a>(t); </code></pre> </details><a id="0x1_option_contains"></a>
containsReturn true if the value in <code>t</code> is equal to <code>e_ref</code> Always returns <code><b>false</b></code> if <code>t</code> does not hold a value
<pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_contains">contains</a><Element>(t: &<a href="option.md#0x1_option_Option">option::Option</a><Element>, e_ref: &Element): bool </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_contains">contains</a><Element>(t: &<a href="option.md#0x1_option_Option">Option</a><Element>, e_ref: &Element): bool { <a href="vector.md#0x1_vector_contains">vector::contains</a>(&t.vec, e_ref) } </code></pre> </details> <details> <summary>Specification</summary> <pre><code><b>pragma</b> opaque; <b>aborts_if</b> <b>false</b>; <b>ensures</b> result == <a href="option.md#0x1_option_spec_contains">spec_contains</a>(t, e_ref); </code></pre><a id="0x1_option_spec_contains"></a>
<pre><code><b>fun</b> <a href="option.md#0x1_option_spec_contains">spec_contains</a><Element>(t: <a href="option.md#0x1_option_Option">Option</a><Element>, e: Element): bool { <a href="option.md#0x1_option_is_some">is_some</a>(t) && <a href="option.md#0x1_option_borrow">borrow</a>(t) == e } </code></pre> </details><a id="0x1_option_borrow"></a>
borrowReturn an immutable reference to the value inside <code>t</code> Aborts if <code>t</code> does not hold a value
<pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_borrow">borrow</a><Element>(t: &<a href="option.md#0x1_option_Option">option::Option</a><Element>): &Element </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_borrow">borrow</a><Element>(t: &<a href="option.md#0x1_option_Option">Option</a><Element>): &Element { <b>assert</b>!(<a href="option.md#0x1_option_is_some">is_some</a>(t), <a href="option.md#0x1_option_EOPTION_NOT_SET">EOPTION_NOT_SET</a>); <a href="vector.md#0x1_vector_borrow">vector::borrow</a>(&t.vec, 0) } </code></pre> </details> <details> <summary>Specification</summary> <pre><code><b>pragma</b> opaque; <b>include</b> <a href="option.md#0x1_option_AbortsIfNone">AbortsIfNone</a><Element>; <b>ensures</b> result == <a href="option.md#0x1_option_borrow">borrow</a>(t); </code></pre> </details><a id="0x1_option_borrow_with_default"></a>
borrow_with_defaultReturn a reference to the value inside <code>t</code> if it holds one Return <code>default_ref</code> if <code>t</code> does not hold a value
<pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_borrow_with_default">borrow_with_default</a><Element>(t: &<a href="option.md#0x1_option_Option">option::Option</a><Element>, default_ref: &Element): &Element </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_borrow_with_default">borrow_with_default</a><Element>(t: &<a href="option.md#0x1_option_Option">Option</a><Element>, default_ref: &Element): &Element { <b>let</b> vec_ref = &t.vec; <b>if</b> (<a href="vector.md#0x1_vector_is_empty">vector::is_empty</a>(vec_ref)) default_ref <b>else</b> <a href="vector.md#0x1_vector_borrow">vector::borrow</a>(vec_ref, 0) } </code></pre> </details> <details> <summary>Specification</summary> <pre><code><b>pragma</b> opaque; <b>aborts_if</b> <b>false</b>; <b>ensures</b> result == (<b>if</b> (<a href="option.md#0x1_option_is_some">is_some</a>(t)) <a href="option.md#0x1_option_borrow">borrow</a>(t) <b>else</b> default_ref); </code></pre> </details><a id="0x1_option_get_with_default"></a>
get_with_defaultReturn the value inside <code>t</code> if it holds one Return <code>default</code> if <code>t</code> does not hold a value
<pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_get_with_default">get_with_default</a><Element: <b>copy</b>, drop>(t: &<a href="option.md#0x1_option_Option">option::Option</a><Element>, default: Element): Element </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_get_with_default">get_with_default</a><Element: <b>copy</b> + drop>( t: &<a href="option.md#0x1_option_Option">Option</a><Element>, default: Element, ): Element { <b>let</b> vec_ref = &t.vec; <b>if</b> (<a href="vector.md#0x1_vector_is_empty">vector::is_empty</a>(vec_ref)) default <b>else</b> *<a href="vector.md#0x1_vector_borrow">vector::borrow</a>(vec_ref, 0) } </code></pre> </details> <details> <summary>Specification</summary> <pre><code><b>pragma</b> opaque; <b>aborts_if</b> <b>false</b>; <b>ensures</b> result == (<b>if</b> (<a href="option.md#0x1_option_is_some">is_some</a>(t)) <a href="option.md#0x1_option_borrow">borrow</a>(t) <b>else</b> default); </code></pre> </details><a id="0x1_option_fill"></a>
fillConvert the none option <code>t</code> to a some option by adding <code>e</code>. Aborts if <code>t</code> already holds a value
<pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_fill">fill</a><Element>(t: &<b>mut</b> <a href="option.md#0x1_option_Option">option::Option</a><Element>, e: Element) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_fill">fill</a><Element>(t: &<b>mut</b> <a href="option.md#0x1_option_Option">Option</a><Element>, e: Element) { <b>let</b> vec_ref = &<b>mut</b> t.vec; <b>if</b> (<a href="vector.md#0x1_vector_is_empty">vector::is_empty</a>(vec_ref)) <a href="vector.md#0x1_vector_push_back">vector::push_back</a>(vec_ref, e) <b>else</b> <b>abort</b> <a href="option.md#0x1_option_EOPTION_IS_SET">EOPTION_IS_SET</a> } </code></pre> </details> <details> <summary>Specification</summary> <pre><code><b>pragma</b> opaque; <b>aborts_if</b> <a href="option.md#0x1_option_is_some">is_some</a>(t) <b>with</b> <a href="option.md#0x1_option_EOPTION_IS_SET">EOPTION_IS_SET</a>; <b>ensures</b> <a href="option.md#0x1_option_is_some">is_some</a>(t); <b>ensures</b> <a href="option.md#0x1_option_borrow">borrow</a>(t) == e; </code></pre> </details><a id="0x1_option_extract"></a>
extractConvert a <code>some</code> option to a <code>none</code> by removing and returning the value stored inside <code>t</code> Aborts if <code>t</code> does not hold a value
<pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_extract">extract</a><Element>(t: &<b>mut</b> <a href="option.md#0x1_option_Option">option::Option</a><Element>): Element </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_extract">extract</a><Element>(t: &<b>mut</b> <a href="option.md#0x1_option_Option">Option</a><Element>): Element { <b>assert</b>!(<a href="option.md#0x1_option_is_some">is_some</a>(t), <a href="option.md#0x1_option_EOPTION_NOT_SET">EOPTION_NOT_SET</a>); <a href="vector.md#0x1_vector_pop_back">vector::pop_back</a>(&<b>mut</b> t.vec) } </code></pre> </details> <details> <summary>Specification</summary> <pre><code><b>pragma</b> opaque; <b>include</b> <a href="option.md#0x1_option_AbortsIfNone">AbortsIfNone</a><Element>; <b>ensures</b> result == <a href="option.md#0x1_option_borrow">borrow</a>(<b>old</b>(t)); <b>ensures</b> <a href="option.md#0x1_option_is_none">is_none</a>(t); </code></pre> </details><a id="0x1_option_borrow_mut"></a>
borrow_mutReturn a mutable reference to the value inside <code>t</code> Aborts if <code>t</code> does not hold a value
<pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_borrow_mut">borrow_mut</a><Element>(t: &<b>mut</b> <a href="option.md#0x1_option_Option">option::Option</a><Element>): &<b>mut</b> Element </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_borrow_mut">borrow_mut</a><Element>(t: &<b>mut</b> <a href="option.md#0x1_option_Option">Option</a><Element>): &<b>mut</b> Element { <b>assert</b>!(<a href="option.md#0x1_option_is_some">is_some</a>(t), <a href="option.md#0x1_option_EOPTION_NOT_SET">EOPTION_NOT_SET</a>); <a href="vector.md#0x1_vector_borrow_mut">vector::borrow_mut</a>(&<b>mut</b> t.vec, 0) } </code></pre> </details> <details> <summary>Specification</summary> <pre><code><b>pragma</b> opaque; <b>include</b> <a href="option.md#0x1_option_AbortsIfNone">AbortsIfNone</a><Element>; <b>ensures</b> result == <a href="option.md#0x1_option_borrow">borrow</a>(t); <b>ensures</b> t == <b>old</b>(t); </code></pre> </details><a id="0x1_option_swap"></a>
swapSwap the old value inside <code>t</code> with <code>e</code> and return the old value Aborts if <code>t</code> does not hold a value
<pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_swap">swap</a><Element>(t: &<b>mut</b> <a href="option.md#0x1_option_Option">option::Option</a><Element>, e: Element): Element </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_swap">swap</a><Element>(t: &<b>mut</b> <a href="option.md#0x1_option_Option">Option</a><Element>, e: Element): Element { <b>assert</b>!(<a href="option.md#0x1_option_is_some">is_some</a>(t), <a href="option.md#0x1_option_EOPTION_NOT_SET">EOPTION_NOT_SET</a>); <b>let</b> vec_ref = &<b>mut</b> t.vec; <b>let</b> old_value = <a href="vector.md#0x1_vector_pop_back">vector::pop_back</a>(vec_ref); <a href="vector.md#0x1_vector_push_back">vector::push_back</a>(vec_ref, e); old_value } </code></pre> </details> <details> <summary>Specification</summary> <pre><code><b>pragma</b> opaque; <b>include</b> <a href="option.md#0x1_option_AbortsIfNone">AbortsIfNone</a><Element>; <b>ensures</b> result == <a href="option.md#0x1_option_borrow">borrow</a>(<b>old</b>(t)); <b>ensures</b> <a href="option.md#0x1_option_is_some">is_some</a>(t); <b>ensures</b> <a href="option.md#0x1_option_borrow">borrow</a>(t) == e; </code></pre> </details><a id="0x1_option_swap_or_fill"></a>
swap_or_fillSwap the old value inside <code>t</code> with <code>e</code> and return the old value; or if there is no old value, fill it with <code>e</code>. Different from swap(), swap_or_fill() allows for <code>t</code> not holding a value.
<pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_swap_or_fill">swap_or_fill</a><Element>(t: &<b>mut</b> <a href="option.md#0x1_option_Option">option::Option</a><Element>, e: Element): <a href="option.md#0x1_option_Option">option::Option</a><Element> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_swap_or_fill">swap_or_fill</a><Element>(t: &<b>mut</b> <a href="option.md#0x1_option_Option">Option</a><Element>, e: Element): <a href="option.md#0x1_option_Option">Option</a><Element> { <b>let</b> vec_ref = &<b>mut</b> t.vec; <b>let</b> old_value = <b>if</b> (<a href="vector.md#0x1_vector_is_empty">vector::is_empty</a>(vec_ref)) <a href="option.md#0x1_option_none">none</a>() <b>else</b> <a href="option.md#0x1_option_some">some</a>(<a href="vector.md#0x1_vector_pop_back">vector::pop_back</a>(vec_ref)); <a href="vector.md#0x1_vector_push_back">vector::push_back</a>(vec_ref, e); old_value } </code></pre> </details> <details> <summary>Specification</summary> <pre><code><b>pragma</b> opaque; <b>ensures</b> result == <b>old</b>(t); <b>ensures</b> <a href="option.md#0x1_option_borrow">borrow</a>(t) == e; </code></pre> </details><a id="0x1_option_destroy_with_default"></a>
destroy_with_defaultDestroys <code>t.</code> If <code>t</code> holds a value, return it. Returns <code>default</code> otherwise
<pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_destroy_with_default">destroy_with_default</a><Element: drop>(t: <a href="option.md#0x1_option_Option">option::Option</a><Element>, default: Element): Element </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_destroy_with_default">destroy_with_default</a><Element: drop>(t: <a href="option.md#0x1_option_Option">Option</a><Element>, default: Element): Element { <b>let</b> <a href="option.md#0x1_option_Option">Option</a> { vec } = t; <b>if</b> (<a href="vector.md#0x1_vector_is_empty">vector::is_empty</a>(&<b>mut</b> vec)) default <b>else</b> <a href="vector.md#0x1_vector_pop_back">vector::pop_back</a>(&<b>mut</b> vec) } </code></pre> </details> <details> <summary>Specification</summary> <pre><code><b>pragma</b> opaque; <b>aborts_if</b> <b>false</b>; <b>ensures</b> result == (<b>if</b> (<a href="option.md#0x1_option_is_some">is_some</a>(t)) <a href="option.md#0x1_option_borrow">borrow</a>(t) <b>else</b> default); </code></pre> </details><a id="0x1_option_destroy_some"></a>
destroy_someUnpack <code>t</code> and return its contents Aborts if <code>t</code> does not hold a value
<pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_destroy_some">destroy_some</a><Element>(t: <a href="option.md#0x1_option_Option">option::Option</a><Element>): Element </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_destroy_some">destroy_some</a><Element>(t: <a href="option.md#0x1_option_Option">Option</a><Element>): Element { <b>assert</b>!(<a href="option.md#0x1_option_is_some">is_some</a>(&t), <a href="option.md#0x1_option_EOPTION_NOT_SET">EOPTION_NOT_SET</a>); <b>let</b> <a href="option.md#0x1_option_Option">Option</a> { vec } = t; <b>let</b> elem = <a href="vector.md#0x1_vector_pop_back">vector::pop_back</a>(&<b>mut</b> vec); <a href="vector.md#0x1_vector_destroy_empty">vector::destroy_empty</a>(vec); elem } </code></pre> </details> <details> <summary>Specification</summary> <pre><code><b>pragma</b> opaque; <b>include</b> <a href="option.md#0x1_option_AbortsIfNone">AbortsIfNone</a><Element>; <b>ensures</b> result == <a href="option.md#0x1_option_borrow">borrow</a>(t); </code></pre> </details><a id="0x1_option_destroy_none"></a>
destroy_noneUnpack <code>t</code> Aborts if <code>t</code> holds a value
<pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_destroy_none">destroy_none</a><Element>(t: <a href="option.md#0x1_option_Option">option::Option</a><Element>) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_destroy_none">destroy_none</a><Element>(t: <a href="option.md#0x1_option_Option">Option</a><Element>) { <b>assert</b>!(<a href="option.md#0x1_option_is_none">is_none</a>(&t), <a href="option.md#0x1_option_EOPTION_IS_SET">EOPTION_IS_SET</a>); <b>let</b> <a href="option.md#0x1_option_Option">Option</a> { vec } = t; <a href="vector.md#0x1_vector_destroy_empty">vector::destroy_empty</a>(vec) } </code></pre> </details> <details> <summary>Specification</summary> <pre><code><b>pragma</b> opaque; <b>aborts_if</b> <a href="option.md#0x1_option_is_some">is_some</a>(t) <b>with</b> <a href="option.md#0x1_option_EOPTION_IS_SET">EOPTION_IS_SET</a>; </code></pre> </details><a id="0x1_option_to_vec"></a>
to_vecConvert <code>t</code> into a vector of length 1 if it is <code>Some</code>, and an empty vector otherwise
<pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_to_vec">to_vec</a><Element>(t: <a href="option.md#0x1_option_Option">option::Option</a><Element>): <a href="vector.md#0x1_vector">vector</a><Element> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_to_vec">to_vec</a><Element>(t: <a href="option.md#0x1_option_Option">Option</a><Element>): <a href="vector.md#0x1_vector">vector</a><Element> { <b>let</b> <a href="option.md#0x1_option_Option">Option</a> { vec } = t; vec } </code></pre> </details> <details> <summary>Specification</summary> <pre><code><b>pragma</b> opaque; <b>aborts_if</b> <b>false</b>; <b>ensures</b> result == t.vec; </code></pre> </details><a id="0x1_option_for_each"></a>
for_eachApply the function to the optional element, consuming it.
<pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_for_each">for_each</a><Element>(o: <a href="option.md#0x1_option_Option">option::Option</a><Element>, f: |Element|) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> inline <b>fun</b> <a href="option.md#0x1_option_for_each">for_each</a><Element>(o: <a href="option.md#0x1_option_Option">Option</a><Element>, f: |Element|) { <b>if</b> (<a href="option.md#0x1_option_is_some">is_some</a>(&o)) { f(<a href="option.md#0x1_option_destroy_some">destroy_some</a>(o)) } <b>else</b> { <a href="option.md#0x1_option_destroy_none">destroy_none</a>(o) } } </code></pre> </details><a id="0x1_option_for_each_ref"></a>
for_each_refApply the function to the optional element reference.
<pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_for_each_ref">for_each_ref</a><Element>(o: &<a href="option.md#0x1_option_Option">option::Option</a><Element>, f: |&Element|) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> inline <b>fun</b> <a href="option.md#0x1_option_for_each_ref">for_each_ref</a><Element>(o: &<a href="option.md#0x1_option_Option">Option</a><Element>, f: |&Element|) { <b>if</b> (<a href="option.md#0x1_option_is_some">is_some</a>(o)) { f(<a href="option.md#0x1_option_borrow">borrow</a>(o)) } } </code></pre> </details><a id="0x1_option_for_each_mut"></a>
for_each_mutApply the function to the optional element reference.
<pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_for_each_mut">for_each_mut</a><Element>(o: &<b>mut</b> <a href="option.md#0x1_option_Option">option::Option</a><Element>, f: |&<b>mut</b> Element|) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> inline <b>fun</b> <a href="option.md#0x1_option_for_each_mut">for_each_mut</a><Element>(o: &<b>mut</b> <a href="option.md#0x1_option_Option">Option</a><Element>, f: |&<b>mut</b> Element|) { <b>if</b> (<a href="option.md#0x1_option_is_some">is_some</a>(o)) { f(<a href="option.md#0x1_option_borrow_mut">borrow_mut</a>(o)) } } </code></pre> </details><a id="0x1_option_fold"></a>
foldFolds the function over the optional element.
<pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_fold">fold</a><Accumulator, Element>(o: <a href="option.md#0x1_option_Option">option::Option</a><Element>, init: Accumulator, f: |Accumulator, Element|Accumulator): Accumulator </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> inline <b>fun</b> <a href="option.md#0x1_option_fold">fold</a><Accumulator, Element>( o: <a href="option.md#0x1_option_Option">Option</a><Element>, init: Accumulator, f: |Accumulator,Element|Accumulator ): Accumulator { <b>if</b> (<a href="option.md#0x1_option_is_some">is_some</a>(&o)) { f(init, <a href="option.md#0x1_option_destroy_some">destroy_some</a>(o)) } <b>else</b> { <a href="option.md#0x1_option_destroy_none">destroy_none</a>(o); init } } </code></pre> </details><a id="0x1_option_map"></a>
mapMaps the content of an option
<pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_map">map</a><Element, OtherElement>(o: <a href="option.md#0x1_option_Option">option::Option</a><Element>, f: |Element|OtherElement): <a href="option.md#0x1_option_Option">option::Option</a><OtherElement> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> inline <b>fun</b> <a href="option.md#0x1_option_map">map</a><Element, OtherElement>(o: <a href="option.md#0x1_option_Option">Option</a><Element>, f: |Element|OtherElement): <a href="option.md#0x1_option_Option">Option</a><OtherElement> { <b>if</b> (<a href="option.md#0x1_option_is_some">is_some</a>(&o)) { <a href="option.md#0x1_option_some">some</a>(f(<a href="option.md#0x1_option_destroy_some">destroy_some</a>(o))) } <b>else</b> { <a href="option.md#0x1_option_destroy_none">destroy_none</a>(o); <a href="option.md#0x1_option_none">none</a>() } } </code></pre> </details><a id="0x1_option_filter"></a>
filterFilters the content of an option
<pre><code><b>public</b> <b>fun</b> <a href="option.md#0x1_option_filter">filter</a><Element: drop>(o: <a href="option.md#0x1_option_Option">option::Option</a><Element>, f: |&Element|bool): <a href="option.md#0x1_option_Option">option::Option</a><Element> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> inline <b>fun</b> <a href="option.md#0x1_option_filter">filter</a><Element:drop>(o: <a href="option.md#0x1_option_Option">Option</a><Element>, f: |&Element|bool): <a href="option.md#0x1_option_Option">Option</a><Element> { <b>if</b> (<a href="option.md#0x1_option_is_some">is_some</a>(&o) && f(<a href="option.md#0x1_option_borrow">borrow</a>(&o))) { o } <b>else</b> { <a href="option.md#0x1_option_none">none</a>() } } </code></pre> </details><a id="@Module_Specification_1"></a>
<a id="@Helper_Schema_2"></a>
<a id="0x1_option_AbortsIfNone"></a>
<pre><code><b>schema</b> <a href="option.md#0x1_option_AbortsIfNone">AbortsIfNone</a><Element> { t: <a href="option.md#0x1_option_Option">Option</a><Element>; <b>aborts_if</b> <a href="option.md#0x1_option_is_none">is_none</a>(t) <b>with</b> <a href="option.md#0x1_option_EOPTION_NOT_SET">EOPTION_NOT_SET</a>; } </code></pre>