Skip to content

Commit adcc0d2

Browse files
committed
clarify swap
1 parent 0ec87d0 commit adcc0d2

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/libcore/ptr.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ pub const fn null_mut<T>() -> *mut T { 0 as *mut T }
236236
///
237237
/// * The two pointed-to values may overlap. If the values do overlap, then the
238238
/// overlapping region of memory from `x` will be used. This is demonstrated
239-
/// in the examples below.
239+
/// in the second example below.
240240
///
241241
/// [`mem::swap`]: ../mem/fn.swap.html
242242
///
@@ -261,8 +261,8 @@ pub const fn null_mut<T>() -> *mut T { 0 as *mut T }
261261
///
262262
/// let mut array = [0, 1, 2, 3];
263263
///
264-
/// let x = array[0..].as_mut_ptr() as *mut [u32; 2];
265-
/// let y = array[2..].as_mut_ptr() as *mut [u32; 2];
264+
/// let x = array[0..].as_mut_ptr() as *mut [u32; 2]; // this is `array[0..2]`
265+
/// let y = array[2..].as_mut_ptr() as *mut [u32; 2]; // this is `array[2..4]`
266266
///
267267
/// unsafe {
268268
/// ptr::swap(x, y);
@@ -277,11 +277,16 @@ pub const fn null_mut<T>() -> *mut T { 0 as *mut T }
277277
///
278278
/// let mut array = [0, 1, 2, 3];
279279
///
280-
/// let x = array[0..].as_mut_ptr() as *mut [u32; 3];
281-
/// let y = array[1..].as_mut_ptr() as *mut [u32; 3];
280+
/// let x = array[0..].as_mut_ptr() as *mut [u32; 3]; // this is `array[0..3]`
281+
/// let y = array[1..].as_mut_ptr() as *mut [u32; 3]; // this is `array[1..4]`
282282
///
283283
/// unsafe {
284284
/// ptr::swap(x, y);
285+
/// // The indices `1..3` of the slice overlap between `x` and `y`.
286+
/// // Reasonable results would be for to them be `[2, 3]`, so that indices `0..3` are
287+
/// // `[1, 2, 3]` (matching `y` before the `swap`); or for them to be `[0, 1]`
288+
/// // so that indices `1..4` are `[0, 1, 2]` (matching `x` before the `swap`).
289+
/// // This implementation is defined to make the latter choice.
285290
/// assert_eq!([1, 0, 1, 2], array);
286291
/// }
287292
/// ```

0 commit comments

Comments
 (0)