diff options
| author | Ben Blum <bblum@andrew.cmu.edu> | 2013-07-02 17:47:38 -0400 |
|---|---|---|
| committer | Ben Blum <bblum@andrew.cmu.edu> | 2013-07-20 05:08:55 -0400 |
| commit | 5a9b33a76d76a77d2ced1c2430a25faa0f284071 (patch) | |
| tree | 1281dae50a170e014973c8f6578c4a636988b01e /src/libstd | |
| parent | 96c1082f0fdbbe5258cfc1dc37f83052feff8421 (diff) | |
| download | rust-5a9b33a76d76a77d2ced1c2430a25faa0f284071.tar.gz rust-5a9b33a76d76a77d2ced1c2430a25faa0f284071.zip | |
Add Option::take_map{,_default}()
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/option.rs | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/src/libstd/option.rs b/src/libstd/option.rs index 42d892fee9b..6f6f18ab85d 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -200,6 +200,20 @@ impl<T> Option<T> { match self { None => def, Some(v) => f(v) } } + /// As `map_consume`, but swaps a None into the original option rather + /// than consuming it by-value. + #[inline] + pub fn take_map<U>(&mut self, blk: &fn(T) -> U) -> Option<U> { + util::replace(self, None).map_consume(blk) + } + + /// As `map_consume_default`, but swaps a None into the original option + /// rather than consuming it by-value. + #[inline] + pub fn take_map_default<U> (&mut self, def: U, blk: &fn(T) -> U) -> U { + util::replace(self, None).map_consume_default(def, blk) + } + /// Apply a function to the contained value or do nothing pub fn mutate(&mut self, f: &fn(T) -> T) { if self.is_some() { |
