RFC 2296: option-replace

libs ()

Summary

This RFC proposes the addition of Option::replace to complete the Option::take method, it replaces the actual value in the option by Some with the value given in parameter, returning the old value if present, without deinitializing either one.

Motivation

You can see the Option as a container and other containers already have this kind of method to change a value in-place like the HashMap::replace method.

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 symmetry of the already present Option::take method.

Detailed design

This method will be added to the core::option::Option type implementation:

use core::mem::replace;

impl<T> Option<T> {
    // ...

    pub fn replace(&mut self, value: T) -> Option<T> {
        mem::replace(self, Some(value))
    }
}

Drawbacks

It increases the size of the standard library by a tiny bit.

The add of this method could be a breaking change in the case of an already implemented method on the Option enum with the replace name. (i.e. a Trait defining the replace method that has been implemented on the Option type).

This method behavior could be misinterpreted: Updating the Option only if the variant is Some, doing nothing if its None. This other method could exist too and be named map_in_place or modify, no method having this kind of behavior already exist in the Rust std library.

Alternatives

Unresolved questions

None.