diff --git a/src/libcore/intrinsics.rs b/src/libcore/intrinsics.rs index 927c6f26b62a7..659b7a23ed922 100644 --- a/src/libcore/intrinsics.rs +++ b/src/libcore/intrinsics.rs @@ -1146,7 +1146,7 @@ extern "rust-intrinsic" { /// Creating an invalid value: /// /// ``` - /// use std::{mem, ptr}; + /// use std::ptr; /// /// let mut v = Box::new(0i32); /// @@ -1162,8 +1162,10 @@ extern "rust-intrinsic" { /// // Even leaking `v` "uses" it, and henc eis undefined behavior. /// // mem::forget(v); // ERROR /// - /// // Let us instead put in a valid value - /// ptr::write(&mut v, Box::new(42i32); + /// unsafe { + /// // Let us instead put in a valid value + /// ptr::write(&mut v, Box::new(42i32)); + /// } /// /// // Now the box is fine /// assert_eq!(*v, 42); diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index adf107dca2231..dba8513d5f099 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -136,12 +136,14 @@ pub use intrinsics::write_bytes; /// let mut v = vec![Rc::new(0), last]; /// /// unsafe { +/// // Get a raw pointer to the last element in `v`. +/// let ptr = &mut v[1] as *mut _; /// // Shorten `v` to prevent the last item from being dropped. We do that first, /// // to prevent issues if the `drop_in_place` below panics. /// v.set_len(1); /// // Without a call `drop_in_place`, the last item would never be dropped, /// // and the memory it manages would be leaked. -/// ptr::drop_in_place(&mut v[1]); +/// ptr::drop_in_place(ptr); /// } /// /// assert_eq!(v, &[0.into()]);