We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent ccb160d commit 94ecabfCopy full SHA for 94ecabf
library/core/src/mem/mod.rs
@@ -909,6 +909,10 @@ pub fn take<T: Default>(dest: &mut T) -> T {
909
#[rustc_const_unstable(feature = "const_replace", issue = "83164")]
910
#[cfg_attr(not(test), rustc_diagnostic_item = "mem_replace")]
911
pub const fn replace<T>(dest: &mut T, src: T) -> T {
912
+ // It may be tempting to use `swap` to avoid `unsafe` here. Don't!
913
+ // The compiler optimizes the implementation below to two `memcpy`s
914
+ // while `swap` would require at least three. See PR#83022 for details.
915
+
916
// SAFETY: We read from `dest` but directly write `src` into it afterwards,
917
// such that the old value is not duplicated. Nothing is dropped and
918
// nothing here can panic.
0 commit comments