Skip to content

Commit

Permalink
Rollup merge of rust-lang#117563 - 0xalpharush:docs/into-raw, r=worki…
Browse files Browse the repository at this point in the history
…ngjubilee

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 :)
  • Loading branch information
matthiaskrgr authored Dec 6, 2023
2 parents f1e1804 + c7d8c65 commit 49d6594
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(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 {
/// ptr::drop_in_place(p);
/// dealloc(p as *mut u8, Layout::new::<String>());
/// drop(Box::from_raw(ptr));
/// }
/// ```
///
Expand Down

0 comments on commit 49d6594

Please sign in to comment.