Enum ::std::result::Result

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

Variants

Ok

Contains the success value

Err

Contains the error value

Methods

fn ok(self) -> Option

Converts from Result<T, E> to Option<T>.

Converts self into an Option<T>, consuming self, and discarding the error, if any.

fn is_ok(self) -> bool

Returns true if the result is [Ok].

Examples

let x = Ok(-3);
assert_eq!(x.is_ok(), true);

let x = Err("Some error message");
assert_eq!(x.is_ok(), false);
fn is_err(self) -> bool

Returns true if the result is [Err].

Examples

let x = Ok(-3);
assert_eq!(x.is_err(), false);

let x = Err("Some error message");
assert_eq!(x.is_err(), true);
fn unwrap(self)

Returns the contained [Ok] value, consuming the self value.

Because this function may panic, its use is generally discouraged. Instead, prefer to use pattern matching and handle the [Err] case explicitly, or call unwrap_or, unwrap_or_else, or unwrap_or_default.

Panics

Panics if the value is an [Err], with a panic message provided by the [Err]'s value.

Examples

Basic usage:

let x = Ok(2);
assert_eq!(x.unwrap(), 2);
let x = Err("emergency failure");
x.unwrap(); // panics with `emergency failure`
fn unwrap_or(self, default)

Returns the contained [Ok] value or a provided default.

Arguments passed to unwrap_or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use unwrap_or_else, which is lazily evaluated.

Examples

let default_value = 2;
let x = Ok(9);
assert_eq!(x.unwrap_or(default_value), 9);

let x = Err("error");
assert_eq!(x.unwrap_or(default_value), default_value);
fn unwrap_or_else(self, default: Function)

Returns the contained [Ok] value or computes it from a closure.

Examples

fn count(x) {
   x.len()
}

assert_eq!(Ok(2).unwrap_or_else(count), 2);
assert_eq!(Err("foo").unwrap_or_else(count), 3);
fn expect(self, message)

Returns the contained [Ok] value, consuming the self value.

Because this function may panic, its use is generally discouraged. Instead, prefer to use pattern matching and handle the [Err] case explicitly, or call unwrap_or, unwrap_or_else, or unwrap_or_default.

Panics

Panics if the value is an [Err], with a panic message including the passed message, and the content of the [Err].

Examples

let x = Err("emergency failure");
x.expect("Testing expect"); // panics with `Testing expect: emergency failure`

Recommended Message Style

We recommend that expect messages are used to describe the reason you expect the Result should be Ok. If you're having trouble remembering how to phrase expect error messages remember to focus on the word "should" as in "env variable should be set by blah" or "the given binary should be available and executable by the current user".

Calls op if the result is [Ok], otherwise returns the [Err] value of self.

This function can be used for control flow based on Result values.

Examples

fn sq_then_to_string(x) {
   x.checked_mul(x).ok_or("overflowed")
}

assert_eq!(Ok(2).and_then(sq_then_to_string), Ok(4));
assert_eq!(Ok(i64::MAX).and_then(sq_then_to_string), Err("overflowed"));
assert_eq!(Err("not a number").and_then(sq_then_to_string), Err("not a number"));
fn map(self, then: Function) -> Result

Maps a Result<T, E> to Result<U, E> by applying a function to a contained [Ok] value, leaving an [Err] value untouched.

This function can be used to compose the results of two functions.

Examples

Print the numbers on each line of a string multiplied by two.

let lines = ["1", "2", "3", "4"];
let out = [];

for num in lines {
   out.push(i64::parse(num).map(|i| i * 2)?);
}

assert_eq!(out, [2, 4, 6, 8]);

Protocols

protocol try
value?

Using Result with the try protocol.

Examples

fn maybe_add_one(value) {
   Ok(value? + 1)
}

assert_eq!(maybe_add_one(Ok(4)), Ok(5));
assert_eq!(maybe_add_one(Err("not a number")), Err("not a number"));