diff --git a/library/core/src/mem/manually_drop.rs b/library/core/src/mem/manually_drop.rs index e45aa86c0795a..aab0e96d83ab9 100644 --- a/library/core/src/mem/manually_drop.rs +++ b/library/core/src/mem/manually_drop.rs @@ -74,8 +74,12 @@ impl ManuallyDrop { /// /// ```rust /// use std::mem::ManuallyDrop; - /// ManuallyDrop::new(Box::new(())); + /// let mut x = ManuallyDrop::new(String::from("Hello World!")); + /// x.truncate(5); // You can still safely operate on the value + /// assert_eq!(*x, "Hello"); + /// // But `Drop` will not be run here /// ``` + #[must_use = "if you don't need the wrapper, you can use `mem::forget` instead"] #[stable(feature = "manually_drop", since = "1.20.0")] #[rustc_const_stable(feature = "const_manually_drop", since = "1.36.0")] #[inline(always)] diff --git a/library/core/src/mem/mod.rs b/library/core/src/mem/mod.rs index 9107c570a8970..6d8ed2f4ffb1a 100644 --- a/library/core/src/mem/mod.rs +++ b/library/core/src/mem/mod.rs @@ -145,7 +145,7 @@ pub use crate::intrinsics::transmute; #[rustc_const_stable(feature = "const_forget", since = "1.46.0")] #[stable(feature = "rust1", since = "1.0.0")] pub const fn forget(t: T) { - ManuallyDrop::new(t); + let _ = ManuallyDrop::new(t); } /// Like [`forget`], but also accepts unsized values. diff --git a/library/std/src/lazy.rs b/library/std/src/lazy.rs index f0548582d2f5d..d0f27df518505 100644 --- a/library/std/src/lazy.rs +++ b/library/std/src/lazy.rs @@ -293,7 +293,7 @@ impl SyncOnceCell { // Don't drop this `SyncOnceCell`. We just moved out one of the fields, but didn't set // the state to uninitialized. - mem::ManuallyDrop::new(self); + mem::forget(self); inner }