Description
Feature gate: #![feature(slice_first_last_chunk)]
This is a tracking issue for the slice::{split_,}{first,last}_chunk{,_mut}
API.
Public API
2024-09-06: Most of this is stable now, except for the const-ness of the mut
methods.
impl [T] {
pub const fn first_chunk<const N: usize>(&self) -> Option<&[T; N]>;
pub const fn first_chunk_mut<const N: usize>(&mut self) -> Option<&mut [T; N]>;
pub const fn last_chunk<const N: usize>(&self) -> Option<&[T; N]>;
pub const fn last_chunk_mut<const N: usize>(&mut self) -> Option<&mut [T; N]>;
pub const fn split_first_chunk<const N: usize>(&self) -> Option<(&[T; N], &[T])>;
pub const fn split_first_chunk_mut<const N: usize>(&mut self) -> Option<(&mut [T; N], &mut [T])>;
pub const fn split_last_chunk<const N: usize>(&self) -> Option<(&[T; N], &[T])>;
pub const fn split_last_chunk_mut<const N: usize>(&mut self) -> Option<(&mut [T; N], &mut [T])>;
}
Note that this mirrors the existing, already-stabilised API:
impl [T] {
pub const fn first(&self) -> Option<&T>;
pub const fn first_mut(&mut self) -> Option<&mut T>;
pub const fn last(&self) -> Option<&T>;
pub const fn last_mut(&mut self) -> Option<&mut T>;
pub const fn split_first(&self) -> Option<(&T, &[T])>;
pub const fn split_first_mut(&mut self) -> Option<(&mut T, &mut [T])>;
pub const fn split_last(&self) -> Option<(&T, &[T])>;
pub const fn split_last_mut(&mut self) -> Option<(&mut T, &mut [T])>;
}
Steps / History
- API change proposal (ACP)1: ACP: slice::{split_,}{first,last}_chunk libs-team#69
- Implementation: Add slice::{split_,}{first,last}_chunk{,_mut} #95198
- Final comment period (FCP)2
- Wait until Tracking Issue for
feature(const_slice_split_at_mut)
#101804 is stable - Stabilization PR
Unresolved Questions
- Is this the best order for the tuple in
split_last_chunk
? Consistency withsplit_last
would have it be-> Option<(&[T;N], &[T])>
as was ACPed, but it might be confusing to have the two slice-like things in the opposite order from every other API I can think of that returns two subslices of the original. In Add slice::{split_,}{first,last}_chunk{,_mut} #95198 (comment), @scottmcm expected-> Option<(&[T;N], &[T])>
forsplit_first_chunk
, but for it to be-> Option<(&[T], &[T;N])>
insplit_last_chunk
so the tuple is "in order", in some sense. - ...