From 603553458639cd65f3cc75b9b74f8176af81aa2b Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Wed, 4 Jul 2018 21:54:45 +0200 Subject: Implement `Option::replace` in the core library --- src/libcore/option.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/libcore') diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 20bc173f7e1..db0a807d152 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -845,6 +845,33 @@ impl Option { pub fn take(&mut self) -> Option { mem::replace(self, None) } + + /// Replaces the actual value in the option by the value given in parameter, + /// returning the old value if present, + /// leaving a `Some` in its place without deinitializing either one. + /// + /// [`Some`]: #variant.Some + /// + /// # Examples + /// + /// ``` + /// #![feature(option_replace)] + /// + /// let mut x = Some(2); + /// let old = x.replace(5); + /// assert_eq!(x, Some(5)); + /// assert_eq!(old, Some(2)); + /// + /// let mut x = None; + /// let old = x.replace(3); + /// assert_eq!(x, Some(3)); + /// assert_eq!(old, None); + /// ``` + #[inline] + #[unstable(feature = "option_replace", issue = "51998")] + pub fn replace(&mut self, value: T) -> Option { + mem::replace(self, Some(value)) + } } impl<'a, T: Clone> Option<&'a T> { -- cgit 1.4.1-3-g733a5 From af87a3594a9f852de0eb7fec7a2f2e7c5fdb4fe8 Mon Sep 17 00:00:00 2001 From: Clément RENAULT Date: Mon, 9 Jul 2018 14:50:54 +0200 Subject: Add a basic test to `Option::replace` --- src/libcore/tests/lib.rs | 1 + src/libcore/tests/option.rs | 15 +++++++++++++++ 2 files changed, 16 insertions(+) (limited to 'src/libcore') diff --git a/src/libcore/tests/lib.rs b/src/libcore/tests/lib.rs index 9d4a5213992..ca7db6e4639 100644 --- a/src/libcore/tests/lib.rs +++ b/src/libcore/tests/lib.rs @@ -44,6 +44,7 @@ #![feature(reverse_bits)] #![feature(iterator_find_map)] #![feature(slice_internals)] +#![feature(option_replace)] extern crate core; extern crate test; diff --git a/src/libcore/tests/option.rs b/src/libcore/tests/option.rs index 22109e28edd..bc3e61a4f54 100644 --- a/src/libcore/tests/option.rs +++ b/src/libcore/tests/option.rs @@ -297,3 +297,18 @@ fn test_try() { } assert_eq!(try_option_err(), Err(NoneError)); } + +#[test] +fn test_replace() { + let mut x = Some(2); + let old = x.replace(5); + + assert_eq!(x, Some(5)); + assert_eq!(old, Some(2)); + + let mut x = None; + let old = x.replace(3); + + assert_eq!(x, Some(3)); + assert_eq!(old, None); +} -- cgit 1.4.1-3-g733a5 From c8f0e6f210caccdaea7dc59fd970c81018ddfb00 Mon Sep 17 00:00:00 2001 From: Clément RENAULT Date: Mon, 9 Jul 2018 14:52:32 +0200 Subject: Fix the documentation of `Option::replace` --- src/libcore/option.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libcore') diff --git a/src/libcore/option.rs b/src/libcore/option.rs index db0a807d152..f3e823670aa 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -848,7 +848,7 @@ impl Option { /// Replaces the actual value in the option by the value given in parameter, /// returning the old value if present, - /// leaving a `Some` in its place without deinitializing either one. + /// leaving a [`Some`] in its place without deinitializing either one. /// /// [`Some`]: #variant.Some /// -- cgit 1.4.1-3-g733a5