# Enum core::result::Result [−] [src]

    #[must_use]
    pub enum Result<T, E> {
        Ok(T),
        Err(E),
    }

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

See the `std::result` module documentation for details.

## Variants

`Ok` | Contains the success value
`Err` | Contains the error value

## Methods

### `impl<T, E> Result<T, E>`

#### `fn is_ok(&self) -> bool`

Returns true if the result is `Ok`

# Examples

fn main() { let x: Result<i32, &str> = Ok(-3); assert_eq!(x.is_ok(), true); let x: Result<i32, &str> = Err("Some error message"); assert_eq!(x.is_ok(), false); }

    let x: Result<i32, &str> = Ok(-3);
    assert_eq!(x.is_ok(), true);

    let x: Result<i32, &str> = Err("Some error message");
    assert_eq!(x.is_ok(), false);

#### `fn is_err(&self) -> bool`

Returns true if the result is `Err`

# Examples

fn main() { let x: Result<i32, &str> = Ok(-3); assert_eq!(x.is_err(), false); let x: Result<i32, &str> = Err("Some error message"); assert_eq!(x.is_err(), true); }

    let x: Result<i32, &str> = Ok(-3);
    assert_eq!(x.is_err(), false);

    let x: Result<i32, &str> = Err("Some error message");
    assert_eq!(x.is_err(), true);

#### `fn ok(self) -> Option<T>`

Converts from `Result<T, E>` to `Option<T>`

Converts `self` into an `Option<T>`, consuming `self`, and discarding the error, if any.

# Examples

fn main() { let x: Result<u32, &str> = Ok(2); assert_eq!(x.ok(), Some(2)); let x: Result<u32, &str> = Err("Nothing here"); assert_eq!(x.ok(), None); }

    let x: Result<u32, &str> = Ok(2);
    assert_eq!(x.ok(), Some(2));

    let x: Result<u32, &str> = Err("Nothing here");
    assert_eq!(x.ok(), None);

#### `fn err(self) -> Option<E>`

Converts from `Result<T, E>` to `Option<E>`

Converts `self` into an `Option<E>`, consuming `self`, and discarding the success value, if any.

# Examples

fn main() { let x: Result<u32, &str> = Ok(2); assert_eq!(x.err(), None); let x: Result<u32, &str> = Err("Nothing here"); assert_eq!(x.err(), Some("Nothing here")); }

    let x: Result<u32, &str> = Ok(2);
    assert_eq!(x.err(), None);

    let x: Result<u32, &str> = Err("Nothing here");
    assert_eq!(x.err(), Some("Nothing here"));

#### `fn as_ref(&self) -> Result<&T, &E>`

Converts from `Result<T, E>` to `Result<&T, &E>`

Produces a new `Result`, containing a reference into the original, leaving the original in place.

fn main() { let x: Result<u32, &str> = Ok(2); assert_eq!(x.as_ref(), Ok(&2)); let x: Result<u32, &str> = Err("Error"); assert_eq!(x.as_ref(), Err(&"Error")); }

    let x: Result<u32, &str> = Ok(2);
    assert_eq!(x.as_ref(), Ok(&2));

    let x: Result<u32, &str> = Err("Error");
    assert_eq!(x.as_ref(), Err(&"Error"));

#### `fn as_mut(&mut self) -> Result<&mut T, &mut E>`

Converts from `Result<T, E>` to `Result<&mut T, &mut E>`

fn main() { fn mutate(r: &mut Result<i32, i32>) { match r.as_mut() { Ok(&mut ref mut v) => *v = 42, Err(&mut ref mut e) => *e = 0, } } let mut x: Result<i32, i32> = Ok(2); mutate(&mut x); assert_eq!(x.unwrap(), 42); let mut x: Result<i32, i32> = Err(13); mutate(&mut x); assert_eq!(x.unwrap_err(), 0); }

    fn mutate(r: &mut Result<i32, i32>) {
        match r.as_mut() {
            Ok(&mut ref mut v) => *v = 42,
            Err(&mut ref mut e) => *e = 0,
        }
    }

    let mut x: Result<i32, i32> = Ok(2);
    mutate(&mut x);
    assert_eq!(x.unwrap(), 42);

    let mut x: Result<i32, i32> = Err(13);
    mutate(&mut x);
    assert_eq!(x.unwrap_err(), 0);

#### `fn map<U, F: FnOnce(T) -> U>(self, op: F) -> Result<U, E>`

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.

