A type representing either success or failure
result
- The result typeextensions for result<T, E>
extensions for result<T, E>
extensions for result<T, E>
extensions for result<T, E>
chain
- Call a function based on a previous resultchain_err
- Call a function based on a previous resultget
- Get the value out of a successful resultget_err
- Get the value out of an error resultis_err
- Returns true if the result is err
is_ok
- Returns true if the result is ok
iter
- Call a function based on a previous resultiter_err
- Call a function based on a previous resultiter_vec2
- Applies op to the pairwise elements from ss
and ts
, aborting on errormap
- Call a function based on a previous resultmap_err
- Call a function based on a previous resultmap_opt
map_vec
- Maps each element in the vector ts
using the operation op
map_vec2
- Same as map, but it operates over two parallel vectors.to_either
- Convert to the either
typeunwrap
- Unwraps a result, assuming it is an ok(T)
result
The result type
ok(T)
- Contains the successful result valueerr(U)
- Contains the error valueextensions for result<T, E>
is_ok
fn is_ok() -> bool
is_err
fn is_err() -> bool
iter
fn iter(f: fn(T))
iter_err
fn iter_err(f: fn(E))
extensions for result<T, E>
get
fn get() -> T
map_err
fn map_err<F: copy>(op: fn(E) -> F) -> result<T, F>
extensions for result<T, E>
get_err
fn get_err() -> E
map
fn map<U: copy>(op: fn(T) -> U) -> result<U, E>
extensions for result<T, E>
chain
fn chain<U: copy>(op: fn(T) -> result<U, E>) -> result<U, E>
chain_err
fn chain_err<F: copy>(op: fn(E) -> result<T, F>) -> result<T, F>
chain
fn chain<T, U: copy, V: copy>(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 op
s 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_buf(buf))
}
chain_err
fn chain_err<T: copy, U: copy,
V: copy>(res: result<T, V>, op: fn(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 op
s 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.
get
pure fn get<T: copy, U>(res: result<T, U>) -> T
Get the value out of a successful result
If the result is an error
get_err
pure fn get_err<T, U: copy>(res: result<T, U>) -> U
Get the value out of an error result
If the result is not an error
is_err
pure fn is_err<T, U>(res: result<T, U>) -> bool
Returns true if the result is err
is_ok
pure fn is_ok<T, U>(res: result<T, U>) -> bool
Returns true if the result is ok
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 op
s 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)
}
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 op
s 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.
iter_vec2
fn iter_vec2<S, T,
U: copy>(ss: ~[S], ts: ~[T],
op:
fn(S, T)
-> result<(), U>) : vec::same_length(ss, ts) ->
result<(), U>
Applies op to the pairwise elements from ss
and ts
, aborting on error. This could be implemented using map2()
but it is more efficient on its own as no result vector is built.
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 op
s 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_buf(buf)
}
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 op
s 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.
map_opt
fn map_opt<T, U: copy, V: copy>(o_t: option<T>, op: fn(T) -> result<V, U>) ->
result<option<V>, U>
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 { ret err("overflow"); }
else { ret ok(x+1u); }
}
map([1u, 2u, 3u]/~, inc_conditionally).chain {|incd|
assert incd == [2u, 3u, 4u]/~;
}
map_vec2
fn map_vec2<S, T, U: copy,
V: copy>(ss: ~[S], ts: ~[T],
op: fn(S, T) -> result<V, U>) : vec::same_length(ss, ts)
-> 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.
to_either
pure 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
.
unwrap
fn unwrap<T, U>(-res: result<T, U>) -> T
Unwraps a result, assuming it is an ok(T)