Skip to content

Tracking Issue for const_vec_string_slice #129041

Closed

Description

@GrigorenkoPV

Feature gate: #![feature(const_vec_string_slice)]

This is a tracking issue for making a bunch of String and Vec methods const.

Public API

The following methods are now const:

impl String {
    pub const fn into_bytes(self) -> Vec<u8>;
    pub const fn as_str(&self) -> &str;
    pub const fn capacity(&self) -> usize;
    pub const fn as_bytes(&self) -> &[u8];
    pub const fn len(&self) -> usize;
    pub const fn is_empty(&self) -> bool;
}

impl Vec<T> {
    pub const fn capacity(&self) -> usize;
    pub const fn as_slice(&self) -> &[T];
    pub const fn as_ptr(&self) -> *const T;
    pub const fn len(&self) -> usize;
    pub const fn is_empty(&self) -> bool;
}

A few additional methods are under this feature gate but were not included in the FCP at #129041 (comment); they got FCP'd in #137319 (comment):

impl String {
    pub const fn as_mut_str(&mut self) -> &mut str;
    pub const unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8>;
}

impl Vec<T> {
    pub const fn as_mut_slice(&mut self) -> &mut [T];
    pub const fn as_mut_ptr(&mut self) -> *mut T;
}

Steps / History

Unresolved Questions

  • None yet.

@rustbot label A-str

Footnotes

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

Activity

  1. added
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    T-libs-api[DEPRECATED; DO NOT USE]
    on Aug 13, 2024
  2. Esper89 commented on Aug 13, 2024

    @Esper89

    This would be very useful for const fns that sometimes need to handle runtime data too, such as a const fn method that needs to use a Cow<'static, [T]> field.

    For example:

    struct Foo(Cow<'static, [u32]>);
    
    impl Foo {
        // Initialize with heap-allocated data at runtime.
        fn heap(data: Vec<u32>) -> Self {
            Foo(Cow::Owned(data))
        }
    
        // Initialize with static data at compile-time.
        const fn static(data: &'static [u32]) -> Self {
            Foo(Cow::Borrowed(data))
        }
    
        // Access the data, possibly at compile-time, possibly at runtime.
        const fn data(&self) -> &[u32] {
            match self.0 {
                Cow::Borrowed(slice) => slice,
                Cow::Owned(vec) => vec.as_slice(),
            }
        }
    }
  3. RalfJung commented on Nov 30, 2024

    @RalfJung
    Member

    @rust-lang/libs-api from a const-eval perspective, these functions all look totally harmless. One can't actually construct non-trivial Vec/String in const, but according to the examples above it could still be useful to have these as const fn. So, I propose we move ahead with stabilizing these.

  4. 17 remaining items

  5. Amanieu commented on Feb 25, 2025

    @Amanieu
    Member

    I started a new FCP on #137319.

  6. added a commit that references this issue on Mar 9, 2025
  7. added a commit that references this issue on Mar 9, 2025
  8. added a commit that references this issue on Mar 9, 2025
  9. added this to the 1.87.0 milestone on Mar 21, 2025
  10. added a commit that references this issue on Jun 4, 2025
  11. added a commit that references this issue on Jun 4, 2025
  12. added a commit that references this issue on Jun 17, 2025
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

    A-strArea: str and StringC-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-api[DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions