diff options
| author | Ben Blum <bblum@andrew.cmu.edu> | 2013-07-12 22:40:57 -0400 |
|---|---|---|
| committer | Ben Blum <bblum@andrew.cmu.edu> | 2013-07-20 05:08:57 -0400 |
| commit | e2a42416ddab88f7ed076cb9a4fd6ecc70be3278 (patch) | |
| tree | fa1ef3905007e4228b1c3bb0c6306655bc14c4fa /src/libstd | |
| parent | 2a7273c71efe0209c45a9e04f54e210a533ddb58 (diff) | |
| download | rust-e2a42416ddab88f7ed076cb9a4fd6ecc70be3278.tar.gz rust-e2a42416ddab88f7ed076cb9a4fd6ecc70be3278.zip | |
Add option::take(), the building block of the option::take_* family.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/option.rs | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/src/libstd/option.rs b/src/libstd/option.rs index 6f6f18ab85d..03f5d94b3f5 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -200,18 +200,24 @@ impl<T> Option<T> { match self { None => def, Some(v) => f(v) } } + /// Take the value out of the option, leaving a `None` in its place. + #[inline] + pub fn take(&mut self) -> Option<T> { + util::replace(self, None) + } + /// 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) + self.take().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) + self.take().map_consume_default(def, blk) } /// Apply a function to the contained value or do nothing @@ -309,7 +315,7 @@ impl<T> Option<T> { #[inline] pub fn take_unwrap(&mut self) -> T { if self.is_none() { fail!("option::take_unwrap none") } - util::replace(self, None).unwrap() + self.take().unwrap() } /** |
