diff --git a/src/libcore/option.rs b/src/libcore/option.rs index e8483875c97e5..fd27883d25142 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -569,6 +569,67 @@ impl Option { } } + /// Transforms the `Option` into a [`Result`], mapping [`Some(v)`] to + /// [`Err(v)`] and [`None`] to [`Ok(ok)`]. + /// + /// Arguments passed to `err_or` are eagerly evaluated; if you are passing the + /// result of a function call, it is recommended to use [`err_or_else`], which is + /// lazily evaluated. + /// + /// [`Result`]: ../../std/result/enum.Result.html + /// [`Err(v)`]: ../../std/result/enum.Result.html#variant.Err + /// [`Ok(ok)`]: ../../std/result/enum.Result.html#variant.Ok + /// [`None`]: #variant.None + /// [`Some(v)`]: #variant.Some + /// [`err_or_else`]: #method.err_or_else + /// + /// # Examples + /// + /// ``` + /// #![feature(option_err_or)] + /// let x = Some("foo"); + /// assert_eq!(x.err_or(0), Err("foo")); + /// + /// let x: Option<&str> = None; + /// assert_eq!(x.err_or(0), Ok(0)); + /// ``` + #[inline] + #[unstable(feature = "option_err_or", issue = "none")] + pub fn err_or(self, ok: O) -> Result { + match self { + Some(v) => Err(v), + None => Ok(ok), + } + } + + /// Transforms the `Option` into a [`Result`], mapping [`Some(v)`] to + /// [`Err(v)`] and [`None`] to [`Ok(ok())`]. + /// + /// [`Result`]: ../../std/result/enum.Result.html + /// [`Err(v)`]: ../../std/result/enum.Result.html#variant.Err + /// [`Ok(ok())`]: ../../std/result/enum.Result.html#variant.Ok + /// [`None`]: #variant.None + /// [`Some(v)`]: #variant.Some + /// + /// # Examples + /// + /// ``` + /// #![feature(option_err_or)] + /// let x = Some("foo"); + /// assert_eq!(x.err_or_else(|| 0), Err("foo")); + /// + /// let x: Option<&str> = None; + /// assert_eq!(x.err_or_else(|| 0), Ok(0)); + /// ``` + #[inline] + #[unstable(feature = "option_err_or", issue = "none")] + pub fn err_or_else O>(self, ok: F) -> Result { + match self { + Some(v) => Err(v), + None => Ok(ok()), + } + } + ///////////////////////////////////////////////////////////////////////// // Iterator constructors /////////////////////////////////////////////////////////////////////////