pub enum Result<T, E> { Ok(T), Err(E), }
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.
impl<T, E: ToStr> Result<T, E>
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
fn is_ok(&self) -> bool
Returns true if the result is Ok
fn is_err(&self) -> bool
Returns true if the result is Err
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)
}
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.
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.
fn unwrap_err(self) -> E
Unwraps a result, yielding the content of an Err
.
Fails if the value is a Ok
.
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.
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.
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)
}
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.
fn and(self, res: Result<T, E>) -> Result<T, E>
Call a method based on a previous result
If self
is Ok
, then res
it is returned. If self
is Err
,
then self
is returned.
fn and_then<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))
};
fn or(self, res: Result<T, E>) -> Result<T, E>
Call a method based on a previous result
If self
is Ok
, then self
is returned. If self
is Err
then res
is returned.
fn or_else<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.
impl<T: Clone, E: ToStr> Result<T, E>
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.
impl<T, E: Clone + ToStr> Result<T, E>
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)
};
impl<T: Clone, E: Clone> Clone for Result<T, E>
fn clone(&self) -> Result<T, E>
Returns a copy of the value. The contents of owned pointers are copied to maintain uniqueness, while the contents of managed pointers are not copied.
impl<T: Eq, E: Eq> Eq for Result<T, E>
impl<T: Clone, E> ToOption<T> for Result<T, E>
fn to_option(&self) -> Option<T>
Convert to the option
type
impl<T, E> IntoOption<T> for Result<T, E>
fn into_option(self) -> Option<T>
Convert to the option
type
impl<T, E> AsOption<T> for Result<T, E>
fn as_option<'a>(&'a self) -> Option<&'a T>
Convert to the option
type
impl<T: Clone, E: Clone> ToResult<T, E> for Result<T, E>
fn to_result(&self) -> Result<T, E>
Convert to the result
type
impl<T, E> IntoResult<T, E> for Result<T, E>
fn into_result(self) -> Result<T, E>
Convert to the result
type
impl<T, E> AsResult<T, E> for Result<T, E>
fn as_result<'a>(&'a self) -> Result<&'a T, &'a E>
Convert to the result
type
impl<T: Clone, E: Clone> ToEither<E, T> for Result<T, E>
fn to_either(&self) -> Either<E, T>
Convert to the either
type
impl<T, E> IntoEither<E, T> for Result<T, E>
fn into_either(self) -> Either<E, T>
Convert to the either
type
impl<T, E> AsEither<E, T> for Result<T, E>
fn as_either<'a>(&'a self) -> Either<&'a E, &'a T>
Convert to the either
type
Prefix searches with a type followed by a colon (e.g.
fn:
) to restrict the search to a given type.
Accepted types are: fn
, mod
,
struct
(or str
), enum
,
trait
, typedef
(or
tdef
).