Skip to content

Commit 5a9b33a

Browse files
committed
Add Option::take_map{,_default}()
1 parent 96c1082 commit 5a9b33a

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/libstd/option.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,20 @@ impl<T> Option<T> {
200200
match self { None => def, Some(v) => f(v) }
201201
}
202202

203+
/// As `map_consume`, but swaps a None into the original option rather
204+
/// than consuming it by-value.
205+
#[inline]
206+
pub fn take_map<U>(&mut self, blk: &fn(T) -> U) -> Option<U> {
207+
util::replace(self, None).map_consume(blk)
208+
}
209+
210+
/// As `map_consume_default`, but swaps a None into the original option
211+
/// rather than consuming it by-value.
212+
#[inline]
213+
pub fn take_map_default<U> (&mut self, def: U, blk: &fn(T) -> U) -> U {
214+
util::replace(self, None).map_consume_default(def, blk)
215+
}
216+
203217
/// Apply a function to the contained value or do nothing
204218
pub fn mutate(&mut self, f: &fn(T) -> T) {
205219
if self.is_some() {

0 commit comments

Comments
 (0)