4 ms·
let x = String::from("abc"); std::mem::drop(&x); std::mem::drop(&x); std::mem::drop(&x); std::mem::drop(&x);
by millstone 7y ago
let x = String::from("abc");
std::mem::drop(&x);
std::mem::drop(&x);
std::mem::drop(&x);
std::mem::drop(&x);
- hathawsh 7y agoFWIW, that won't compile because std::mem::drop requires ownership of the object being passed; your code is trying to pass a reference instead.
- lpghatguy 7y agoThis code compiles just fine. It doesn't do anything since immutable references are Copy, and so dropping them doesn't do anything. https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f2b4935fbb248718f1f2878ee4c53409 https://play.rust-lang.org/?version=stable&mode=debug&editio...
- hathawsh 7y agoOh right, I forgot about the interaction with generics in this case. Semantically, I suppose it's creating and dropping a reference 4 times.
- steveklabnik 7y agoThis should compile, &T is Copy and so it will not really do anything, but it will still compile. On my phone so I can’t triple check, but there’s no bound on T, you can pass in any type.