Skip to content

Commit

Permalink
Implement ignore function on Result. Fixes rust-lang#20949
Browse files Browse the repository at this point in the history
  • Loading branch information
sixohsix committed Jan 13, 2015
1 parent e94a9f0 commit 64f5de4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/libcore/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,17 @@
//! }
//! ```
//!
//! If you are absolutely sure you want to ignore a result, you must
//! do so explicitly:
//!
//! ```{.no_run}
//! # use std::io::{File, Open, Write};
//!
//! # let mut file = File::open_mode(&Path::new("valuable_data.txt"), Open, Write);
//! file.write_line("important message").ignore();
//! # drop(file);
//! ```
//!
//! # The `try!` macro
//!
//! When writing code that calls many functions that return the
Expand Down Expand Up @@ -353,6 +364,26 @@ impl<T, E> Result<T, E> {
}
}

/// Ignore this result.
///
/// Converts `self` into (), consuming `self`, and
/// discarding the value or error.
///
/// # Example
///
/// ```
/// let x: Result<uint, &str> = Ok(2);
/// x.ignore();
/// ```
#[inline]
#[stable]
pub fn ignore(self) {
match self {
Ok(_) => (),
Err(_) => (),
}
}

/////////////////////////////////////////////////////////////////////////
// Adapter for working with references
/////////////////////////////////////////////////////////////////////////
Expand Down
9 changes: 9 additions & 0 deletions src/libcoretest/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,12 @@ pub fn test_unwrap_or_else_panic() {
let bad_err: Result<int, &'static str> = Err("Unrecoverable mess.");
let _ : int = bad_err.unwrap_or_else(handler);
}

#[test]
pub fn test_ignore() {
let ok: Result<int, &'static str> = Ok(100i);
let ok_err: Result<int, &'static str> = Err("Err");

ok.ignore();
ok_err.ignore();
}

0 comments on commit 64f5de4

Please sign in to comment.