@@ -29,13 +29,15 @@ use ops::{Deref, DerefMut, CoerceUnsized};
29
29
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
30
30
pub use intrinsics:: transmute;
31
31
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**.
34
33
///
35
34
/// 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.
37
37
///
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
39
41
/// [`mem::drop`][drop].
40
42
///
41
43
/// # Safety
@@ -59,15 +61,6 @@ pub use intrinsics::transmute;
59
61
///
60
62
/// # Examples
61
63
///
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
- ///
71
64
/// Leak an I/O object, never closing the file:
72
65
///
73
66
/// ```no_run
@@ -137,38 +130,13 @@ pub use intrinsics::transmute;
137
130
/// }
138
131
/// ```
139
132
///
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
- ///
166
133
/// [drop]: fn.drop.html
167
134
/// [uninit]: fn.uninitialized.html
168
135
/// [clone]: ../clone/trait.Clone.html
169
136
/// [swap]: fn.swap.html
170
137
/// [FFI]: ../../book/first-edition/ffi.html
171
138
/// [box]: ../../std/boxed/struct.Box.html
139
+ /// [leak]: ../../std/boxed/struct.Box.html#method.leak
172
140
/// [into_raw]: ../../std/boxed/struct.Box.html#method.into_raw
173
141
/// [ub]: ../../reference/behavior-considered-undefined.html
174
142
#[ inline]
0 commit comments