File tree 2 files changed +8
-4
lines changed
2 files changed +8
-4
lines changed Original file line number Diff line number Diff line change @@ -1146,7 +1146,7 @@ extern "rust-intrinsic" {
1146
1146
/// Creating an invalid value:
1147
1147
///
1148
1148
/// ```
1149
- /// use std::{mem, ptr} ;
1149
+ /// use std::ptr;
1150
1150
///
1151
1151
/// let mut v = Box::new(0i32);
1152
1152
///
@@ -1162,8 +1162,10 @@ extern "rust-intrinsic" {
1162
1162
/// // Even leaking `v` "uses" it, and henc eis undefined behavior.
1163
1163
/// // mem::forget(v); // ERROR
1164
1164
///
1165
- /// // Let us instead put in a valid value
1166
- /// ptr::write(&mut v, Box::new(42i32);
1165
+ /// unsafe {
1166
+ /// // Let us instead put in a valid value
1167
+ /// ptr::write(&mut v, Box::new(42i32));
1168
+ /// }
1167
1169
///
1168
1170
/// // Now the box is fine
1169
1171
/// assert_eq!(*v, 42);
Original file line number Diff line number Diff line change @@ -136,12 +136,14 @@ pub use intrinsics::write_bytes;
136
136
/// let mut v = vec![Rc::new(0), last];
137
137
///
138
138
/// unsafe {
139
+ /// // Get a raw pointer to the last element in `v`.
140
+ /// let ptr = &mut v[1] as *mut _;
139
141
/// // Shorten `v` to prevent the last item from being dropped. We do that first,
140
142
/// // to prevent issues if the `drop_in_place` below panics.
141
143
/// v.set_len(1);
142
144
/// // Without a call `drop_in_place`, the last item would never be dropped,
143
145
/// // and the memory it manages would be leaked.
144
- /// ptr::drop_in_place(&mut v[1] );
146
+ /// ptr::drop_in_place(ptr );
145
147
/// }
146
148
///
147
149
/// assert_eq!(v, &[0.into()]);
You can’t perform that action at this time.
0 commit comments