Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Module 0x1::result

Provides the Result<T, E> type, which allows to represent a success value T or an error value E.

use 0x1::error;

Enum Result

Represents the result of some computation, either a value T or an error E.

enum Result<T, E> has copy, store
Variants
Ok
Fields
0: T
Err
Fields
0: E

Constants

Attempt to unwrap error but found value

const E_UNWRAP_ERR: u64 = 1;

Attempt to unwrap value but found error

const E_UNWRAP_OK: u64 = 0;

Function is_ok

Checks whether the result is Ok.

public fun is_ok<T, E>(self: &result::Result<T, E>): bool
Implementation
public fun is_ok<T, E>(self: &Result<T, E>): bool {
    self is Ok
}

Function is_err

Checks whether the result is Err.

public fun is_err<T, E>(self: &result::Result<T, E>): bool
Implementation
public fun is_err<T, E>(self: &Result<T, E>): bool {
    self is Err
}

Function unwrap

Unpacks the T of Ok or aborts.

public fun unwrap<T, E>(self: result::Result<T, E>): T
Implementation
public fun unwrap<T, E>(self: Result<T, E>): T {
    match (self) {
        Ok(x) => x,
        _ => abort error::invalid_argument(E_UNWRAP_OK)
    }
}

Function unwrap_err

Unpacks the E of Err or aborts.

public fun unwrap_err<T, E>(self: result::Result<T, E>): E
Implementation
public fun unwrap_err<T, E>(self: Result<T, E>): E {
    match (self) {
        Err(x) => x,
        _ => abort error::invalid_argument(E_UNWRAP_ERR)
    }
}