Enum std::result::Result

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.

Methods

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

Failure

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

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.

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

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.

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

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

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

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

Trait Implementations

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>

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

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

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