Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve docs for std::mem::replace #50657

Closed
vitiral opened this issue May 11, 2018 · 4 comments
Closed

improve docs for std::mem::replace #50657

vitiral opened this issue May 11, 2018 · 4 comments
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools

Comments

@vitiral
Copy link
Contributor

vitiral commented May 11, 2018

I came across this reddit comment. This is a "safe" feature of rust I didn't know about.

I was at first confused about the docs for std::mem::replace however. My main confusion was around the "deinitializing" word which made me think that T is not consumed. It is consumed but it is not dropped, which is a subtle distinction. I propose here to remove the confusing language.

Current docs

pub fn replace<T>(dest: &mut T, src: T) -> T

Replaces the value at a mutable location with a new one, returning the old value, without deinitializing either one.

Suggested new docs:

pub fn replace<T>(dest: &mut T, src: T) -> T

Consumes src to replace the value at dest in-place, returning the old value.

Neither value is dropped.

Another possible option (I don't like it as much but it is more similar to the previous docs):

pub fn replace<T>(dest: &mut T, src: T) -> T

Replaces the value at a mutable location with a new one, returning the old value.

Neither value is dropped.

As a side note, I have not seen the "deinitialized" qualifier in rust before. I don't think it is part of the standard rust lingo which is probably some of my confusion 😄

@nagisa nagisa added the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label May 11, 2018
@CAD97
Copy link
Contributor

CAD97 commented May 11, 2018

Another option, which I think is the best of both worlds:

Moves src behind the mutable reference dest, returning the previous value.

Neither value is dropped.

The important points are to mention that src is moved, and to use standard Drop terminology rather than "deinitialized".

@oli-obk
Copy link
Contributor

oli-obk commented May 11, 2018

I like the third option the most:

Replaces the value at a mutable location with a new one, returning the old value.

Neither value is dropped.

@vitiral
Copy link
Contributor Author

vitiral commented May 11, 2018

@CAD97 I have to say I like yours the best. Maybe change "Moves src behind the mutable reference" to "Moves src into the mutable reference"? I'm not sure "behind" is standard lingo either 😄

@frewsxcv
Copy link
Member

opened a pr #51124

frewsxcv added a commit to frewsxcv/rust that referenced this issue Jun 2, 2018
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue Jun 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools
Projects
None yet
Development

No branches or pull requests

5 participants