about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorJohn Hodge <tpg@mutabah.net>2015-06-13 22:34:58 +0800
committerJohn Hodge <tpg@mutabah.net>2015-06-15 12:00:16 +0800
commit0937c10f3ca21585f0cb00c1a112e4ff4a742456 (patch)
treee3e83799a5ebf9dec1503115d93e33a7aff21aa6 /src/libcore
parent172cd83490cc66065e72861aed53e3efec29b34f (diff)
downloadrust-0937c10f3ca21585f0cb00c1a112e4ff4a742456.tar.gz
rust-0937c10f3ca21585f0cb00c1a112e4ff4a742456.zip
libcore/Result - RFC#1119 Add an 'expect' method to Result
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/result.rs20
1 files changed, 20 insertions, 0 deletions
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<T, E: fmt::Debug> Result<T, E> {
                 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<u32, &str> = 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")]