Skip to content

Commit 55020c6

Browse files
committed
Reverse ordering of split_{first,last}_chunk to be (preceding, last)
These methods currently return `(last_chunk, preceding_slice)`, which matches the existing `split_x` methods that remove one item. Change these to instead return `(preceding_slice, last_chunk)` which matches string split methods, should be more intuitive, and will allow for consistency with methods that split more items.
1 parent 9c20ddd commit 55020c6

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

library/core/src/slice/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -456,15 +456,15 @@ impl<T> [T] {
456456
///
457457
/// let x = &[0, 1, 2];
458458
///
459-
/// if let Some((last, elements)) = x.split_last_chunk::<2>() {
460-
/// assert_eq!(last, &[1, 2]);
459+
/// if let Some((elements, last)) = x.split_last_chunk::<2>() {
461460
/// assert_eq!(elements, &[0]);
461+
/// assert_eq!(last, &[1, 2]);
462462
/// }
463463
/// ```
464464
#[unstable(feature = "slice_first_last_chunk", issue = "111774")]
465465
#[rustc_const_unstable(feature = "slice_first_last_chunk", issue = "111774")]
466466
#[inline]
467-
pub const fn split_last_chunk<const N: usize>(&self) -> Option<(&[T; N], &[T])> {
467+
pub const fn split_last_chunk<const N: usize>(&self) -> Option<(&[T], &[T; N])> {
468468
if self.len() < N {
469469
None
470470
} else {
@@ -473,7 +473,7 @@ impl<T> [T] {
473473

474474
// SAFETY: We explicitly check for the correct number of elements,
475475
// and do not let the references outlive the slice.
476-
Some((unsafe { &*(last.as_ptr() as *const [T; N]) }, init))
476+
Some((init, unsafe { &*(last.as_ptr() as *const [T; N]) }))
477477
}
478478
}
479479

@@ -486,7 +486,7 @@ impl<T> [T] {
486486
///
487487
/// let x = &mut [0, 1, 2];
488488
///
489-
/// if let Some((last, elements)) = x.split_last_chunk_mut::<2>() {
489+
/// if let Some((elements, last)) = x.split_last_chunk_mut::<2>() {
490490
/// last[0] = 3;
491491
/// last[1] = 4;
492492
/// elements[0] = 5;
@@ -498,7 +498,7 @@ impl<T> [T] {
498498
#[inline]
499499
pub const fn split_last_chunk_mut<const N: usize>(
500500
&mut self,
501-
) -> Option<(&mut [T; N], &mut [T])> {
501+
) -> Option<(&mut [T], &mut [T; N])> {
502502
if self.len() < N {
503503
None
504504
} else {
@@ -508,7 +508,7 @@ impl<T> [T] {
508508
// SAFETY: We explicitly check for the correct number of elements,
509509
// do not let the reference outlive the slice,
510510
// and enforce exclusive mutability of the chunk by the split.
511-
Some((unsafe { &mut *(last.as_mut_ptr() as *mut [T; N]) }, init))
511+
Some((init, unsafe { &mut *(last.as_mut_ptr() as *mut [T; N]) }))
512512
}
513513
}
514514

0 commit comments

Comments
 (0)