Skip to content

Commit f7d875a

Browse files
authored
Unrolled build for rust-lang#117563
Rollup merge of rust-lang#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 :)
2 parents f32d298 + c7d8c65 commit f7d875a

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

library/alloc/src/boxed.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -1038,10 +1038,18 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
10381038
/// use std::ptr;
10391039
///
10401040
/// let x = Box::new(String::from("Hello"));
1041-
/// let p = Box::into_raw(x);
1041+
/// let ptr = Box::into_raw(x);
1042+
/// unsafe {
1043+
/// ptr::drop_in_place(ptr);
1044+
/// dealloc(ptr as *mut u8, Layout::new::<String>());
1045+
/// }
1046+
/// ```
1047+
/// Note: This is equivalent to the following:
1048+
/// ```
1049+
/// let x = Box::new(String::from("Hello"));
1050+
/// let ptr = Box::into_raw(x);
10421051
/// unsafe {
1043-
/// ptr::drop_in_place(p);
1044-
/// dealloc(p as *mut u8, Layout::new::<String>());
1052+
/// drop(Box::from_raw(ptr));
10451053
/// }
10461054
/// ```
10471055
///

0 commit comments

Comments
 (0)