-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c892139
commit 6cce94f
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
- 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<T> { | ||
mem::replace(self, Some(value)) | ||
} | ||
``` | ||
|
||
# Drawbacks | ||
[drawbacks]: #drawbacks | ||
|
||
Use directly `mem::replace`. | ||
|
||
# Alternatives | ||
[alternatives]: #alternatives | ||
|
||
None. | ||
|
||
# Unresolved questions | ||
[unresolved]: #unresolved-questions | ||
|
||
None. |