W3cubDocs

/Rust

Function std::ptr::drop_in_place

#[lang = "drop_in_place"]
pub unsafe fn drop_in_place<T>(to_drop: *mut T) where    T: ?Sized, 

Executes the destructor (if any) of the pointed-to value.

This has two use cases:

  • It is required to use drop_in_place to drop unsized types like trait objects, because they can't be read out onto the stack and dropped normally.

  • It is friendlier to the optimizer to do this over ptr::read when dropping manually allocated memory (e.g. when writing Box/Rc/Vec), as the compiler doesn't need to prove that it's sound to elide the copy.

Safety

This has all the same safety problems as ptr::read with respect to invalid pointers, types, and double drops.

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/ptr/fn.drop_in_place.html