Skip to content
CryoCryo home
Stdlibcore

result

import std::core::result; — in the prelude · source

Types

Result<T, E>

type enum Result<T, E> {
    Ok(T);
    Err(E);
}

implement enum Result<T, E> {
    is_ok(&this) -> boolean;
    is_err(&this) -> boolean;
    as_ref(&this) -> Result<T*, E*>;
    ![sink]
    unwrap(&this) -> T;
    ![sink]
    expect(&this, message: string) -> T;
    ![sink]
    unwrap_err(&this) -> E;
    ![sink]
    unwrap_or(&this, default_value: T) -> T;
    ![sink]
    unwrap_or_else(&this, op: (E) -> T) -> T;
    ![sink]
    map<U>(&this, op: (T) -> U) -> Result<U, E>;
    ![sink]
    map_err<F>(&this, op: (E) -> F) -> Result<T, F>;
    ![sink]
    and_then<U>(&this, op: (T) -> Result<U, E>) -> Result<U, E>;
    ![sink]
    or_else<F>(&this, op: (E) -> Result<T, F>) -> Result<T, F>;
    ![sink]
    ok(&this) -> Option<T>;
    ![sink]
    err(&this) -> Option<E>;

    contains(&this, value: &T) -> boolean
    where T: Eq;

    contains_err(&this, error: &E) -> boolean
    where E: Eq;
}

Every fallible call in the standard library returns Result<T, E> with a module-specific error type. The rule of thumb: if the only way to fail is "it wasn't there", the signature returns Option; if failure carries a reason, it returns Result.

import std::fs::file;

match (file::read_to_string(path)) {
    Result::Ok(text) => { println(f"{text.length()} bytes"); }
    Result::Err(e)   => { eprintln(f"failed: {e}"); }
}

unwrap panics on Err and unwrap_err on Ok; unwrap_or_else hands the error to its callback. as_ref borrows both payloads in place as Result<T*, E*>. map and and_then act on the Ok side, map_err and or_else on the Err side, and ok / err project either into an Option.

As on Option, the ![sink] methods consume the receiver. The ? operator propagates an Err out of the enclosing function; see Operators.

Every error type in the standard library exposes the same accessor — describe(&this) -> Str — and implements Display, so you can report a failure without knowing which module it came from.

Variants

Variant
Ok(T)
Err(E)

Methods

is_ok(&this) -> boolean;
is_err(&this) -> boolean;
as_ref(&this) -> Result<T*, E*>;

Borrow both payloads in place: Ok(v) -> Ok(&v), Err(e) -> Err(&e). The pointers alias the receiver's payload (no copy), so unlike the consuming accessors this is NOT ![sink] - the receiver stays live and keeps ownership of T/E. The pointers are valid only while the receiver outlives them.

![sink] unwrap(&this) -> T;

![sink]: the match-arm binding copies the payload out of the receiver, so the receiver must be treated as moved at the call site - otherwise both the returned value and the still-live receiver would be dropped, double-freeing an owning T/E.

![sink] expect(&this, message: string) -> T;
![sink] unwrap_err(&this) -> E;

Mirror of unwrap for error paths. Useful in tests that assert an operation fails.

![sink] unwrap_or(&this, default_value: T) -> T;
![sink] unwrap_or_else(&this, op: (E) -> T) -> T;
![sink] map<U>(&this, op: (T) -> U) -> Result<U, E>;
![sink] map_err<F>(&this, op: (E) -> F) -> Result<T, F>;

Adapt an inner error to a caller's outer error type.

![sink] and_then<U>(&this, op: (T) -> Result<U, E>) -> Result<U, E>;
![sink] or_else<F>(&this, op: (E) -> Result<T, F>) -> Result<T, F>;
![sink] ok(&this) -> Option<T>;

Project the success side to an Option, discarding the error.

![sink] err(&this) -> Option<E>;

Project the error side to an Option, discarding the success.

contains(&this, value: &T) -> boolean where T: Eq;

True if the result is Ok(x) where x.equals(&value).

contains_err(&this, error: &E) -> boolean where E: Eq;

True if the result is Err(e) where e.equals(&error).

Trait implementations

implement<T, E> trait Clone for enum Result<T, E> where T: Clone, E: Clone
implement<T, E> trait Eq for enum Result<T, E> where T: Eq, E: Eq
implement<T, E> trait Display for enum Result<T, E> where T: Display, E: Display
std::fmt::display
implement<T, E> trait Debug for enum Result<T, E> where T: Debug, E: Debug
std::fmt::display