3 ms·
Thanks for the example! Yeah, I'm realizing my framing (around the Drop trait, and not "droppability" as a concept) was incorrect. Would it be more accurate to
by woodruffw 4y ago
Thanks for the example! Yeah, I'm realizing my framing (around the Drop trait, and not "droppability" as a concept) was incorrect.
Would it be more accurate to say that Rust guarantees the droppability of owned values? I know there's a guarantee that &'static items never have their underlying value dropped, but that works because you can never actually hold the static value itself, only its reference.
- steveklabnik 4y ago> Would it be more accurate to say that Rust guarantees the droppability of owned values? I'm not really sure, exactly, since "droppability" isn't really a thing that's talked about, because as you're sort of getting at here, it's universal, and therefore not really an interesting concept. > I know there's a guarantee that &'static items never have their underlying value dropped, Even this is sort of... not correct. Consider this program: #[derive(Debug)] struct Boom; impl Drop for Boom { fn drop(&mut self) { println!("BOOM"); } } use std::mem; static mut FOO: Boom = Boom; fn main() { let mut s = Boom; unsafe { dbg!(&FOO); mem::swap(&mut FOO, &mut s); dbg!(&FOO); } } This will print BOOM, as the initial Boom is swapped out from behind the reference and then dropped.
- steveklabnik 4y ago(Oh I just realized, for those not super familiar with Rust, mem::swap is safe but since dereferencing FOO is unsafe, I just left in one block, not two.)