@@ -22,15 +22,54 @@ use ptr;
2222#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2323pub use intrinsics:: transmute;
2424
25- /// Moves a thing into the void.
25+ /// Leaks a value into the void, consuming ownership and never running its
26+ /// destructor.
2627///
27- /// The forget function will take ownership of the provided value but neglect
28- /// to run any required cleanup or memory management operations on it.
28+ /// This function will take ownership of its argument, but is distinct from the
29+ /// `mem::drop` function in that it **does not run the destructor**, leaking the
30+ /// value and any resources that it owns.
2931///
30- /// This function is the unsafe version of the `drop` function because it does
31- /// not run any destructors.
32+ /// # Safety
33+ ///
34+ /// This function is not marked as `unsafe` as Rust does not guarantee that the
35+ /// `Drop` implementation for a value will always run. Note, however, that
36+ /// leaking resources such as memory or I/O objects is likely not desired, so
37+ /// this function is only recommended for specialized use cases.
38+ ///
39+ /// The safety of this function implies that when writing `unsafe` code
40+ /// yourself care must be taken when leveraging a destructor that is required to
41+ /// run to preserve memory safety. There are known situations where the
42+ /// destructor may not run (such as if ownership of the object with the
43+ /// destructor is returned) which must be taken into account.
44+ ///
45+ /// # Other forms of Leakage
46+ ///
47+ /// It's important to point out that this function is not the only method by
48+ /// which a value can be leaked in safe Rust code. Other known sources of
49+ /// leakage are:
50+ ///
51+ /// * `Rc` and `Arc` cycles
52+ /// * `mpsc::{Sender, Receiver}` cycles (they use `Arc` internally)
53+ /// * Panicking destructors are likely to leak local resources
54+ ///
55+ /// # Example
56+ ///
57+ /// ```rust,no_run
58+ /// use std::mem;
59+ /// use std::fs::File;
60+ ///
61+ /// // Leak some heap memory by never deallocating it
62+ /// let heap_memory = Box::new(3);
63+ /// mem::forget(heap_memory);
64+ ///
65+ /// // Leak an I/O object, never closing the file
66+ /// let file = File::open("foo.txt").unwrap();
67+ /// mem::forget(file);
68+ /// ```
3269#[ stable( feature = "rust1" , since = "1.0.0" ) ]
33- pub use intrinsics:: forget;
70+ pub fn forget < T > ( t : T ) {
71+ unsafe { intrinsics:: forget ( t) }
72+ }
3473
3574/// Returns the size of a type in bytes.
3675///
0 commit comments