mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-01 15:01:51 +00:00
Rollup merge of #117563 - 0xalpharush:docs/into-raw, r=workingjubilee
docs: clarify explicitly freeing heap allocated memory The documentation for `Box::into_raw` didn't mention `drop` and wondered if I was doing something wrong. Based off [this](https://stackoverflow.com/questions/75441199/rust-how-do-i-correctly-free-heap-allocated-memory), I think it's helpful to include the more concise yet explicit way to free heap allocated memory. This is my first rust PR and I went through https://std-dev-guide.rust-lang.org/development/, but let me know if I missed something :)
This commit is contained in:
commit
49d6594278
@ -1038,10 +1038,18 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
|
||||
/// use std::ptr;
|
||||
///
|
||||
/// let x = Box::new(String::from("Hello"));
|
||||
/// let p = Box::into_raw(x);
|
||||
/// let ptr = Box::into_raw(x);
|
||||
/// unsafe {
|
||||
/// ptr::drop_in_place(p);
|
||||
/// dealloc(p as *mut u8, Layout::new::<String>());
|
||||
/// ptr::drop_in_place(ptr);
|
||||
/// dealloc(ptr as *mut u8, Layout::new::<String>());
|
||||
/// }
|
||||
/// ```
|
||||
/// Note: This is equivalent to the following:
|
||||
/// ```
|
||||
/// let x = Box::new(String::from("Hello"));
|
||||
/// let ptr = Box::into_raw(x);
|
||||
/// unsafe {
|
||||
/// drop(Box::from_raw(ptr));
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
|
Loading…
Reference in New Issue
Block a user