Skip to content
Tutorials Center
My learning

Topics

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.

Rust
let r: Result<i32, String> = Ok(3);
r.is_ok();     // true
r.clone().ok(); // Some(3)
r.err();       // None

Try it

Rust From Result to Option and back to a test

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.

Rust Borrowing a Result

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

Rust
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 with match or partition when you need a report.
  • .parse().ok() inside filter_map is the compact idiom for "keep what parses".
  • is_ok() followed by unwrap() is an anti-pattern. Use if let Ok(v) = r or match; they avoid the second check and the possible panic.
  • is_ok_and and is_err_and (stable since Rust 1.70) consume the result. Call them on r.as_ref() or a clone to keep the original.
  • ok() consumes self; to keep a result, call r.as_ref().ok() to get an Option<&T>.
  • Unlike Option, a Result does not implement Copy unless both types do. Cloning a result requires T: Clone and E: Clone.
  • Compare with Option: the same borrowing ideas are described in as_ref, as_mut, as_deref, and Option::ok_or goes the opposite way (ok_or).