A type representing either success or failure

Enum Result

The result type

Variants

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

Automatically derived.

Method clone

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

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

Automatically derived.

Method eq

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

Method ne

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

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

Method get_ref

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

Method is_ok

fn is_ok(&self) -> bool

Method is_err

fn is_err(&self) -> bool

Method iter

fn iter(&self, f: &fn(&T))

Method iter_err

fn iter_err(&self, f: &fn(&E))

Method unwrap

fn unwrap(self) -> T

Method unwrap_err

fn unwrap_err(self) -> E

Method chain

fn chain<U>(self, op: &fn(T) -> Result<U, E>) -> Result<U, E>

Method chain_err

fn chain_err<F>(self, op: &fn(E) -> Result<T, F>) -> Result<T, F>

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

Method get

fn get(&self) -> T

Method map_err

fn map_err<F: Copy>(&self, op: &fn(&E) -> F) -> Result<T, F>

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

Method get_err

fn get_err(&self) -> E

Method map

fn map<U: Copy>(&self, op: &fn(&T) -> U) -> Result<U, E>

Function chain

fn chain<T, U, V>(res: Result<T, V>, op: &fn(T) -> Result<U, V>) ->
 Result<U, V>

Call a function based on a previous result

If res is ok then the value is extracted and passed to op whereupon ops result is returned. if res is err then it is immediately returned. This function can be used to compose the results of two functions.

Example:

let res = chain(read_file(file)) { |buf|
    ok(parse_bytes(buf))
}

Function chain_err

fn chain_err<T, U, V>(res: Result<T, V>, op: &fn(t: V) -> Result<T, U>) ->
 Result<T, U>

Call a function based on a previous result

If res is err then the value is extracted and passed to op whereupon ops result is returned. if res is ok then it is immediately returned. This function can be used to pass through a successful result while handling an error.

Function get

fn get<T: Copy, U>(res: &Result<T, U>) -> T

Get the value out of a successful result

Failure

If the result is an error

Function get_err

fn get_err<T, U: Copy>(res: &Result<T, U>) -> U

Get the value out of an error result

Failure

If the result is not an error

Function get_ref

fn get_ref<'a, T, U>(res: &'a Result<T, U>) -> &'a T

Get a reference to the value out of a successful result

Failure

If the result is an error

Function is_err

fn is_err<T, U>(res: &Result<T, U>) -> bool

Returns true if the result is err

Function is_ok

fn is_ok<T, U>(res: &Result<T, U>) -> bool

Returns true if the result is ok

Function iter

fn iter<T, E>(res: &Result<T, E>, f: &fn(&T))

Call a function based on a previous result

If res is ok then the value is extracted and passed to op whereupon ops result is returned. if res is err then it is immediately returned. This function can be used to compose the results of two functions.

Example:

iter(read_file(file)) { |buf|
    print_buf(buf)
}

Function iter_err

fn iter_err<T, E>(res: &Result<T, E>, f: &fn(&E))

Call a function based on a previous result

If res is err then the value is extracted and passed to op whereupon ops result is returned. if res is ok then it is immediately returned. This function can be used to pass through a successful result while handling an error.

Function iter_vec2

fn iter_vec2<S, T,
             U: Copy>(ss: &[S], ts: &[T], op: &fn(&S, &T) -> Result<(), U>) ->
 Result<(), U>

Applies op to the pairwise elements from ss and ts, aborting on error. This could be implemented using map_zip() but it is more efficient on its own as no result vector is built.

Function map

fn map<T, E: Copy, U: Copy>(res: &Result<T, E>, op: &fn(&T) -> U) ->
 Result<U, E>

Call a function based on a previous result

If res is ok then the value is extracted and passed to op whereupon ops result is wrapped in ok and returned. if res is err then it is immediately returned. This function can be used to compose the results of two functions.

Example:

let res = map(read_file(file)) { |buf|
    parse_bytes(buf)
}

Function map_err

fn map_err<T: Copy, E, F: Copy>(res: &Result<T, E>, op: &fn(&E) -> F) ->
 Result<T, F>

Call a function based on a previous result

If res is err then the value is extracted and passed to op whereupon ops result is wrapped in an err and returned. if res is ok then it is immediately returned. This function can be used to pass through a successful result while handling an error.

Function map_opt

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

Function map_vec

fn map_vec<T, U: Copy, V: Copy>(ts: &[T], op: &fn(&T) -> Result<V, U>) ->
 Result<~[V], U>

Maps each element in the vector ts using the operation op. Should an error occur, no further mappings are performed and the error is returned. Should no error occur, a vector containing the result of each map is returned.

Here is an example which increments every integer in a vector, checking for overflow:

fn inc_conditionally(x: uint) -> result<uint,str> {
    if x == uint::max_value { return err("overflow"); }
    else { return ok(x+1u); }
}
map(~[1u, 2u, 3u], inc_conditionally).chain {|incd|
    assert!(incd == ~[2u, 3u, 4u]);
}

Function map_vec2

fn map_vec2<S, T, U: Copy,
            V: Copy>(ss: &[S], ts: &[T], op: &fn(&S, &T) -> Result<V, U>) ->
 Result<~[V], U>

Same as map, but it operates over two parallel vectors.

A precondition is used here to ensure that the vectors are the same length. While we do not often use preconditions in the standard library, a precondition is used here because result::t is generally used in 'careful' code contexts where it is both appropriate and easy to accommodate an error like the vectors being of different lengths.

Function to_either

fn to_either<T: Copy, U: Copy>(res: &Result<U, T>) -> Either<T, U>

Convert to the either type

ok result variants are converted to either::right variants, err result variants are converted to either::left.

Function unwrap

fn unwrap<T, U>(res: Result<T, U>) -> T

Unwraps a result, assuming it is an ok(T)

Function unwrap_err

fn unwrap_err<T, U>(res: Result<T, U>) -> U

Unwraps a result, assuming it is an err(U)