From 0937c10f3ca21585f0cb00c1a112e4ff4a742456 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sat, 13 Jun 2015 22:34:58 +0800 Subject: libcore/Result - RFC#1119 Add an 'expect' method to Result --- src/libcore/result.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/libcore') diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 003c4b2b78c..772831b1a58 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -731,6 +731,26 @@ impl Result { panic!("called `Result::unwrap()` on an `Err` value: {:?}", e) } } + + /// Unwraps a result, yielding the content of an `Ok`. + /// + /// Panics if the value is an `Err`, with a panic message including the + /// passed message, and the content of the `Err`. + /// + /// # Examples + /// ```{.should_panic} + /// #![feature(result_expect)] + /// let x: Result = Err("emergency failure"); + /// x.expect("Testing expect"); // panics with `Testing expect: emergency failure` + /// ``` + #[inline] + #[unstable(feature = "result_expect", reason = "newly introduced")] + pub fn expect(self, msg: &str) -> T { + match self { + Ok(t) => t, + Err(e) => panic!("{}: {:?}", msg, e), + } + } } #[stable(feature = "rust1", since = "1.0.0")] -- cgit 1.4.1-3-g733a5