diff options
| author | alpharush <0xalpharush@protonmail.com> | 2023-11-03 21:45:19 -0500 |
|---|---|---|
| committer | alpharush <0xalpharush@protonmail.com> | 2023-11-04 03:00:43 -0500 |
| commit | c7d8c65c1a3ca9f1d5cb13c5ea4db4243ea975bf (patch) | |
| tree | 4811465b5450598f86ca4ebfc06a8a75f592f41e | |
| parent | f1b104f52350c800f78d4372aec52080bd6f9164 (diff) | |
| download | rust-c7d8c65c1a3ca9f1d5cb13c5ea4db4243ea975bf.tar.gz rust-c7d8c65c1a3ca9f1d5cb13c5ea4db4243ea975bf.zip | |
docs: clarify explicitly freeing heap allocated memory
| -rw-r--r-- | library/alloc/src/boxed.rs | 14 |
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)); /// } /// ``` /// |
