Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2df731d

Browse files
authoredMar 19, 2025
Rollup merge of rust-lang#138540 - okaneco:const_split_off_first_last, r=m-ou-se
core/slice: Mark some `split_off` variants unstably const Tracking issue: rust-lang#138539 Add feature gate `#![feature(const_split_off_first_last)]` Mark `split_off_first`, `split_off_first_mut`, `split_off_last`, and `split_off_last_mut` slice methods unstably const
2 parents 2ab69b8 + e1388bf commit 2df731d

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed
 

‎library/core/src/slice/mod.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -4445,8 +4445,10 @@ impl<T> [T] {
44454445
/// ```
44464446
#[inline]
44474447
#[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")]
4448-
pub fn split_off_first<'a>(self: &mut &'a Self) -> Option<&'a T> {
4449-
let (first, rem) = self.split_first()?;
4448+
#[rustc_const_unstable(feature = "const_split_off_first_last", issue = "138539")]
4449+
pub const fn split_off_first<'a>(self: &mut &'a Self) -> Option<&'a T> {
4450+
// FIXME(const-hack): Use `?` when available in const instead of `let-else`.
4451+
let Some((first, rem)) = self.split_first() else { return None };
44504452
*self = rem;
44514453
Some(first)
44524454
}
@@ -4468,8 +4470,11 @@ impl<T> [T] {
44684470
/// ```
44694471
#[inline]
44704472
#[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")]
4471-
pub fn split_off_first_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T> {
4472-
let (first, rem) = mem::take(self).split_first_mut()?;
4473+
#[rustc_const_unstable(feature = "const_split_off_first_last", issue = "138539")]
4474+
pub const fn split_off_first_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T> {
4475+
// FIXME(const-hack): Use `mem::take` and `?` when available in const.
4476+
// Original: `mem::take(self).split_first_mut()?`
4477+
let Some((first, rem)) = mem::replace(self, &mut []).split_first_mut() else { return None };
44734478
*self = rem;
44744479
Some(first)
44754480
}
@@ -4490,8 +4495,10 @@ impl<T> [T] {
44904495
/// ```
44914496
#[inline]
44924497
#[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")]
4493-
pub fn split_off_last<'a>(self: &mut &'a Self) -> Option<&'a T> {
4494-
let (last, rem) = self.split_last()?;
4498+
#[rustc_const_unstable(feature = "const_split_off_first_last", issue = "138539")]
4499+
pub const fn split_off_last<'a>(self: &mut &'a Self) -> Option<&'a T> {
4500+
// FIXME(const-hack): Use `?` when available in const instead of `let-else`.
4501+
let Some((last, rem)) = self.split_last() else { return None };
44954502
*self = rem;
44964503
Some(last)
44974504
}
@@ -4513,8 +4520,11 @@ impl<T> [T] {
45134520
/// ```
45144521
#[inline]
45154522
#[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")]
4516-
pub fn split_off_last_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T> {
4517-
let (last, rem) = mem::take(self).split_last_mut()?;
4523+
#[rustc_const_unstable(feature = "const_split_off_first_last", issue = "138539")]
4524+
pub const fn split_off_last_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T> {
4525+
// FIXME(const-hack): Use `mem::take` and `?` when available in const.
4526+
// Original: `mem::take(self).split_last_mut()?`
4527+
let Some((last, rem)) = mem::replace(self, &mut []).split_last_mut() else { return None };
45184528
*self = rem;
45194529
Some(last)
45204530
}

0 commit comments

Comments
 (0)
Failed to load comments.