A type representing either success or failure
Result
- The result typeof ::std::clone::Clone for Result<T, U> where <T: ::std::clone::Clone, U: ::std::clone::Clone>
- Automatically derived.of ::std::cmp::Eq for Result<T, U> where <T: ::std::cmp::Eq, U: ::std::cmp::Eq>
- Automatically derived.for Result<T, E> where <T, E>
for Result<T, E> where <T: Copy, E>
for Result<T, E> where <T, E: Copy>
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 resultget_ref
- Get a reference to the value out of a successful 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)
unwrap_err
- Unwraps a result, assuming it is an err(U)
Result
The result type
Ok(T)
- Contains the successful result value
Err(U)
- Contains the error value
::std::clone::Clone
for Result<T, U>
where <T: ::std::clone::Clone, U: ::std::clone::Clone>
Automatically derived.
clone
fn clone(&self) -> Result<T, U>
::std::cmp::Eq
for Result<T, U>
where <T: ::std::cmp::Eq, U: ::std::cmp::Eq>
Automatically derived.
eq
fn eq(&self, __arg_0: &Result<T, U>) -> ::bool
ne
fn ne(&self, __arg_0: &Result<T, U>) -> ::bool
Result<T, E>
where <T, E>
get_ref
fn get_ref<'a>(&'a self) -> &'a T
is_ok
fn is_ok(&self) -> bool
is_err
fn is_err(&self) -> bool
iter
fn iter(&self, f: &fn(&T))
iter_err
fn iter_err(&self, f: &fn(&E))
unwrap
fn unwrap(self) -> T
unwrap_err
fn unwrap_err(self) -> E
chain
fn chain<U>(self, op: &fn(T) -> Result<U, E>) -> Result<U, E>
chain_err
fn chain_err<F>(self, op: &fn(E) -> Result<T, F>) -> Result<T, F>
Result<T, E>
where <T: Copy, E>
get
fn get(&self) -> T
map_err
fn map_err<F: Copy>(&self, op: &fn(&E) -> F) -> Result<T, F>
Result<T, E>
where <T, E: Copy>
get_err
fn get_err(&self) -> E
map
fn map<U: Copy>(&self, op: &fn(&T) -> U) -> Result<U, E>
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 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_bytes(buf))
}
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 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
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
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
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
If the result is an error
is_err
fn is_err<T, U>(res: &Result<T, U>) -> bool
Returns true if the result is err
is_ok
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>) ->
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.
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_bytes(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 { return err("overflow"); }
else { return 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>) ->
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
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)
unwrap_err
fn unwrap_err<T, U>(res: Result<T, U>) -> U
Unwraps a result, assuming it is an err(U)