rust-lang / rust-lang/rfcs

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

Open
#1,508 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

T-libs
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

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,

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the linked take_mut crate, especially take_mut::take, and compare its proposed role with std::mem::swap and std::mem::replace. The work is complete only after the API name and panic behavior are resolved and the inclusion of this function in libstd is agreed.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.