Skip to content

Tracking Issue for const_split_off_first_last #138539

Open

Description

@okaneco

Feature gate: #![feature(const_split_off_first_last)]

This is a tracking issue for using <[T]>::split_off_* functions in const. These functions remove either the first or last element from a slice and return a reference or mutable reference to that element.

The original functions were stabilized with the slice_take feature.
slice_take tracking issue: #62280

Public API

impl<T> [T] {
    pub const fn split_off_first<'a>(self: &mut &'a Self) -> Option<&'a T>;
    pub const fn split_off_first_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T>;
    pub const fn split_off_last<'a>(self: &mut &'a Self) -> Option<&'a T>;
    pub const fn split_off_last_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T>;
}

Steps / History

Unresolved Questions

  • None yet.

Footnotes

  1. https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html ↩

Activity

  1. added
    C-tracking-issue
    T-libs-api
    on Mar 15, 2025
  2. added a commit that references this issue on Mar 19, 2025
  3. added a commit that references this issue on Mar 20, 2025
  4. added a commit that references this issue on Apr 2, 2025
  5. okaneco commented on Dec 2, 2025

    @okaneco
    ContributorAuthor

    I'd like to stabilize this feature.

    This would make the following functions on slices const-stable.
    These functions were stabilized in 1.87 and made const-unstable in March 2025.
    They are implementable in safe stable code, and I'm not aware of any blockers.

    impl<T> [T] {
        pub const fn split_off_first<'a>(self: &mut &'a Self) -> Option<&'a T>;
        pub const fn split_off_first_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T>;
        pub const fn split_off_last<'a>(self: &mut &'a Self) -> Option<&'a T>;
        pub const fn split_off_last_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T>;
    }

    Current implementation:

    /// Removes the first element of the slice and returns a reference
    /// to it.
    ///
    /// Returns `None` if the slice is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut slice: &[_] = &['a', 'b', 'c'];
    /// let first = slice.split_off_first().unwrap();
    ///
    /// assert_eq!(slice, &['b', 'c']);
    /// assert_eq!(first, &'a');
    /// ```
    #[inline]
    #[stable(feature = "slice_take", since = "1.87.0")]
    #[rustc_const_unstable(feature = "const_split_off_first_last", issue = "138539")]
    pub const fn split_off_first<'a>(self: &mut &'a Self) -> Option<&'a T> {
    // FIXME(const-hack): Use `?` when available in const instead of `let-else`.
    let Some((first, rem)) = self.split_first() else { return None };
    *self = rem;
    Some(first)
    }
    /// Removes the first element of the slice and returns a mutable
    /// reference to it.
    ///
    /// Returns `None` if the slice is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut slice: &mut [_] = &mut ['a', 'b', 'c'];
    /// let first = slice.split_off_first_mut().unwrap();
    /// *first = 'd';
    ///
    /// assert_eq!(slice, &['b', 'c']);
    /// assert_eq!(first, &'d');
    /// ```
    #[inline]
    #[stable(feature = "slice_take", since = "1.87.0")]
    #[rustc_const_unstable(feature = "const_split_off_first_last", issue = "138539")]
    pub const fn split_off_first_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T> {
    // FIXME(const-hack): Use `mem::take` and `?` when available in const.
    // Original: `mem::take(self).split_first_mut()?`
    let Some((first, rem)) = mem::replace(self, &mut []).split_first_mut() else { return None };
    *self = rem;
    Some(first)
    }
    /// Removes the last element of the slice and returns a reference
    /// to it.
    ///
    /// Returns `None` if the slice is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut slice: &[_] = &['a', 'b', 'c'];
    /// let last = slice.split_off_last().unwrap();
    ///
    /// assert_eq!(slice, &['a', 'b']);
    /// assert_eq!(last, &'c');
    /// ```
    #[inline]
    #[stable(feature = "slice_take", since = "1.87.0")]
    #[rustc_const_unstable(feature = "const_split_off_first_last", issue = "138539")]
    pub const fn split_off_last<'a>(self: &mut &'a Self) -> Option<&'a T> {
    // FIXME(const-hack): Use `?` when available in const instead of `let-else`.
    let Some((last, rem)) = self.split_last() else { return None };
    *self = rem;
    Some(last)
    }
    /// Removes the last element of the slice and returns a mutable
    /// reference to it.
    ///
    /// Returns `None` if the slice is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut slice: &mut [_] = &mut ['a', 'b', 'c'];
    /// let last = slice.split_off_last_mut().unwrap();
    /// *last = 'd';
    ///
    /// assert_eq!(slice, &['a', 'b']);
    /// assert_eq!(last, &'d');
    /// ```
    #[inline]
    #[stable(feature = "slice_take", since = "1.87.0")]
    #[rustc_const_unstable(feature = "const_split_off_first_last", issue = "138539")]
    pub const fn split_off_last_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T> {
    // FIXME(const-hack): Use `mem::take` and `?` when available in const.
    // Original: `mem::take(self).split_last_mut()?`
    let Some((last, rem)) = mem::replace(self, &mut []).split_last_mut() else { return None };
    *self = rem;
    Some(last)
    }

  6. added
    T-libs
    and removed
    T-libs-api
    on Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-tracking-issueT-libs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions