about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2023-12-06 17:21:57 +0100
committerGitHub <noreply@github.com>2023-12-06 17:21:57 +0100
commit49d65942787ccc4696877f1d6b40a76790bda601 (patch)
tree6c699bc76b96ae33c02500d65617b3b239997641
parentf1e180451b2782efa1c31e634945da255070d340 (diff)
parentc7d8c65c1a3ca9f1d5cb13c5ea4db4243ea975bf (diff)
downloadrust-49d65942787ccc4696877f1d6b40a76790bda601.tar.gz
rust-49d65942787ccc4696877f1d6b40a76790bda601.zip
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 :)
-rw-r--r--library/alloc/src/boxed.rs14
1 files changed, 11 insertions, 3 deletions
diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs
index 25c63b425ce..fdf5e134f4d 100644
--- a/library/alloc/src/boxed.rs
+++ b/library/alloc/src/boxed.rs
@@ -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));
     /// }
     /// ```
     ///