Enum std::result::Result 1.0.0
[−]
[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(T)
Contains the success value
Err(E)
Contains the error value
Methods
impl<T, E> Result<T, E>
[src]
impl<T, E> Result<T, E>
pub fn is_ok(&self) -> bool
[src]
pub fn is_ok(&self) -> bool
Returns true
if the result is Ok
.
Examples
Basic usage:
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);Run
pub fn is_err(&self) -> bool
[src]
pub fn is_err(&self) -> bool
Returns true
if the result is Err
.
Examples
Basic usage:
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);Run
pub fn ok(self) -> Option<T>
[src]
pub 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
Basic usage:
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);Run
pub fn err(self) -> Option<E>
[src]
pub 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
Basic usage:
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"));Run
pub fn as_ref(&self) -> Result<&T, &E>
[src]
pub 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.
Examples
Basic usage:
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"));Run
pub fn as_mut(&mut self) -> Result<&mut T, &mut E>
[src]
pub fn as_mut(&mut self) -> Result<&mut T, &mut E>
Converts from Result<T, E>
to Result<&mut T, &mut E>
.
Examples
Basic usage:
fn mutate(r: &mut Result<i32, i32>) { match r.as_mut() { Ok(v) => *v = 42, Err(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);Run
pub fn map<U, F>(self, op: F) -> Result<U, E> where
F: FnOnce(T) -> U,
[src]
pub fn map<U, F>(self, op: F) -> Result<U, E> where
F: FnOnce(T) -> U,
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 line = "1\n2\n3\n4\n"; for num in line.lines() { match num.parse::<i32>().map(|i| i * 2) { Ok(n) => println!("{}", n), Err(..) => {} } }Run
pub fn map_err<F, O>(self, op: O) -> Result<T, F> where
O: FnOnce(E) -> F,
[src]
pub fn map_err<F, O>(self, op: O) -> Result<T, F> where
O: FnOnce(E) -> 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
Basic usage:
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()));Run
ⓘImportant traits for Iter<'a, T>pub fn iter(&self) -> Iter<T>
[src]
pub fn iter(&self) -> Iter<T>
Returns an iterator over the possibly contained value.
The iterator yields one value if the result is Ok
, otherwise none.
Examples
Basic usage:
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);Run
ⓘImportant traits for IterMut<'a, T>pub fn iter_mut(&mut self) -> IterMut<T>
[src]
pub fn iter_mut(&mut self) -> IterMut<T>
Returns a mutable iterator over the possibly contained value.
The iterator yields one value if the result is Ok
, otherwise none.
Examples
Basic usage:
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);Run
pub fn and<U>(self, res: Result<U, E>) -> Result<U, E>
[src]
pub 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
Basic usage:
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"));Run
pub fn and_then<U, F>(self, op: F) -> Result<U, E> where
F: FnOnce(T) -> Result<U, E>,
[src]
pub fn and_then<U, F>(self, op: F) -> Result<U, E> where
F: FnOnce(T) -> 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
Basic usage:
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));Run
pub fn or<F>(self, res: Result<T, F>) -> Result<T, F>
[src]
pub 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
.
Arguments passed to or
are eagerly evaluated; if you are passing the
result of a function call, it is recommended to use or_else
, which is
lazily evaluated.
Examples
Basic usage:
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));Run
pub fn or_else<F, O>(self, op: O) -> Result<T, F> where
O: FnOnce(E) -> Result<T, F>,
[src]
pub fn or_else<F, O>(self, op: O) -> Result<T, F> where
O: FnOnce(E) -> 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
Basic usage:
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));Run
pub fn unwrap_or(self, optb: T) -> T
[src]
pub fn unwrap_or(self, optb: T) -> T
Unwraps a result, yielding the content of an Ok
.
Else, it returns optb
.
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
Basic usage:
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);Run
pub fn unwrap_or_else<F>(self, op: F) -> T where
F: FnOnce(E) -> T,
[src]
pub fn unwrap_or_else<F>(self, op: F) -> T where
F: FnOnce(E) -> T,
impl<T, E> Result<T, E> where
E: Debug,
[src]
impl<T, E> Result<T, E> where
E: Debug,
pub fn unwrap(self) -> T
[src]
pub 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
Basic usage:
let x: Result<u32, &str> = Ok(2); assert_eq!(x.unwrap(), 2);Run
let x: Result<u32, &str> = Err("emergency failure"); x.unwrap(); // panics with `emergency failure`Run
pub fn expect(self, msg: &str) -> T
1.4.0[src]
pub 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
Basic usage:
let x: Result<u32, &str> = Err("emergency failure"); x.expect("Testing expect"); // panics with `Testing expect: emergency failure`Run
impl<T, E> Result<T, E> where
T: Debug,
[src]
impl<T, E> Result<T, E> where
T: Debug,
pub fn unwrap_err(self) -> E
[src]
pub 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
let x: Result<u32, &str> = Ok(2); x.unwrap_err(); // panics with `2`Run
let x: Result<u32, &str> = Err("emergency failure"); assert_eq!(x.unwrap_err(), "emergency failure");Run
pub fn expect_err(self, msg: &str) -> E
1.17.0[src]
pub fn expect_err(self, msg: &str) -> E
Unwraps a result, yielding the content of an Err
.
Panics
Panics if the value is an Ok
, with a panic message including the
passed message, and the content of the Ok
.
Examples
Basic usage:
let x: Result<u32, &str> = Ok(10); x.expect_err("Testing expect_err"); // panics with `Testing expect_err: 10`Run
impl<T, E> Result<T, E> where
T: Default,
[src]
impl<T, E> Result<T, E> where
T: Default,
pub fn unwrap_or_default(self) -> T
1.16.0[src]
pub fn unwrap_or_default(self) -> T
Returns the contained value or a default
Consumes the self
argument then, if Ok
, returns the contained
value, otherwise if Err
, returns the default value for that
type.
Examples
Convert a string to an integer, turning poorly-formed strings
into 0 (the default value for integers). parse
converts
a string to any other type that implements FromStr
, returning an
Err
on error.
let good_year_from_input = "1909"; let bad_year_from_input = "190blarg"; let good_year = good_year_from_input.parse().unwrap_or_default(); let bad_year = bad_year_from_input.parse().unwrap_or_default(); assert_eq!(1909, good_year); assert_eq!(0, bad_year);Run
impl<T, E> Result<Option<T>, E>
[src]
impl<T, E> Result<Option<T>, E>
pub fn transpose(self) -> Option<Result<T, E>>
[src]
pub fn transpose(self) -> Option<Result<T, E>>
Transposes a Result
of an Option
into an Option
of a Result
.
Ok(None)
will be mapped to None
.
Ok(Some(_))
and Err(_)
will be mapped to Some(Ok(_))
and Some(Err(_))
.
Examples
#![feature(transpose_result)] #[derive(Debug, Eq, PartialEq)] struct SomeErr; let x: Result<Option<i32>, SomeErr> = Ok(Some(5)); let y: Option<Result<i32, SomeErr>> = Some(Ok(5)); assert_eq!(x.transpose(), y);Run
Trait Implementations
impl<T, E> Copy for Result<T, E> where
E: Copy,
T: Copy,
[src]
impl<T, E> Copy for Result<T, E> where
E: Copy,
T: Copy,
impl<A, E, V> FromIterator<Result<A, E>> for Result<V, E> where
V: FromIterator<A>,
[src]
impl<A, E, V> FromIterator<Result<A, E>> for Result<V, E> where
V: FromIterator<A>,
fn from_iter<I>(iter: I) -> Result<V, E> where
I: IntoIterator<Item = Result<A, E>>,
[src]
fn from_iter<I>(iter: I) -> Result<V, E> where
I: IntoIterator<Item = Result<A, E>>,
Takes each element in the Iterator
: if it is an Err
, no further
elements are taken, and the Err
is returned. Should no Err
occur, a
container with the values of each Result
is returned.
Here is an example which increments every integer in a vector, checking for overflow:
let v = vec![1, 2]; let res: Result<Vec<u32>, &'static str> = v.iter().map(|x: &u32| x.checked_add(1).ok_or("Overflow!") ).collect(); assert!(res == Ok(vec![2, 3]));Run
impl<T, E> Debug for Result<T, E> where
E: Debug,
T: Debug,
[src]
impl<T, E> Debug for Result<T, E> where
E: Debug,
T: Debug,
fn fmt(&self, __arg_0: &mut Formatter) -> Result<(), Error>
[src]
fn fmt(&self, __arg_0: &mut Formatter) -> Result<(), Error>
Formats the value using the given formatter. Read more
impl<T, U, E> Sum<Result<U, E>> for Result<T, E> where
T: Sum<U>,
1.16.0[src]
impl<T, U, E> Sum<Result<U, E>> for Result<T, E> where
T: Sum<U>,
fn sum<I>(iter: I) -> Result<T, E> where
I: Iterator<Item = Result<U, E>>,
[src]
fn sum<I>(iter: I) -> Result<T, E> where
I: Iterator<Item = Result<U, E>>,
Takes each element in the Iterator
: if it is an Err
, no further
elements are taken, and the Err
is returned. Should no Err
occur,
the sum of all elements is returned.
Examples
This sums up every integer in a vector, rejecting the sum if a negative element is encountered:
let v = vec![1, 2]; let res: Result<i32, &'static str> = v.iter().map(|&x: &i32| if x < 0 { Err("Negative element found") } else { Ok(x) } ).sum(); assert_eq!(res, Ok(3));Run
impl<T, E> Ord for Result<T, E> where
E: Ord,
T: Ord,
[src]
impl<T, E> Ord for Result<T, E> where
E: Ord,
T: Ord,
fn cmp(&self, __arg_0: &Result<T, E>) -> Ordering
[src]
fn cmp(&self, __arg_0: &Result<T, E>) -> Ordering
This method returns an Ordering
between self
and other
. Read more
fn max(self, other: Self) -> Self
1.21.0[src]
fn max(self, other: Self) -> Self
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self
1.21.0[src]
fn min(self, other: Self) -> Self
Compares and returns the minimum of two values. Read more
impl<T, E> Hash for Result<T, E> where
E: Hash,
T: Hash,
[src]
impl<T, E> Hash for Result<T, E> where
E: Hash,
T: Hash,
fn hash<__HTE>(&self, __arg_0: &mut __HTE) where
__HTE: Hasher,
[src]
fn hash<__HTE>(&self, __arg_0: &mut __HTE) where
__HTE: Hasher,
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl<T, E> PartialEq<Result<T, E>> for Result<T, E> where
E: PartialEq<E>,
T: PartialEq<T>,
[src]
impl<T, E> PartialEq<Result<T, E>> for Result<T, E> where
E: PartialEq<E>,
T: PartialEq<T>,
fn eq(&self, __arg_0: &Result<T, E>) -> bool
[src]
fn eq(&self, __arg_0: &Result<T, E>) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &Result<T, E>) -> bool
[src]
fn ne(&self, __arg_0: &Result<T, E>) -> bool
This method tests for !=
.
impl<T, E> IntoIterator for Result<T, E>
[src]
impl<T, E> IntoIterator for Result<T, E>
type Item = T
The type of the elements being iterated over.
type IntoIter = IntoIter<T>
Which kind of iterator are we turning this into?
ⓘImportant traits for IntoIter<T>fn into_iter(self) -> IntoIter<T>
[src]
fn into_iter(self) -> IntoIter<T>
Returns a consuming iterator over the possibly contained value.
The iterator yields one value if the result is Ok
, otherwise none.
Examples
Basic usage:
let x: Result<u32, &str> = Ok(5); let v: Vec<u32> = x.into_iter().collect(); assert_eq!(v, [5]); let x: Result<u32, &str> = Err("nothing!"); let v: Vec<u32> = x.into_iter().collect(); assert_eq!(v, []);Run
impl<'a, T, E> IntoIterator for &'a Result<T, E>
1.4.0[src]
impl<'a, T, E> IntoIterator for &'a Result<T, E>
type Item = &'a T
The type of the elements being iterated over.
type IntoIter = Iter<'a, T>
Which kind of iterator are we turning this into?
ⓘImportant traits for Iter<'a, T>fn into_iter(self) -> Iter<'a, T>
[src]
fn into_iter(self) -> Iter<'a, T>
Creates an iterator from a value. Read more
impl<'a, T, E> IntoIterator for &'a mut Result<T, E>
1.4.0[src]
impl<'a, T, E> IntoIterator for &'a mut Result<T, E>
type Item = &'a mut T
The type of the elements being iterated over.
type IntoIter = IterMut<'a, T>
Which kind of iterator are we turning this into?
ⓘImportant traits for IterMut<'a, T>fn into_iter(self) -> IterMut<'a, T>
[src]
fn into_iter(self) -> IterMut<'a, T>
Creates an iterator from a value. Read more
impl<T, U, E> Product<Result<U, E>> for Result<T, E> where
T: Product<U>,
1.16.0[src]
impl<T, U, E> Product<Result<U, E>> for Result<T, E> where
T: Product<U>,
fn product<I>(iter: I) -> Result<T, E> where
I: Iterator<Item = Result<U, E>>,
[src]
fn product<I>(iter: I) -> Result<T, E> where
I: Iterator<Item = Result<U, E>>,
Takes each element in the Iterator
: if it is an Err
, no further
elements are taken, and the Err
is returned. Should no Err
occur,
the product of all elements is returned.
impl<T, E> Try for Result<T, E>
[src]
impl<T, E> Try for Result<T, E>
type Ok = T
The type of this value when viewed as successful.
type Error = E
The type of this value when viewed as failed.
fn into_result(self) -> Result<T, E>
[src]
fn into_result(self) -> Result<T, E>
Applies the "?" operator. A return of Ok(t)
means that the execution should continue normally, and the result of ?
is the value t
. A return of Err(e)
means that execution should branch to the innermost enclosing catch
, or return from the function. Read more
fn from_ok(v: T) -> Result<T, E>
[src]
fn from_ok(v: T) -> Result<T, E>
Wrap an OK value to construct the composite result. For example, Result::Ok(x)
and Result::from_ok(x)
are equivalent. Read more
fn from_error(v: E) -> Result<T, E>
[src]
fn from_error(v: E) -> Result<T, E>
Wrap an error value to construct the composite result. For example, Result::Err(x)
and Result::from_error(x)
are equivalent. Read more
impl<T, E> Clone for Result<T, E> where
E: Clone,
T: Clone,
[src]
impl<T, E> Clone for Result<T, E> where
E: Clone,
T: Clone,
fn clone(&self) -> Result<T, E>
[src]
fn clone(&self) -> Result<T, E>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
[src]
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl<T, E> PartialOrd<Result<T, E>> for Result<T, E> where
E: PartialOrd<E>,
T: PartialOrd<T>,
[src]
impl<T, E> PartialOrd<Result<T, E>> for Result<T, E> where
E: PartialOrd<E>,
T: PartialOrd<T>,
fn partial_cmp(&self, __arg_0: &Result<T, E>) -> Option<Ordering>
[src]
fn partial_cmp(&self, __arg_0: &Result<T, E>) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, __arg_0: &Result<T, E>) -> bool
[src]
fn lt(&self, __arg_0: &Result<T, E>) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, __arg_0: &Result<T, E>) -> bool
[src]
fn le(&self, __arg_0: &Result<T, E>) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, __arg_0: &Result<T, E>) -> bool
[src]
fn gt(&self, __arg_0: &Result<T, E>) -> bool
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, __arg_0: &Result<T, E>) -> bool
[src]
fn ge(&self, __arg_0: &Result<T, E>) -> bool
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl<T, E> Eq for Result<T, E> where
E: Eq,
T: Eq,
[src]
impl<T, E> Eq for Result<T, E> where
E: Eq,
T: Eq,
impl<E: Debug> Termination for Result<(), E>
[src]
impl<E: Debug> Termination for Result<(), E>
fn report(self) -> i32
[src]
fn report(self) -> i32
Is called to get the representation of the value as status code. This status code is returned to the operating system. Read more
impl<E: Debug> Termination for Result<!, E>
[src]
impl<E: Debug> Termination for Result<!, E>