Skip to content

Commit 2065c4b

Browse files
authored
Rollup merge of rust-lang#85464 - steffahn:fix_ub_in_ptr_swap_docs, r=dtolnay
Fix UB in documented example for `ptr::swap` Compare [this (short) discussion on zulip](https://rust-lang.zulipchat.com/#narrow/stream/122651-general/topic/Pointers.20to.20overlapping.20arrays) (or [in the archive](https://zulip-archive.rust-lang.org/122651general/92017Pointerstooverlappingarrays.html), if you don’t have an account). ``@rustbot`` label T-doc T-libs
2 parents 1207b7f + f9752b4 commit 2065c4b

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

library/core/src/ptr/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,12 @@ pub const fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T] {
342342
/// ```
343343
/// use std::ptr;
344344
///
345-
/// let mut array = [0, 1, 2, 3];
345+
/// let mut array: [i32; 4] = [0, 1, 2, 3];
346+
///
347+
/// let array_ptr: *mut i32 = array.as_mut_ptr();
346348
///
347-
/// let x = array[0..].as_mut_ptr() as *mut [u32; 3]; // this is `array[0..3]`
348-
/// let y = array[1..].as_mut_ptr() as *mut [u32; 3]; // this is `array[1..4]`
349+
/// let x = array_ptr as *mut [i32; 3]; // this is `array[0..3]`
350+
/// let y = unsafe { array_ptr.add(1) } as *mut [i32; 3]; // this is `array[1..4]`
349351
///
350352
/// unsafe {
351353
/// ptr::swap(x, y);

0 commit comments

Comments
 (0)