Description
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.
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 atdest
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 😄