fn main() { let line = "1\n2\n3\n4\n"; for num in line.lines() { match num.parse::<i32>().map(|i| i * 2) { Ok(n) => println!("{}", n), Err(..) => {} } } }

    let line = "1\n2\n3\n4\n";

    for num in line.lines() {
        match num.parse::<i32>().map(|i| i * 2) {
            Ok(n) => println!("{}", n),
            Err(..) => {}
        }
    }

#### `fn map_err<F, O: FnOnce(E) -> F>(self, op: O) -> Result<T, F>`

Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Err` value, leaving an `Ok` value untouched.

This function can be used to pass through a successful result while handling an error.

# Examples

fn main() { fn stringify(x: u32) -> String { format!("error code: {}", x) } let x: Result<u32, u32> = Ok(2); assert_eq!(x.map_err(stringify), Ok(2)); let x: Result<u32, u32> = Err(13); assert_eq!(x.map_err(stringify), Err("error code: 13".to_string())); }

    fn stringify(x: u32) -> String { format!("error code: {}", x) }

    let x: Result<u32, u32> = Ok(2);
    assert_eq!(x.map_err(stringify), Ok(2));

    let x: Result<u32, u32> = Err(13);
    assert_eq!(x.map_err(stringify), Err("error code: 13".to_string()));

#### `fn iter(&self) -> Iter<T>`

Returns an iterator over the possibly contained value.

# Examples

fn main() { let x: Result<u32, &str> = Ok(7); assert_eq!(x.iter().next(), Some(&7)); let x: Result<u32, &str> = Err("nothing!"); assert_eq!(x.iter().next(), None); }

    let x: Result<u32, &str> = Ok(7);
    assert_eq!(x.iter().next(), Some(&7));

    let x: Result<u32, &str> = Err("nothing!");
    assert_eq!(x.iter().next(), None);

#### `fn iter_mut(&mut self) -> IterMut<T>`

Returns a mutable iterator over the possibly contained value.

# Examples

fn main() { let mut x: Result<u32, &str> = Ok(7); match x.iter_mut().next() { Some(v) => *v = 40, None => {}, } assert_eq!(x, Ok(40)); let mut x: Result<u32, &str> = Err("nothing!"); assert_eq!(x.iter_mut().next(), None); }

    let mut x: Result<u32, &str> = Ok(7);
    match x.iter_mut().next() {
        Some(v) => *v = 40,
        None => {},
    }
    assert_eq!(x, Ok(40));

    let mut x: Result<u32, &str> = Err("nothing!");
    assert_eq!(x.iter_mut().next(), None);

#### `fn and<U>(self, res: Result<U, E>) -> Result<U, E>`

Returns `res` if the result is `Ok`, otherwise returns the `Err` value of `self`.

# Examples

fn main() { let x: Result<u32, &str> = Ok(2); let y: Result<&str, &str> = Err("late error"); assert_eq!(x.and(y), Err("late error")); let x: Result<u32, &str> = Err("early error"); let y: Result<&str, &str> = Ok("foo"); assert_eq!(x.and(y), Err("early error")); let x: Result<u32, &str> = Err("not a 2"); let y: Result<&str, &str> = Err("late error"); assert_eq!(x.and(y), Err("not a 2")); let x: Result<u32, &str> = Ok(2); let y: Result<&str, &str> = Ok("different result type"); assert_eq!(x.and(y), Ok("different result type")); }

    let x: Result<u32, &str> = Ok(2);
    let y: Result<&str, &str> = Err("late error");
    assert_eq!(x.and(y), Err("late error"));

    let x: Result<u32, &str> = Err("early error");
    let y: Result<&str, &str> = Ok("foo");
    assert_eq!(x.and(y), Err("early error"));

    let x: Result<u32, &str> = Err("not a 2");
    let y: Result<&str, &str> = Err("late error");
    assert_eq!(x.and(y), Err("not a 2"));

    let x: Result<u32, &str> = Ok(2);
    let y: Result<&str, &str> = Ok("different result type");
    assert_eq!(x.and(y), Ok("different result type"));

#### `fn and_then<U, F: FnOnce(T) -> Result<U, E>>(self, op: F) -> Result<U, E>`

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 main() { fn sq(x: u32) -> Result<u32, u32> { Ok(x * x) } fn err(x: u32) -> Result<u32, u32> { Err(x) } assert_eq!(Ok(2).and_then(sq).and_then(sq), Ok(16)); assert_eq!(Ok(2).and_then(sq).and_then(err), Err(4)); assert_eq!(Ok(2).and_then(err).and_then(sq), Err(2)); assert_eq!(Err(3).and_then(sq).and_then(sq), Err(3)); }

    fn sq(x: u32) -> Result<u32, u32> { Ok(x * x) }
    fn err(x: u32) -> Result<u32, u32> { Err(x) }

    assert_eq!(Ok(2).and_then(sq).and_then(sq), Ok(16));
    assert_eq!(Ok(2).and_then(sq).and_then(err), Err(4));
    assert_eq!(Ok(2).and_then(err).and_then(sq), Err(2));
    assert_eq!(Err(3).and_then(sq).and_then(sq), Err(3));

#### `fn or<F>(self, res: Result<T, F>) -> Result<T, F>`

Returns `res` if the result is `Err`, otherwise returns the `Ok` value of `self`.

# Examples

fn main() { let x: Result<u32, &str> = Ok(2); let y: Result<u32, &str> = Err("late error"); assert_eq!(x.or(y), Ok(2)); let x: Result<u32, &str> = Err("early error"); let y: Result<u32, &str> = Ok(2); assert_eq!(x.or(y), Ok(2)); let x: Result<u32, &str> = Err("not a 2"); let y: Result<u32, &str> = Err("late error"); assert_eq!(x.or(y), Err("late error")); let x: Result<u32, &str> = Ok(2); let y: Result<u32, &str> = Ok(100); assert_eq!(x.or(y), Ok(2)); }

    let x: Result<u32, &str> = Ok(2);
    let y: Result<u32, &str> = Err("late error");
    assert_eq!(x.or(y), Ok(2));

    let x: Result<u32, &str> = Err("early error");
    let y: Result<u32, &str> = Ok(2);
    assert_eq!(x.or(y), Ok(2));

    let x: Result<u32, &str> = Err("not a 2");
    let y: Result<u32, &str> = Err("late error");
    assert_eq!(x.or(y), Err("late error"));

    let x: Result<u32, &str> = Ok(2);
    let y: Result<u32, &str> = Ok(100);
    assert_eq!(x.or(y), Ok(2));

#### `fn or_else<F, O: FnOnce(E) -> Result<T, F>>(self, op: O) -> Result<T, F>`

Calls `op` if the result is `Err`, otherwise returns the `Ok` value of `self`.

This function can be used for control flow based on result values.

# Examples

fn main() { fn sq(x: u32) -> Result<u32, u32> { Ok(x * x) } fn err(x: u32) -> Result<u32, u32> { Err(x) } assert_eq!(Ok(2).or_else(sq).or_else(sq), Ok(2)); assert_eq!(Ok(2).or_else(err).or_else(sq), Ok(2)); assert_eq!(Err(3).or_else(sq).or_else(err), Ok(9)); assert_eq!(Err(3).or_else(err).or_else(err), Err(3)); }

    fn sq(x: u32) -> Result<u32, u32> { Ok(x * x) }
    fn err(x: u32) -> Result<u32, u32> { Err(x) }

    assert_eq!(Ok(2).or_else(sq).or_else(sq), Ok(2));
    assert_eq!(Ok(2).or_else(err).or_else(sq), Ok(2));
    assert_eq!(Err(3).or_else(sq).or_else(err), Ok(9));
    assert_eq!(Err(3).or_else(err).or_else(err), Err(3));

#### `fn unwrap_or(self, optb: T) -> T`

Unwraps a result, yielding the content of an `Ok`. Else it returns `optb`.

# Examples

fn main() { let optb = 2; let x: Result<u32, &str> = Ok(9); assert_eq!(x.unwrap_or(optb), 9); let x: Result<u32, &str> = Err("error"); assert_eq!(x.unwrap_or(optb), optb); }

    let optb = 2;
    let x: Result<u32, &str> = Ok(9);
    assert_eq!(x.unwrap_or(optb), 9);

    let x: Result<u32, &str> = Err("error");
    assert_eq!(x.unwrap_or(optb), optb);

#### `fn unwrap_or_else<F: FnOnce(E) -> T>(self, op: F) -> T`

Unwraps a result, yielding the content of an `Ok`. If the value is an `Err` then it calls `op` with its value.

# Examples

fn main() { fn count(x: &str) -> usize { x.len() } assert_eq!(Ok(2).unwrap_or_else(count), 2); assert_eq!(Err("foo").unwrap_or_else(count), 3); }

    fn count(x: &str) -> usize { x.len() }

    assert_eq!(Ok(2).unwrap_or_else(count), 2);
    assert_eq!(Err("foo").unwrap_or_else(count), 3);

### `impl<T, E: Debug> Result<T, E>`

#### `fn unwrap(self) -> T`

Unwraps a result, yielding the content of an `Ok`.

# Panics

Panics if the value is an `Err`, with a panic message provided by the `Err`'s value.

# Examples

fn main() { let x: Result<u32, &str> = Ok(2); assert_eq!(x.unwrap(), 2); }

    let x: Result<u32, &str> = Ok(2);
    assert_eq!(x.unwrap(), 2);

fn main() { let x: Result<u32, &str> = Err("emergency failure"); x.unwrap(); // panics with `emergency failure` }

    let x: Result<u32, &str> = Err("emergency failure");
    x.unwrap(); // panics with `emergency failure`

#### `fn expect(self, msg: &str) -> T`

Unwraps a result, yielding the content of an `Ok`.

# Panics

Panics if the value is an `Err`, with a panic message including the passed message, and the content of the `Err`.

# Examples

fn main() { let x: Result<u32, &str> = Err("emergency failure"); x.expect("Testing expect"); // panics with `Testing expect: emergency failure` }

    let x: Result<u32, &str> = Err("emergency failure");
    x.expect("Testing expect"); // panics with `Testing expect: emergency failure`

### `impl<T: Debug, E> Result<T, E>`

#### `fn unwrap_err(self) -> E`

Unwraps a result, yielding the content of an `Err`.

# Panics

Panics if the value is an `Ok`, with a custom panic message provided by the `Ok`'s value.

# Examples

fn main() { let x: Result<u32, &str> = Ok(2); x.unwrap_err(); // panics with `2` }

    let x: Result<u32, &str> = Ok(2);
    x.unwrap_err(); // panics with `2`

fn main() { let x: Result<u32, &str> = Err("emergency failure"); assert_eq!(x.unwrap_err(), "emergency failure"); }

    let x: Result<u32, &str> = Err("emergency failure");
    assert_eq!(x.unwrap_err(), "emergency failure");

## Trait Implementations

### `impl<T, E> IntoIterator for Result<T, E>`

#### `type Item = T`

#### `type IntoIter = IntoIter<T>`

#### `fn into_iter(self) -> IntoIter<T>`

### `impl<'a, T, E> IntoIterator for &'a Result<T, E>`

#### `type Item = &'a T`

