]> git.lizzy.rs Git - rust.git/commitdiff
Add option::take(), the building block of the option::take_* family.
authorBen Blum <bblum@andrew.cmu.edu>
Sat, 13 Jul 2013 02:40:57 +0000 (22:40 -0400)
committerBen Blum <bblum@andrew.cmu.edu>
Sat, 20 Jul 2013 09:08:57 +0000 (05:08 -0400)
src/libstd/option.rs

index 6f6f18ab85d336569542b3291bd3f2baa59e9bf4..03f5d94b3f5fdebf9b55b577b3a9583afedf5d7f 100644 (file)
@@ -200,18 +200,24 @@ pub fn map_consume_default<U>(self, def: U, f: &fn(v: T) -> U) -> U {
         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 @@ pub fn unwrap(self) -> 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()
     }
 
     /**