Result is a type that represents either success (Ok) or failure (Err).
Methods
Converts from Result
to Option
.
Examples
let a = Ok;
let b = Err;
assert_eq!;
assert_eq!;
Returns true
if the result is [Ok
].
Examples
let x = Ok;
assert_eq!;
let x = Err;
assert_eq!;
Returns true
if the result is [Err
].
Examples
let x = Ok;
assert_eq!;
let x = Err;
assert_eq!;
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;
assert_eq!;
let x = Err;
x.unwrap; // panics with `emergency failure`
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;
assert_eq!;
let x = Err;
assert_eq!;
Returns the contained [Ok
] value or computes it from a closure.
Examples
assert_eq!;
assert_eq!;
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;
x.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
assert_eq!;
assert_eq!;
assert_eq!;
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 = ;
let out = ;
for num in lines
assert_eq!;
Trait Implementations
Clone the specified value
.
Examples
let a = 42;
let b = a;
let c = a.clone;
a += 1;
assert_eq!;
assert_eq!;
assert_eq!;
Compare two values for equality.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Compare two values for inequality.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Compare two values.
Examples
use Ordering;
assert_eq!;
assert_eq!;
assert_eq!;
Tests less than (for self
and other
) and is used by the <
operator.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Tests less than or equal to (for self
and other
) and is used by the <=
operator.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Tests greater than (for self
and other
) and is used by the >
operator.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Tests greater than or equal to (for self
and other
) and is used by the >=
operator.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Compare two values.
Examples
use Ordering;
assert_eq!;
assert_eq!;
assert_eq!;
Return the minimum of two values.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Return the maximum of two values.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Protocols
Test if the provided argument is a variant.
let $out = clone
Clone the result.
Examples
let a = Ok;
let b = a.clone;
a?.extend;
assert_eq!;
assert_eq!;
if value == b
Test two results for partial equality.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Using explicit functions:
use partial_eq;
assert_eq!;
assert_eq!;
assert_eq!;
if value == b
Test two results for total equality.
Examples
use eq;
assert_eq!;
assert_eq!;
assert_eq!;
if value < b
Perform a partial ordered comparison between two results.
Examples
assert!;
assert!;
assert!;
Using explicit functions:
use Ordering;
use partial_cmp;
assert_eq!;
assert_eq!;
assert_eq!;
if value < b
Perform a totally ordered comparison between two results.
Examples
use Ordering;
use cmp;
assert_eq!;
assert_eq!;
assert_eq!;