pub enum Result<T, E> {
    /// Contains the success value.
    Ok: T,
    /// Contains the error value.
    Err: E,
}
Expand description

Result is a type that represents either success (Ok) or failure (Err).

Variants

Ok: T

Contains the success value.

Err: E

Contains the error value.

Trait Implementations

pub fn abi_encode(self, buffer: Buffer) -> Buffer

pub fn abi_decode(refmut buffer: BufferReader) -> Self

pub fn is_ok(self) -> bool

Returns whether a result contains a success value.

Returns

  • [bool] - Returns true if the result is Ok.

Examples

enum Error {
    NotFound,
    Invalid,
}

fn foo() {
    let x: Result<u64, Error> = Result::Ok(42);
    assert(x.is_ok());

    let y: Result<u64, Error> = Result::Err(Error::NotFound));
    assert(!y.is_ok());
}

pub fn is_err(self) -> bool

Returns whether a result contains an error value.

Returns

  • [bool] - Returns true if the result is Err.

Examples

enum Error {
    NotFound,
    Invalid,
}

fn foo() {
    let x: Result<u64, Error> = Result::Ok(42);
    assert(!x.is_err());

    let y: Result<u64, Error> = Result::Err(Error::NotFound));
    assert(y.is_err());
}

pub fn unwrap(self) -> T

Returns the contained Ok value, consuming the self value.

Additional Information

Because this function may revert, its use is generally discouraged.
Instead, prefer to use pattern matching and handle the Err
case explicitly.

Returns

  • [T] - The value contained by the result.

Reverts

  • Reverts if the Result is the Err variant.

Examples

enum Error {
    NotFound,
    Invalid,
}

fn foo() {
    let x: Result<u64, Error> = Result::Ok(42);
    assert(x.unwrap() == 42);

    let y: Result<u64, Error> = Result::Err(Error::NotFound));
    let val = y.unwrap(); // reverts
}

pub fn unwrap_or(self, default: T) -> T

Returns the contained Ok value or a provided default.

Arguments

  • default: [T] - The value that is the default.

Returns

  • [T] - The value of the result or the default.

Examples

enum Error {
    NotFound,
    Invalid,
}

fn foo() {
    let x: Result<u64, Error> = Result::Ok(42);
    assert(x.unwrap_or(69) == 42);

    let y: Result<u64, Error> = Result::Err(Error::NotFound));
    assert(y.unwrap_or(69) == 69);
}

pub fn expect(self, msg: M) -> T

Returns the contained Ok value, consuming the self value.
If the Result is the Err variant, logs the provided message, along with the error value.

Additional Information

Because this function may revert, its use is generally discouraged.
Instead, prefer to use pattern matching and handle the Err
case explicitly.

Arguments

  • msg: [M] - The message to be logged if the Result is the Err variant.

Returns

  • [T] - The value contained by the result.

Reverts

  • Reverts if the Result is the Err variant.

Examples

enum Error {
    NotFound,
    Invalid,
}

fn foo() {
    let x: Result<u64, Error> = Result::Ok(42);
    assert(x.expect("X is known to be 42") == 42);

    let y: Result<u64, Error> = Result::Err(Error::NotFound));
    let val = y.expect("Testing expect"); // reverts with `("Testing Expect", "Error::NotFound")`
}

Recommended Message Style

We recommend that expect messages are used to describe the reason you expect the Result should be Ok.

let x: Result<u64, Error> = bar(1);
let value = x.expect("bar() should never return Err with 1 as an argument");

pub fn eq(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, otherwise false.

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);
}