提交 1c4a9b98 编写于 作者: B bors

auto merge of #14294 : kballard/rust/result_unwrap_or_else, r=alexcrichton

Result.unwrap_or_handle() is the equivalent of Option.unwrap_or_else().
In the interests of naming consistency, call it the same thing.

[breaking-change]
......@@ -508,12 +508,19 @@ pub fn unwrap_or(self, optb: T) -> T {
/// Unwraps a result, yielding the content of an `Ok`.
/// If the value is an `Err` then it calls `op` with its value.
#[inline]
pub fn unwrap_or_handle(self, op: |E| -> T) -> T {
pub fn unwrap_or_else(self, op: |E| -> T) -> T {
match self {
Ok(t) => t,
Err(e) => op(e)
}
}
/// Deprecated name for `unwrap_or_else()`.
#[deprecated = "replaced by .unwrap_or_else()"]
#[inline]
pub fn unwrap_or_handle(self, op: |E| -> T) -> T {
self.unwrap_or_else(op)
}
}
impl<T, E: Show> Result<T, E> {
......@@ -758,8 +765,8 @@ fn handler(msg: ~str) -> int {
let ok: Result<int, ~str> = Ok(100);
let ok_err: Result<int, ~str> = Err("I got this.".to_owned());
assert_eq!(ok.unwrap_or_handle(handler), 100);
assert_eq!(ok_err.unwrap_or_handle(handler), 50);
assert_eq!(ok.unwrap_or_else(handler), 100);
assert_eq!(ok_err.unwrap_or_else(handler), 50);
}
#[test]
......@@ -774,6 +781,6 @@ fn handler(msg: ~str) -> int {
}
let bad_err: Result<int, ~str> = Err("Unrecoverable mess.".to_owned());
let _ : int = bad_err.unwrap_or_handle(handler);
let _ : int = bad_err.unwrap_or_else(handler);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册