From a8ecfce8224ff9f7c8f46960b48f468407f28705 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Sat, 20 Jan 2018 10:58:31 +0100 Subject: [PATCH] Add a replace method to Option --- text/0000-option-replace.md | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 text/0000-option-replace.md diff --git a/text/0000-option-replace.md b/text/0000-option-replace.md new file mode 100644 index 00000000000..aa292b8d9e2 --- /dev/null +++ b/text/0000-option-replace.md @@ -0,0 +1,46 @@ +- Feature Name: `option-replace` +- Start Date: 2017-01-16 +- RFC PR: (leave this empty) +- Rust Issue: (leave this empty) + +# Summary +[summary]: #summary + +The standard library provides the `Option::take` method that returns the optional inner value and leaves a `None` in place. This RFC proposes the addition of `Option::replace` to complete the previously cited method, this method 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. + +# Motivation +[motivation]: #motivation + +Why are we doing this? What use cases does it support? What is the expected outcome? + +How do you replace a value inside an `Option`, you can use `mem::replace` but it can be really unconvenient to import the `mem` module just for that. Why not adding a useful method to do that ? +This is the same kind of method than the `take` one. + +# Detailed design +[design]: #detailed-design + +This methid will be added to the `core::option::Option` type implementation: + +```rust +use core::mem::replace; + +pub fn replace(&mut self, value: T) -> Option { + mem::replace(self, Some(value)) +} +``` + +# Drawbacks +[drawbacks]: #drawbacks + +It increases the size of the standard library by a tiny bit. + +# Alternatives +[alternatives]: #alternatives + +Don't use the `replace` name and use `give` instead in symmetry with the actual `take` method. +Use directly `mem::replace`. + +# Unresolved questions +[unresolved]: #unresolved-questions + +None.