A type representing either success or failure
Result
- Result
is a type that represents either success (Ok
) or failure (Err
).of ::std::clone::Clone for Result<T, E> where <T: ::std::clone::Clone, E: ::std::clone::Clone>
- Automatically derived.of ::std::cmp::Eq for Result<T, E> where <T: ::std::cmp::Eq, E: ::std::cmp::Eq>
- Automatically derived.for Result<T, E> where <T, E: ToStr>
for Result<T, E> where <T: Clone, E: ToStr>
for Result<T, E> where <T, E: Clone + ToStr>
collect
- Takes each element in the iterator: if it is an error, no further elements are taken, and the error is returnedfold
- Perform a fold operation over the result values from an iterator.fold_
- Perform a trivial fold operation over the result values from an iterator.map_opt
Result
Result
is a type that represents either success (Ok
) or failure (Err
).
In order to provide informative error messages, E
is required to implement ToStr
. It is further recommended for E
to be a descriptive error type, eg a enum
for all possible errors cases.
Ok(T)
- Contains the successful result value
Err(E)
- Contains the error value
::std::clone::Clone
for Result<T, E>
where <T: ::std::clone::Clone, E: ::std::clone::Clone>
Automatically derived.
clone
fn clone(&self) -> Result<T, E>
::std::cmp::Eq
for Result<T, E>
where <T: ::std::cmp::Eq, E: ::std::cmp::Eq>
Automatically derived.
eq
fn eq(&self, __arg_0: &Result<T, E>) -> ::bool
ne
fn ne(&self, __arg_0: &Result<T, E>) -> ::bool
Result<T, E>
where <T, E: ToStr>
to_either
fn to_either(self) -> either::Either<E, T>
Convert to the either
type
Ok
result variants are converted to either::Right
variants, Err
result variants are converted to either::Left
.
get_ref
fn get_ref<'a>(&'a self) -> &'a T
Get a reference to the value out of a successful result
If the result is an error
is_ok
fn is_ok(&self) -> bool
Returns true if the result is Ok
is_err
fn is_err(&self) -> bool
Returns true if the result is Err
iter
fn iter<'r>(&'r self) -> OptionIterator<&'r T>
Call a method based on a previous result
If self
is Ok
then the value is extracted and passed to op
whereupon op
s result is returned. if self
is Err
then it is immediately returned. This function can be used to compose the results of two functions.
Example:
for buf in read_file(file) {
print_buf(buf)
}
iter_err
fn iter_err<'r>(&'r self) -> OptionIterator<&'r E>
Call a method based on a previous result
If self
is Err
then the value is extracted and passed to op
whereupon op
s result is returned. if self
is Ok
then it is immediately returned. This function can be used to pass through a successful result while handling an error.
unwrap
fn unwrap(self) -> T
Unwraps a result, yielding the content of an Ok
. Fails if the value is a Err
with an error message derived from E
's ToStr
implementation.
unwrap_err
fn unwrap_err(self) -> E
Unwraps a result, yielding the content of an Err
. Fails if the value is a Ok
.
expect
fn expect(self, reason: &str) -> T
Unwraps a result, yielding the content of an Ok
. Fails if the value is a Err
with a custom failure message.
expect_err
fn expect_err(self, reason: &str) -> E
Unwraps a result, yielding the content of an Err
Fails if the value is a Ok
with a custom failure message.
map_move
fn map_move<U>(self, op: &fn(T) -> U) -> Result<U, E>
Call a method based on a previous result
If self
is Ok
then the value is extracted and passed to op
whereupon op
s result is wrapped in Ok
and returned. if self
is Err
then it is immediately returned. This function can be used to compose the results of two functions.
Example:
let res = do read_file(file).map_move |buf| {
parse_bytes(buf)
}
map_err_move
fn map_err_move<F>(self, op: &fn(E) -> F) -> Result<T, F>
Call a method based on a previous result
If self
is Err
then the value is extracted and passed to op
whereupon op
s result is wrapped in an Err
and returned. if self
is Ok
then it is immediately returned. This function can be used to pass through a successful result while handling an error.
chain
fn chain<U>(self, op: &fn(T) -> Result<U, E>) -> Result<U, E>
Call a method based on a previous result
If self
is Ok
then the value is extracted and passed to op
whereupon op
s result is returned. if self
is Err
then it is immediately returned. This function can be used to compose the results of two functions.
Example:
let res = do read_file(file) |buf| {
Ok(parse_bytes(buf))
};
chain_err
fn chain_err<F>(self, op: &fn(E) -> Result<T, F>) -> Result<T, F>
Call a function based on a previous result
If self
is Err
then the value is extracted and passed to op
whereupon op
s result is returned. if self
is Ok
then it is immediately returned. This function can be used to pass through a successful result while handling an error.
Result<T, E>
where <T: Clone, E: ToStr>
map_err
fn map_err<F: Clone>(&self, op: &fn(&E) -> F) -> Result<T, F>
Call a method based on a previous result
If self
is Err
then the value is extracted and passed to op
whereupon op
s result is wrapped in an Err
and returned. if self
is Ok
then it is immediately returned. This function can be used to pass through a successful result while handling an error.
Result<T, E>
where <T, E: Clone + ToStr>
map
fn map<U>(&self, op: &fn(&T) -> U) -> Result<U, E>
Call a method based on a previous result
If self
is Ok
then the value is extracted and passed to op
whereupon op
s result is wrapped in Ok
and returned. if self
is Err
then it is immediately returned. This function can be used to compose the results of two functions.
Example:
let res = do read_file(file).map |buf| {
parse_bytes(buf)
};
collect
fn collect<T, E, Iter: Iterator<Result<T, E>>>(mut iterator: Iter) ->
Result<~[T], E>
Takes each element in the iterator: if it is an error, no further elements are taken, and the error is returned. Should no error occur, a vector containing the values of each Result is returned.
Here is an example which increments every integer in a vector, checking for overflow:
fn inc_conditionally(x: uint) -> Result<uint, &'static str> {
if x == uint::max_value { return Err("overflow"); }
else { return Ok(x+1u); }
}
let v = [1u, 2, 3];
let res = collect(v.iter().map(|&x| inc_conditionally(x)));
assert!(res == Ok(~[2u, 3, 4]));
fold
fn fold<T, V, E,
Iter: Iterator<Result<T,
E>>>(mut iterator: Iter, mut init: V,
f: &fn(V, T) -> V) -> Result<V, E>
Perform a fold operation over the result values from an iterator.
If an Err
is encountered, it is immediately returned. Otherwise, the folded value is returned.
fold_
fn fold_<T, E, Iter: Iterator<Result<T, E>>>(iterator: Iter) -> Result<(), E>
Perform a trivial fold operation over the result values from an iterator.
If an Err
is encountered, it is immediately returned. Otherwise, a simple Ok(())
is returned.
map_opt
fn map_opt<T, U: ToStr, V>(o_t: &Option<T>, op: &fn(&T) -> Result<V, U>) ->
Result<Option<V>, U>