Skip to content

Commit c2ddf7c

Browse files
committed
Merge pull request #3236 from dbp/or
core: adding option::or(a,b) that returns the leftmost some() or none otherwise
2 parents c321cdb + 62ea688 commit c2ddf7c

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/libcore/option.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ pure fn chain_ref<T, U>(opt: &option<T>,
9393
match *opt { some(ref x) => f(x), none => none }
9494
}
9595

96+
pure fn or<T>(+opta: option<T>, +optb: option<T>) -> option<T> {
97+
/*!
98+
* Returns the leftmost some() value, or none if both are none.
99+
*/
100+
match opta {
101+
some(_) => opta,
102+
_ => optb
103+
}
104+
}
105+
96106
#[inline(always)]
97107
pure fn while_some<T>(+x: option<T>, blk: fn(+T) -> option<T>) {
98108
//! Applies a function zero or more times until the result is none.

0 commit comments

Comments
 (0)