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*>;
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] 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;
![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>;
![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>;
![sink] ok(&this) -> Option<T>;Project the success side to an Option, discarding the error.
![sink] err(&this) -> Option<E>;
![sink] err(&this) -> Option<E>;Project the error side to an Option, discarding the success.
contains(&this, value: &T) -> boolean where T: Eq;
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;
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
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type enum Name {
Unit,
Payload(T),
Named { x: i32 },
}An algebraic data type. Variants may be bare or carry a tuple or struct payload, and a match on an enum must cover every variant. Name::Variant names a variant; methods are added with implement enum Name { ... }.
type enum Result<T, E> {
Ok(T);
Err(E);
}Result: a value that may be an error.
type parameter Ttype parameter EResult::Ok(T)type parameter TResult::Err(E)type parameter Eimplement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement enum Name { ... }Names the enum an inherent block extends - the only way to give an enum methods.
language referenceResult::is_ok(&this) -> boolean&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
Result::is_err(&this) -> booleanResult::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.
type parameter Ttype parameter EResult::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.
Result::expect(&this, message: string) -> T(parameter) message: stringprimitive stringNUL-terminated raw string (u8*), matching the C ABI. Length-typed text is Str / String.
Result::unwrap_err(&this) -> EMirror of unwrap for error paths. Useful in tests that assert an operation fails.
Result::unwrap_or(&this, default_value: T) -> T(parameter) default_value: TResult::unwrap_or_else(&this, op: (E) -> T) -> T(parameter) op: (E) -> TResult::map<U>(&this, op: (T) -> U) -> Result<U, E>type parameter U(parameter) op: (T) -> Utype parameter UResult::map_err<F>(&this, op: (E) -> F) -> Result<T, F>Adapt an inner error to a caller's outer error type.
type parameter F(parameter) op: (E) -> Ftype parameter FResult::and_then<U>(&this, op: (T) -> Result<U, E>) -> Result<U, E>(parameter) op: (T) -> Result<U, E>type parameter UResult::or_else<F>(&this, op: (E) -> Result<T, F>) -> Result<T, F>(parameter) op: (E) -> Result<T, F>type parameter FResult::ok(&this) -> Option<T>Project the success side to an Option, discarding the error.
Result::err(&this) -> Option<E>Project the error side to an Option, discarding the success.
Result::contains(&this, value: &T) -> boolean
where T: EqTrue if the result is Ok(x) where x.equals(&value).
(parameter) value: &Twhere T: Ord + Clone, U: HashConstrains generic parameters to types that implement the listed traits, which is what makes those traits' methods callable on T. Bounds on one parameter join with +; parameters separate with ,. A where on a single method narrows the type's parameter for that method only.
type trait Eq {
equals(&this, other: &This) -> boolean;
}Types that support value equality. equals(a, b) must be reflexive, symmetric, and transitive.
Result::contains_err(&this, error: &E) -> boolean
where E: EqTrue if the result is Err(e) where e.equals(&error).
(parameter) error: &Eimport a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
match (subject) {
Pattern(x) => { ... }
_ => { ... }
}The primary discriminator: branches on enum variants, integers, ranges, and destructuring patterns, and the compiler rejects a match that misses a case. There is no fallthrough. As an expression it evaluates to the chosen arm's value, so every arm must produce the same type.
function read_to_string(p: Path) -> Result<String, IoError>Read the entire contents of p into a fresh String. The bytes are taken verbatim; no UTF-8 validation is performed (the caller is responsible for that if it matters). Panics on allocation failure; I/O failure surfaces through the Result.
Result::Ok(T)text: Stringpattern => { body }Separates a match arm's pattern from its body. Arms do not fall through.
text: StringString::length(&this) -> u64Result::Err(E)e: IoErrore: IoErrorResult::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.
Result::unwrap_err(&this) -> EMirror of unwrap for error paths. Useful in tests that assert an operation fails.
Result::unwrap_or_else(&this, op: (E) -> T) -> TResult::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.
Result::map<U>(&this, op: (T) -> U) -> Result<U, E>Result::and_then<U>(&this, op: (T) -> Result<U, E>) -> Result<U, E>Result::map_err<F>(&this, op: (E) -> F) -> Result<T, F>Adapt an inner error to a caller's outer error type.
std::core::resultResult::or_else<F>(&this, op: (E) -> Result<T, F>) -> Result<T, F>Result::ok(&this) -> Option<T>Project the success side to an Option, discarding the error.
Result::err(&this) -> Option<E>Project the error side to an Option, discarding the success.
type struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::strtype trait Display {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}Display / Debug / Formatter.
std::fmt::displayconst E: f64 = 2.718281828459045;Result::is_ok(&this) -> booleanResult::is_err(&this) -> booleanResult::expect(&this, message: string) -> TResult::unwrap_or(&this, default_value: T) -> TResult::contains(&this, value: &T) -> boolean
where T: EqTrue if the result is Ok(x) where x.equals(&value).
Result::contains_err(&this, error: &E) -> boolean
where E: EqTrue if the result is Err(e) where e.equals(&error).
type parameter Twhere T: Clone
type parameter Ewhere E: Clone
type trait Name : Base {
method(&this, other: &This) -> Ret;
}Names a set of methods a type may implement. Inside the body This is the implementing type; methods may carry default bodies, and a trait may require a base trait (type trait Ord : Eq). Generic code asks for a trait with where T: Name.
implement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referencetype parameter Twhere T: Eq
type parameter Ewhere E: Eq
type parameter Twhere T: Display
type parameter Ewhere E: Display
type parameter Twhere T: Debug
type parameter Ewhere E: Debug
type trait Debug {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}