Skip to content

Commit 973428d

Browse files
authored
Rollup merge of rust-lang#53503 - kornelski:master, r=dtolnay
Discourage overuse of mem::forget Some uses of `mem::forget` have been replaced by better methods of `Box`, so I've removed obsoleted use-cases from these docs. I've removed emphasis on leaking, because it's not obvious `mem::forget` does not guarantee leaking of memory: memory of stack-allocated objects and values partially moved out of `Box` will still be freed. That's a potential error when used to pass objects to FFI, so it's better to direct users to `Box::into_raw` instead.
2 parents ab5a71b + e7709b3 commit 973428d

File tree

1 file changed

+7
-39
lines changed

1 file changed

+7
-39
lines changed

src/libcore/mem.rs

+7-39
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ use ops::{Deref, DerefMut, CoerceUnsized};
2929
#[stable(feature = "rust1", since = "1.0.0")]
3030
pub use intrinsics::transmute;
3131

32-
/// Leaks a value: takes ownership and "forgets" about the value **without running
33-
/// its destructor**.
32+
/// Takes ownership and "forgets" about the value **without running its destructor**.
3433
///
3534
/// Any resources the value manages, such as heap memory or a file handle, will linger
36-
/// forever in an unreachable state.
35+
/// forever in an unreachable state. However, it does not guarantee that pointers
36+
/// to this memory will remain valid.
3737
///
38-
/// If you want to dispose of a value properly, running its destructor, see
38+
/// * If you want to leak memory, see [`Box::leak`][leak].
39+
/// * If you want to obtain a raw pointer to the memory, see [`Box::into_raw`][into_raw].
40+
/// * If you want to dispose of a value properly, running its destructor, see
3941
/// [`mem::drop`][drop].
4042
///
4143
/// # Safety
@@ -59,15 +61,6 @@ pub use intrinsics::transmute;
5961
///
6062
/// # Examples
6163
///
62-
/// Leak some heap memory by never deallocating it:
63-
///
64-
/// ```
65-
/// use std::mem;
66-
///
67-
/// let heap_memory = Box::new(3);
68-
/// mem::forget(heap_memory);
69-
/// ```
70-
///
7164
/// Leak an I/O object, never closing the file:
7265
///
7366
/// ```no_run
@@ -137,38 +130,13 @@ pub use intrinsics::transmute;
137130
/// }
138131
/// ```
139132
///
140-
/// ## Use case 3
141-
///
142-
/// You are transferring ownership across a [FFI] boundary to code written in
143-
/// another language. You need to `forget` the value on the Rust side because Rust
144-
/// code is no longer responsible for it.
145-
///
146-
/// ```no_run
147-
/// use std::mem;
148-
///
149-
/// extern "C" {
150-
/// fn my_c_function(x: *const u32);
151-
/// }
152-
///
153-
/// let x: Box<u32> = Box::new(3);
154-
///
155-
/// // Transfer ownership into C code.
156-
/// unsafe {
157-
/// my_c_function(&*x);
158-
/// }
159-
/// mem::forget(x);
160-
/// ```
161-
///
162-
/// In this case, C code must call back into Rust to free the object. Calling C's `free`
163-
/// function on a [`Box`][box] is *not* safe! Also, `Box` provides an [`into_raw`][into_raw]
164-
/// method which is the preferred way to do this in practice.
165-
///
166133
/// [drop]: fn.drop.html
167134
/// [uninit]: fn.uninitialized.html
168135
/// [clone]: ../clone/trait.Clone.html
169136
/// [swap]: fn.swap.html
170137
/// [FFI]: ../../book/first-edition/ffi.html
171138
/// [box]: ../../std/boxed/struct.Box.html
139+
/// [leak]: ../../std/boxed/struct.Box.html#method.leak
172140
/// [into_raw]: ../../std/boxed/struct.Box.html#method.into_raw
173141
/// [ub]: ../../reference/behavior-considered-undefined.html
174142
#[inline]

0 commit comments

Comments
 (0)