Rust Reference ยท Option and Result
Result ok, err, is_ok and is_err
Reference for Rust Result::ok, err, is_ok, is_err, as_ref, as_mut, copied, cloned and iter: convert a Result to an Option and test or borrow it.
Sometimes you only care about one side of a Result. ok() keeps the success value and turns an error into None; err() keeps the error and turns success into None. is_ok() and is_err() answer a yes/no question without extracting anything.
A second group of methods (as_ref, as_mut, as_deref, copied, cloned) changes how a result holds its data, mostly so you can look at it without consuming it. Result also implements IntoIterator, so an Ok value behaves like a one-item iterator and an Err like an empty one.
let r: Result<i32, String> = Ok(3); r.is_ok(); // true r.clone().ok(); // Some(3) r.err(); // None
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
impl<T, E> Result<T, E> { pub const fn is_ok(&self) -> bool; pub const fn is_err(&self) -> bool; pub fn is_ok_and(self, f: impl FnOnce(T) -> bool) -> bool; pub fn is_err_and(self, f: impl FnOnce(E) -> bool) -> bool; pub fn ok(self) -> Option<T>; pub fn err(self) -> Option<E>; pub const fn as_ref(&self) -> Result<&T, &E>; pub const fn as_mut(&mut self) -> Result<&mut T, &mut E>; pub fn as_deref(&self) -> Result<&T::Target, &E> where T: Deref; pub fn iter(&self) -> Iter<'_, T>; } impl<T, E> Result<&T, E> { pub fn copied(self) -> Result<T, E> where T: Copy; pub fn cloned(self) -> Result<T, E> where T: Clone; }
| Method | Returns |
|---|---|
is_ok(), is_err() |
bool, borrowing the result. |
ok() |
Some(v) for Ok(v), otherwise None; the error is dropped. |
err() |
Some(e) for Err(e), otherwise None; the value is dropped. |
as_ref(), as_mut() |
A result of references into the original. |
copied(), cloned() |
An owned Result<T, E> from a Result<&T, E>. |
iter() |
An iterator that yields the Ok value once, or nothing. |
Panics: none.
Usage notes
ok()throws the error away. That is exactly what you want for "skip bad lines" loops, but it hides diagnostics. Keep the error withmatchorpartitionwhen you need a report..parse().ok()insidefilter_mapis the compact idiom for "keep what parses".is_ok()followed byunwrap()is an anti-pattern. Useif let Ok(v) = rormatch; they avoid the second check and the possible panic.is_ok_andandis_err_and(stable since Rust 1.70) consume the result. Call them onr.as_ref()or a clone to keep the original.ok()consumesself; to keep a result, callr.as_ref().ok()to get anOption<&T>.- Unlike
Option, aResultdoes not implementCopyunless both types do. Cloning a result requiresT: CloneandE: Clone. - Compare with
Option: the same borrowing ideas are described inas_ref,as_mut,as_deref, andOption::ok_orgoes the opposite way (ok_or).