#### `type IntoIter = Iter<'a, T>`

#### `fn into_iter(self) -> Iter<'a, T>`

### `impl<'a, T, E> IntoIterator for &'a mut Result<T, E>`

#### `type Item = &'a mut T`

#### `type IntoIter = IterMut<'a, T>`

#### `fn into_iter(self) -> IterMut<'a, T>`

### `impl<A, E, V: FromIterator<A>> FromIterator<Result<A, E>> for Result<V, E>`

#### `fn from_iter<I: IntoIterator<Item=Result<A, E>>>(iter: I) -> Result<V, E>`

### Derived Implementations

### `impl<T: Hash, E: Hash> Hash for Result<T, E>`

#### `fn hash<__H: Hasher>(&self, __arg_0: &mut __H)`

#### `fn hash_slice<H: Hasher>(data: &[Self], state: &mut H) where Self: Sized`

### `impl<T: Debug, E: Debug> Debug for Result<T, E>`

#### `fn fmt(&self, __arg_0: &mut Formatter) -> Result`

### `impl<T: Ord, E: Ord> Ord for Result<T, E>`

#### `fn cmp(&self, __arg_0: &Result<T, E>) -> Ordering`

### `impl<T: Eq, E: Eq> Eq for Result<T, E>`

### `impl<T: PartialOrd, E: PartialOrd> PartialOrd for Result<T, E>`

#### `fn partial_cmp(&self, __arg_0: &Result<T, E>) -> Option<Ordering>`

#### `fn lt(&self, __arg_0: &Result<T, E>) -> bool`

#### `fn le(&self, __arg_0: &Result<T, E>) -> bool`

#### `fn gt(&self, __arg_0: &Result<T, E>) -> bool`

#### `fn ge(&self, __arg_0: &Result<T, E>) -> bool`

### `impl<T: PartialEq, E: PartialEq> PartialEq for Result<T, E>`

#### `fn eq(&self, __arg_0: &Result<T, E>) -> bool`

#### `fn ne(&self, __arg_0: &Result<T, E>) -> bool`

### `impl<T: Copy, E: Copy> Copy for Result<T, E>`

### `impl<T: Clone, E: Clone> Clone for Result<T, E>`

#### `fn clone(&self) -> Result<T, E>`

#### `fn clone_from(&mut self, source: &Self)`

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize`)