From e6c11313c88574aa2500df2f76c5534fbc2e0512 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Mon, 9 Sep 2013 19:32:32 -0700 Subject: std: Add Option.{result_or_default,or_default} that uses Default --- src/libstd/option.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/libstd') diff --git a/src/libstd/option.rs b/src/libstd/option.rs index 1f3a31a403c..84d8a3aa188 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -350,6 +350,26 @@ impl Option { } } +impl Option { + /// Returns the contained value or default (for this type) + #[inline] + pub fn unwrap_or_default(self) -> T { + match self { + Some(x) => x, + None => Default::default() + } + } + + /// Returns self or `Some`-wrapped default value + #[inline] + pub fn or_default(self) -> Option { + match self { + None => Some(Default::default()), + x => x, + } + } +} + impl Default for Option { fn default() -> Option { None } } -- cgit 1.4.1-3-g733a5