Enum std::result::Result

Overview

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

Methods

fn ok(self) -> Option

Converts from Result to Option.

Examples

let a = Ok(2);
let b = Err(3);

assert_eq!(a.ok(), Some(2));
assert_eq!(b.ok(), None);
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) -> any

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: any) -> any

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) -> any

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: any) -> any

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(u64::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]);

Trait Implementations

impl Clone for Result
fn clone(value: any) -> any

Clone the specified value.

Examples

let a = 42;
let b = a;
let c = a.clone();

a += 1;

assert_eq!(a, 43);
assert_eq!(b, 42);
assert_eq!(c, 42);
impl PartialEq for Result
fn eq(value: any, value1: any) -> bool

Compare two values for equality.

Examples

assert_eq!(1.eq(2), false);
assert_eq!(2.eq(2), true);
assert_eq!(2.eq(1), false);
fn ne(value: any, value1: any) -> bool

Compare two values for inequality.

Examples

assert_eq!(1.ne(2), true);
assert_eq!(2.ne(2), false);
assert_eq!(2.ne(1), true);
impl Eq for Result
impl PartialOrd for Result
fn partial_cmp(value: any, value1: any) -> Option

Compare two values.

Examples

use std::cmp::Ordering;

assert_eq!(1.partial_cmp(2), Some(Ordering::Less));
assert_eq!(2.partial_cmp(2), Some(Ordering::Equal));
assert_eq!(2.partial_cmp(1), Some(Ordering::Greater));
fn lt(value: any, value1: any) -> bool

Tests less than (for self and other) and is used by the < operator.

Examples

assert_eq!(1.0 < 1.0, false);
assert_eq!(1.0 < 2.0, true);
assert_eq!(2.0 < 1.0, false);
fn le(value: any, value1: any) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator.

Examples

assert_eq!(1.0 <= 1.0, true);
assert_eq!(1.0 <= 2.0, true);
assert_eq!(2.0 <= 1.0, false);
fn gt(value: any, value1: any) -> bool

Tests greater than (for self and other) and is used by the > operator.

Examples

assert_eq!(1.0 > 1.0, false);
assert_eq!(1.0 > 2.0, false);
assert_eq!(2.0 > 1.0, true);
fn ge(value: any, value1: any) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator.

Examples

assert_eq!(1.0 >= 1.0, true);
assert_eq!(1.0 >= 2.0, false);
assert_eq!(2.0 >= 1.0, true);
impl Ord for Result
fn cmp(value: any, value1: any) -> Ordering

Compare two values.

Examples

use std::cmp::Ordering;

assert_eq!(1.cmp(2), Ordering::Less);
assert_eq!(2.cmp(2), Ordering::Equal);
assert_eq!(2.cmp(1), Ordering::Greater);
fn min(value: any, value1: any) -> Ordering

Return the minimum of two values.

Examples

assert_eq!(1.min(2), 1);
assert_eq!(2.min(2), 2);
assert_eq!(2.min(1), 1);
fn max(value: any, value1: any) -> Ordering

Return the maximum of two values.

Examples

assert_eq!(1.max(2), 2);
assert_eq!(2.max(2), 2);
assert_eq!(2.max(1), 2);

Protocols

protocol IS_VARIANT

Test if the provided argument is a variant.

protocol GET
let $out = value.0

Allows a get operation to work.

protocol CLONE
let $out = clone(value)

Clone the result.

Examples

let a = Ok(b"hello world");
let b = a.clone();

a?.extend(b"!");

assert_eq!(a, Ok(b"hello world!"));
assert_eq!(b, Ok(b"hello world"));
protocol PARTIAL_EQ
if value == b { }

Test two results for partial equality.

Examples

assert_eq!(Ok(b"a") == Ok(b"a"), true);
assert_eq!(Ok(b"a") == Ok(b"ab"), false);
assert_eq!(Ok(b"ab") == Ok(b"a"), false);

Using explicit functions:

use std::ops::partial_eq;

assert_eq!(partial_eq(Ok(b"a"), Ok(b"a")), true);
assert_eq!(partial_eq(Ok(b"a"), Ok(b"ab")), false);
assert_eq!(partial_eq(Ok(b"ab"), Ok(b"a")), false);
protocol EQ
if value == b { }

Test two results for total equality.

Examples

use std::ops::eq;

assert_eq!(eq(Ok(b"a"), Ok(b"a")), true);
assert_eq!(eq(Ok(b"a"), Ok(b"ab")), false);
assert_eq!(eq(Ok(b"ab"), Ok(b"a")), false);
protocol PARTIAL_CMP
if value < b { }

Perform a partial ordered comparison between two results.

Examples

assert!(Ok(b"a") < Ok(b"ab"));
assert!(Ok(b"ab") > Ok(b"a"));
assert!(Ok(b"a") == Ok(b"a"));

Using explicit functions:

use std::cmp::Ordering;
use std::ops::partial_cmp;

assert_eq!(partial_cmp(Ok(b"a"), Ok(b"ab")), Some(Ordering::Less));
assert_eq!(partial_cmp(Ok(b"ab"), Ok(b"a")), Some(Ordering::Greater));
assert_eq!(partial_cmp(Ok(b"a"), Ok(b"a")), Some(Ordering::Equal));
protocol CMP
if value < b { }

Perform a totally ordered comparison between two results.

Examples

use std::cmp::Ordering;
use std::ops::cmp;

assert_eq!(cmp(Ok(b"a"), Ok(b"ab")), Ordering::Less);
assert_eq!(cmp(Ok(b"ab"), Ok(b"a")), Ordering::Greater);
assert_eq!(cmp(Ok(b"a"), Ok(b"a")), Ordering::Equal);
protocol HASH
let $out = hash(value)

Hash the result.

Examples

use std::ops::hash;

let a = Ok("hello");
let b = Ok("hello");

assert_eq!(hash(a), hash(b));
protocol DEBUG_FMT
format!("{:?}", value)

Write a debug representation of a result.

Examples

println!("{:?}", Ok("Hello"));
println!("{:?}", Err("Hello"));
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"));
protocol LT
if $a < $b { }

The protocol behind the < operator.

protocol LE
if $a <= $b { }

The protocol behind the <= operator.

protocol GT
if $a > $b { }

The protocol behind the > operator.

protocol GE
if $a >= $b { }

The protocol behind the >= operator.

protocol MIN
$a.min($b)

The implementation protocol for the PartialOrd::min method.

protocol MAX
$a.max($b)

The implementation protocol for the PartialOrd::max method.