From 62ea688e508fdd265eff5207acf2191cc2a715e8 Mon Sep 17 00:00:00 2001 From: Daniel Patterson Date: Mon, 20 Aug 2012 20:01:06 -0400 Subject: [PATCH] core: adding option::or, a function to return the leftmost of two some() values or none if both are none --- src/libcore/option.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index c122fb9e46f8..bae84796b167 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -93,6 +93,16 @@ pure fn chain_ref(opt: &option, match *opt { some(ref x) => f(x), none => none } } +pure fn or(+opta: option, +optb: option) -> option { + /*! + * Returns the leftmost some() value, or none if both are none. + */ + match opta { + some(_) => opta, + _ => optb + } +} + #[inline(always)] pure fn while_some(+x: option, blk: fn(+T) -> option) { //! Applies a function zero or more times until the result is none.