Stdlibcore
result
import std::core::result; — in the prelude · source
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}"); }
}
| Method | Returns |
|---|---|
is_ok / is_err | boolean |
unwrap | T — panics on Err |
expect(message) | T |
unwrap_err | E — panics on Ok |
unwrap_or(default_value) | T |
unwrap_or_else(op) | T — op receives the error |
as_ref | Result<T*, E*> — borrows both payloads in place |
map<U>(op) | Result<U, E> |
map_err<F>(op) | Result<T, F> |
and_then<U>(op) | Result<U, E> |
or_else<F>(op) | Result<T, F> |
ok / err | Option<T> / Option<E> |
contains(value) / contains_err(error) | boolean |
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.
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