A type representing either success or failure

Enum 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.

Variants

Implementation of ::std::clone::Clone for Result<T, E> where <T: ::std::clone::Clone, E: ::std::clone::Clone>

Automatically derived.

Method clone

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

Implementation of ::std::cmp::Eq for Result<T, E> where <T: ::std::cmp::Eq, E: ::std::cmp::Eq>

Automatically derived.

Method eq

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

Method ne

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

Implementation for Result<T, E> where <T, E: ToStr>

Method 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.

Method get_ref

fn get_ref<'a>(&'a self) -> &'a T

Get a reference to the value out of a successful result

Failure

If the result is an error

Method is_ok

fn is_ok(&self) -> bool

Returns true if the result is Ok

Method is_err

fn is_err(&self) -> bool

Returns true if the result is Err

Method 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 ops 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)
}

Method 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 ops 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.

Method 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.

Method unwrap_err

fn unwrap_err(self) -> E

Unwraps a result, yielding the content of an Err. Fails if the value is a Ok.

Method 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.

Method 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.

Method 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 ops 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)
}

Method 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 ops 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.

Method 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 ops 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))
};

Method 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 ops 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.

Implementation for Result<T, E> where <T: Clone, E: ToStr>

Method 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 ops 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.

Implementation for Result<T, E> where <T, E: Clone + ToStr>

Method 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 ops 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)
};

Function 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]));

Function 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.

Function 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.

Function map_opt

fn map_opt<T, U: ToStr, V>(o_t: &Option<T>, op: &fn(&T) -> Result<V, U>) ->
 Result<Option<V>, U>