@@ -63,62 +63,6 @@ pub use crate::intrinsics::transmute;
63
63
/// The practical use cases for `forget` are rather specialized and mainly come
64
64
/// up in unsafe or FFI code.
65
65
///
66
- /// ## Use case 1
67
- ///
68
- /// You have created an uninitialized value using [`mem::uninitialized`][uninit].
69
- /// You must either initialize or `forget` it on every computation path before
70
- /// Rust drops it automatically, like at the end of a scope or after a panic.
71
- /// Running the destructor on an uninitialized value would be [undefined behavior][ub].
72
- ///
73
- /// ```
74
- /// use std::mem;
75
- /// use std::ptr;
76
- ///
77
- /// # let some_condition = false;
78
- /// unsafe {
79
- /// let mut uninit_vec: Vec<u32> = mem::uninitialized();
80
- ///
81
- /// if some_condition {
82
- /// // Initialize the variable.
83
- /// ptr::write(&mut uninit_vec, Vec::new());
84
- /// } else {
85
- /// // Forget the uninitialized value so its destructor doesn't run.
86
- /// mem::forget(uninit_vec);
87
- /// }
88
- /// }
89
- /// ```
90
- ///
91
- /// ## Use case 2
92
- ///
93
- /// You have duplicated the bytes making up a value, without doing a proper
94
- /// [`Clone`][clone]. You need the value's destructor to run only once,
95
- /// because a double `free` is undefined behavior.
96
- ///
97
- /// An example is a possible implementation of [`mem::swap`][swap]:
98
- ///
99
- /// ```
100
- /// use std::mem;
101
- /// use std::ptr;
102
- ///
103
- /// # #[allow(dead_code)]
104
- /// fn swap<T>(x: &mut T, y: &mut T) {
105
- /// unsafe {
106
- /// // Give ourselves some scratch space to work with
107
- /// let mut t: T = mem::uninitialized();
108
- ///
109
- /// // Perform the swap, `&mut` pointers never alias
110
- /// ptr::copy_nonoverlapping(&*x, &mut t, 1);
111
- /// ptr::copy_nonoverlapping(&*y, x, 1);
112
- /// ptr::copy_nonoverlapping(&t, y, 1);
113
- ///
114
- /// // y and t now point to the same thing, but we need to completely
115
- /// // forget `t` because we do not want to run the destructor for `T`
116
- /// // on its value, which is still owned somewhere outside this function.
117
- /// mem::forget(t);
118
- /// }
119
- /// }
120
- /// ```
121
- ///
122
66
/// [drop]: fn.drop.html
123
67
/// [uninit]: fn.uninitialized.html
124
68
/// [clone]: ../clone/trait.Clone.html
0 commit comments