Fix UB in documented example for ptr::swap
This commit is contained in:
parent
be8450eec8
commit
f9752b4930
1 changed files with 5 additions and 3 deletions
|
@ -342,10 +342,12 @@ pub const fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T] {
|
|||
/// ```
|
||||
/// use std::ptr;
|
||||
///
|
||||
/// let mut array = [0, 1, 2, 3];
|
||||
/// let mut array: [i32; 4] = [0, 1, 2, 3];
|
||||
///
|
||||
/// let x = array[0..].as_mut_ptr() as *mut [u32; 3]; // this is `array[0..3]`
|
||||
/// let y = array[1..].as_mut_ptr() as *mut [u32; 3]; // this is `array[1..4]`
|
||||
/// let array_ptr: *mut i32 = array.as_mut_ptr();
|
||||
///
|
||||
/// let x = array_ptr as *mut [i32; 3]; // this is `array[0..3]`
|
||||
/// let y = unsafe { array_ptr.add(1) } as *mut [i32; 3]; // this is `array[1..4]`
|
||||
///
|
||||
/// unsafe {
|
||||
/// ptr::swap(x, y);
|
||||
|
|
Loading…
Add table
Reference in a new issue