Result is a type that represents either success (Ok) or failure (Err).
Variants
Contains the success value
Contains the error value
Methods
Converts from Result<T, E>
to Option<T>
.
Converts self into an Option<T>
, consuming self
, and discarding the error, if any.
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!;