EDIT: To summarize it a bit better, since it seems like I worded things a bit unclearly:
src's destructor doesn't get run when it's passed by-value to ptr::write, but the docs don't really make this clear. It would be better to clarify this, especially given it's an unsafe function.
#[derive(Debug)]structFoo(i32);implDropforFoo{fndrop(&mutself){println!("{:?} dropped!",self);}}fnmain(){letmut dst = Foo(0);let src = Foo(1);unsafe{ std::ptr::write(&mut dst, src);}println!("dst = {:?}", dst);}This code prints:
dst = Foo(1)
Foo(1) dropped!
demonstrating that the original value stored in dst, and the value passed to std::ptr::write, were not dropped; only dst was dropped after the println! line.
However, the docs for std::ptr::write only mention that the former isn't dropped, saying nothing about the latter.
EDIT: To summarize it a bit better, since it seems like I worded things a bit unclearly:
src's destructor doesn't get run when it's passed by-value toptr::write, but the docs don't really make this clear. It would be better to clarify this, especially given it's an unsafe function.This code prints:
demonstrating that the original value stored in
dst, and the value passed tostd::ptr::write, were not dropped; onlydstwas dropped after theprintln!line.However, the docs for
std::ptr::writeonly mention that the former isn't dropped, saying nothing about the latter.