Skip to content

Include fn take_mut<T, F: FnOnce(T) -> T>(&mut T, F) in libstd #1508

Open
@Sgeo

Description

@Sgeo

It is possible to write a function to temporarily take, by value, a T out of a &mut T, replacing it by the end of the closure. Panics can be handled by killing the process. Crate to do this: https://github.com/Sgeo/take_mut/tree/v0.1.3 , take_mut::take.

This might be reasonable to include in std::mem, possibly under a different name. It's a function that effectively extends what it's possible to do with a &mut T in safe code, similarly to std::mem::swap and std::mem::replace.

Drawbacks include that we might not want to encourage use of functions that, in the worst case, do things more extreme than panicking (exiting or aborting the process).

More bikeshedding opportunities on whether it should exit or abort on panic,

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiRelevant to the library API team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions