Enum core::result::Result[src]
pub enum Result<T, E> {
Ok(T),
Err(E),
}Result is a type that represents either success (Ok) or failure (Err).
See the std::result module documentation for details.
Variants
Ok | Contains the success value |
Err | Contains the error value |
Methods
impl<T, E> Result<T, E>
fn is_ok(&self) -> bool
Returns true if the result is Ok
Example
fn main() { use std::io::{File, Open, Write}; fn do_not_run_example() { // creates a file let mut file = File::open_mode(&Path::new("secret.txt"), Open, Write); assert!(file.write_line("it's cold in here").is_ok()); } }use std::io::{File, Open, Write}; let mut file = File::open_mode(&Path::new("secret.txt"), Open, Write); assert!(file.write_line("it's cold in here").is_ok());
fn is_err(&self) -> bool
Returns true if the result is Err
Example
fn main() { use std::io::{File, Open, Read}; // When opening with `Read` access, if the file does not exist // then `open_mode` returns an error. let bogus = File::open_mode(&Path::new("not_a_file.txt"), Open, Read); assert!(bogus.is_err()); }use std::io::{File, Open, Read}; // When opening with `Read` access, if the file does not exist // then `open_mode` returns an error. let bogus = File::open_mode(&Path::new("not_a_file.txt"), Open, Read); assert!(bogus.is_err());
fn ok(self) -> Option<T>
Convert from Result<T, E> to Option<T>
Converts self into an Option<T>, consuming self,
and discarding the error, if any.
To convert to an Option without discarding the error value,
use as_ref to first convert the Result<T, E> into a
Result<&T, &E>.
Examples
fn main() { use std::io::{File, IoResult}; let bdays: IoResult<File> = File::open(&Path::new("important_birthdays.txt")); let bdays: File = bdays.ok().expect("unable to open birthday file"); }use std::io::{File, IoResult}; let bdays: IoResult<File> = File::open(&Path::new("important_birthdays.txt")); let bdays: File = bdays.ok().expect("unable to open birthday file");
fn err(self) -> Option<E>
Convert from Result<T, E> to Option<E>
Converts self into an Option<T>, consuming self,
and discarding the value, if any.
fn as_ref<'r>(&'r self) -> Result<&'r T, &'r E>
Convert from Result<T, E> to Result<&T, &E>
Produces a new Result, containing a reference
into the original, leaving the original in place.
fn as_mut<'r>(&'r mut self) -> Result<&'r mut T, &'r mut E>
Convert from Result<T, E> to Result<&mut T, &mut E>
fn map<U>(self, op: |T| -> U) -> Result<U, E>
Maps a Result<T, E> to Result<U, E> by applying a function to an
contained Ok value, leaving an Err value untouched.
This function can be used to compose the results of two functions.
Examples
Sum the lines of a buffer by mapping strings to numbers, ignoring I/O and parse errors:
fn main() { use std::io::{BufReader, IoResult}; let buffer = "1\n2\n3\n4\n"; let mut reader = BufReader::new(buffer.as_bytes()); let mut sum = 0; while !reader.eof() { let line: IoResult<String> = reader.read_line(); // Convert the string line to a number using `map` and `from_str` let val: IoResult<int> = line.map(|line| { from_str::<int>(line.as_slice().trim_right()).unwrap_or(0) }); // Add the value if there were no errors, otherwise add 0 sum += val.ok().unwrap_or(0); } assert!(sum == 10); }use std::io::{BufReader, IoResult}; let buffer = "1\n2\n3\n4\n"; let mut reader = BufReader::new(buffer.as_bytes()); let mut sum = 0; while !reader.eof() { let line: IoResult<String> = reader.read_line(); // Convert the string line to a number using `map` and `from_str` let val: IoResult<int> = line.map(|line| { from_str::<int>(line.as_slice().trim_right()).unwrap_or(0) }); // Add the value if there were no errors, otherwise add 0 sum += val.ok().unwrap_or(0); } assert!(sum == 10);
fn map_err<F>(self, op: |E| -> F) -> Result<T, F>
Maps a Result<T, E> to Result<T, F> by applying a function to an
contained Err value, leaving an Ok value untouched.
This function can be used to pass through a successful result while handling an error.
fn and<U>(self, res: Result<U, E>) -> Result<U, E>
Returns res if the result is Ok, otherwise returns the Err value of self.
fn and_then<U>(self, op: |T| -> Result<U, E>) -> Result<U, E>
Calls op if the result is Ok, otherwise returns the Err value of self.
This function can be used for control flow based on result values
fn or(self, res: Result<T, E>) -> Result<T, E>
Returns res if the result is Err, otherwise returns the Ok value of self.
fn or_else<F>(self, op: |E| -> Result<T, F>) -> Result<T, F>
Calls op if the result is Err, otherwise returns the Ok value of self.
This function can be used for control flow based on result values
fn unwrap_or(self, optb: T) -> T
Unwraps a result, yielding the content of an Ok.
Else it returns optb.
fn unwrap_or_else(self, op: |E| -> T) -> T
Unwraps a result, yielding the content of an Ok.
If the value is an Err then it calls op with its value.
fn unwrap_or_handle(self, op: |E| -> T) -> T
Deprecated name for unwrap_or_else().
impl<T, E: Show> Result<T, E>
fn unwrap(self) -> T
Unwraps a result, yielding the content of an Ok.
Fails if the value is an Err.
impl<T: Show, E> Result<T, E>
fn unwrap_err(self) -> E
Unwraps a result, yielding the content of an Err.
Fails if the value is an Ok.