Skip to content

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

Closed
@vitiral

Description

@vitiral

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 😄

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions