pub enum Option<T> {
/// No value.
None: (),
/// Some value of type `T`.
Some: T,
}
Expand description
A type that represents an optional value, either Some(val)
or None
.
Variants
None: ()
No value.
Some: T
Some value of type T
.
Trait Implementations
impl AbiEncode for Option<T>
impl AbiEncode for Option<T>
pub fn abi_encode(self, buffer: Buffer) -> Buffer
impl AbiDecode for Option<T>
impl AbiDecode for Option<T>
pub fn abi_decode(refmut buffer: BufferReader) -> Self
impl Eq for Option<T>
impl Eq for Option<T>
pub fn eq(self, other: Self) -> bool
pub fn neq(self, other: Self) -> bool
pub fn neq(self, other: Self) -> bool
Evaluates if two values of the same type are not equal.
Additional Information
This function is inherited when eq()
is implemented.
Arguments
other
: [Self] - The value of the same type.
Returns
- [bool] -
true
if the two values are not equal, otherwisefalse
.
Examples
struct MyStruct {
val: u64,
}
impl Eq for MyStruct {
fn eq(self, other: Self) -> bool {
self.val == other.val
}
}
fn foo() {
let struct1 = MyStruct { val: 10 };
let struct2 = MyStruct { val: 2 };
let result = struct1 != struct2;
assert(result);
}
impl Option for Option<T>
impl Option for Option<T>
pub fn is_some(self) -> bool
pub fn is_some(self) -> bool
Returns whether the option is the Some
variant.
Returns
- [bool] - Returns
true
if the option isSome
, otherwisefalse
.
Examples
fn foo() {
let x: Option<u32> = Some(2);
assert(x.is_some());
let x: Option<u32> = None;
assert(!x.is_some());
}
pub fn is_none(self) -> bool
pub fn is_none(self) -> bool
Returns whether the option is the None
variant.
Returns
- [bool] - Returns
true
if the option isNone
, otherwisefalse
.
Examples
fn foo() {
let x: Option<u32> = Some(2);
assert(!x.is_none());
let x: Option<u32> = None;
assert(x.is_none());
}
pub fn unwrap(self) -> T
pub fn unwrap(self) -> T
Returns the contained Some
value, consuming the self
value.
Additional Information
Because this function may revert, its use is generally discouraged.
Instead, use pattern matching and handle the None
case explicitly, or call unwrap_or
.
Returns
- [T] - The value contained by the option.
Reverts
- Reverts if the
Option
is theNone
variant.
Examples
fn foo() {
let x = Some(42);
assert(x.unwrap() == 42);
}
fn foo() {
let x: Option<u64> = None;
let value = x.unwrap(); // reverts
}
pub fn unwrap_or(self, default: T) -> T
pub fn unwrap_or(self, default: T) -> T
Returns the contained Some
value or a provided default.
Arguments
default
: [T] - The default value the function will revert to.
Returns
- [T] - The contained value or the default value.
Examples
fn foo() {
assert(Some(42).unwrap_or(69) == 42);
assert(None::<u64>().unwrap_or(69) == 69);
}
pub fn ok_or(self, err: E) -> Result<T, E>
pub fn ok_or(self, err: E) -> Result<T, E>
Transforms the Option<T>
into a Result<T, E>
, mapping Some(v)
to
Ok(v)
and None
to Err(e)
.
Additional Information
Ok(v)
: Result::Ok
Err(e)
: Result::Err
Some(v)
: Option::Some
ok_or
: Option::ok_or
Arguments
err
: [E] - The error value if the option isNone
.
Returns
- [Result<T, E>] - The result containing the value or the error.
Examples
fn foo() {
let x = Some(42);
match x.ok_or(0) {
Result::Ok(inner) => assert(inner == 42),
Result::Err => revert(0),
}
let x: Option<u64> = None;
match x.ok_or(0) {
Result::Ok(_) => revert(0),
Result::Err(e) => assert(e == 0),
}
}
pub fn expect(self, msg: M) -> T
pub fn expect(self, msg: M) -> T
Returns the contained Some
value, consuming the self
value.
If the Option
is the None
variant, logs the provided message.
Additional Information
Because this function may revert, its use is generally discouraged.
Instead, prefer to use pattern matching and handle the None
case explicitly.
Arguments
msg
: [M] - The message to be logged if theOption
is theNone
variant.
Returns
- [T] - The value contained by the option.
Reverts
- Reverts if the
Option
is theNone
variant.
Examples
fn foo() {
let x: Option<u64> = Some(42);
assert(x.expect("X is known to be 42") == 42);
let y: Option<u64> = None;
let val = y.expect("Testing expect"); // reverts with `("Testing Expect")`
}
Recommended Message Style
We recommend that expect
messages are used to describe the reason you expect the Option
should be Some
.
let x: Option<u64> = bar(1);
let value = x.expect("bar() should never return None with 1 as an argument");