Skip to content

Tracking Issue for str_from_raw_parts #119206

Open

Description

@Sky9x

Feature gate: #![feature(str_from_raw_parts)]

This is a tracking issue for ACP#167, which adds core::str::from_raw_parts[_mut].

Public API

// core::str

pub const unsafe fn from_raw_parts<'a>(ptr: *const u8, len: usize) -> &'a str

pub const unsafe fn from_raw_parts_mut<'a>(ptr: *mut u8, len: usize) -> &'a mut str

Steps / History

Unresolved Questions

  • None yet.

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 Dec 22, 2023
  2. added 2 commits that reference this issue on Jan 26, 2024
  3. added a commit that references this issue on Jan 26, 2024
  4. added 2 commits that reference this issue on Apr 18, 2024
  5. added a commit that references this issue on Apr 18, 2024
  6. robertbastian commented on Jun 18, 2024

    @robertbastian
    Contributor

    I'm not a fan of this. This saves a negligible amount of code but bunches up a whole lot of safety considerations. slice::from_raw_parts has language-level memory safety invariants, and str::from_utf8_unchecked has a single library-level safety invariant (that the slice contains valid UTF-8). In code, these should be justified separately. The example in rust-lang/libs-team#167 was

    unsafe {
        let bytes = slice::from_raw_parts(ptr, len);
        str::from_utf8_unchecked(bytes)
    }

    but it should have really been

    // SAFETY: <explain why ptr and len make a valid slice>
    let bytes = unsafe { slice::from_raw_parts(ptr, len) };
    // SAFETY: <explain why this slice is UTF-8>
    unsafe { str::from_utf8_unchecked(bytes) }

    I don't see the value of combining these to

    // SAFETY: <explain why ptr and len make a valid slice and why this slice is UTF-8>
    unsafe { str::from_raw_parts(ptr, len) }

    The proposed documentation also says

    The pointed-to bytes must be valid UTF-8. If this might not be the case, use str::from_utf8(slice::from_raw_parts(ptr, len)), which will return an Err if the data isn’t valid UTF-8.

    which kind of admits that there are two safety considerations that should be handled by two function calls. In the end, docs could just recommend str::from_utf8_unchecked(slice::from_raw_parts(ptr, len)).

  7. Sky9x commented on Jun 19, 2024

    @Sky9x
    ContributorAuthor

    This API intends to be the borrowed equivalent the already-stable String::from_raw_parts, and fill in the two missing methods in the table below, bringing the string API more in line with the slice API.

    String Slice
    & ❌ str::from_raw_parts ✅ slice::from_raw_parts
    &mut ❌ str::from_raw_parts_mut ✅ slice::from_raw_parts_mut
    Owned ✅ String::from_raw_parts ✅ Vec::from_raw_parts

    The documentation could be improved to properly express the safety concerns.

    Also note that the ACP was accepted because of the existence of String::from_raw_parts, which by the same argument could be written as String::from_utf8_unchecked(Vec::from_raw_parts(ptr, len, cap)).

    If you really don't want these functions to appear in your codebase, consider the clippy::disallowed_methods lint:

    # clippy.toml
    disallowed-methods = [
        "core::str::from_raw_parts",
        "core::str::from_raw_parts_mut",
        "alloc::string::String::from_raw_parts",
        # ...
    ]
  8. robertbastian commented on Jun 20, 2024

    @robertbastian
    Contributor

    It looks like String::from_raw_parts's safety documentation was at some point copy-pasted from Vec::from_raw_parts's, but it has since diverged and is currently much less detailed. It for example misses the invariant "The allocated size in bytes must be no larger than isize::MAX". This is exactly the issue I'm trying to point out, the documentation basically has to say

    Safety

    • Everything in Vec::<u8>::from_raw_parts's safety requirements
    • Everything in String::from_utf8_unchecked's safety requirements

    at which point why not just make those two calls?

    String::from_raw_parts is one method with this issue, does this justify adding two more?

  9. scottmcm commented on Oct 1, 2024

    @scottmcm
    Member

    I'll copy my comment from the ACP here, as something potentially still worth discussing at stabilization time:

    I'm not convinced that one more function call is "a fairly boilerplate filled task". That's especially true for unsafe code where the hard part is demonstrating that the safety conditions have been met.

    Because of that, I think two calls is actually better when the two safety conditions are very different, like they are here.

    (This is the same sentiment as #119206 (comment) above.)

  10. PureWhiteWu commented on Dec 9, 2024

    @PureWhiteWu

    Hi, is there any progress on this feature? Seems there's no outstanding questions.

  11. robertbastian commented on Feb 9, 2025

    @robertbastian
    Contributor

    There is an open discussion on whether this should be done.

    If consistency is the driving argument, we should look at this complete table:

      String Slice
    & ❌ str::from_raw_parts ✅ slice::from_raw_parts
    &mut ❌ str::from_raw_parts_mut
    ❌ str::into_raw_parts
    ✅ slice::from_raw_parts_mut
    ❌ slice::into_raw_parts
    Owned ✅ String::from_raw_parts
    ❌ String::into_raw_parts
    ✅ Vec::from_raw_parts
    ❌ Vec::into_raw_parts
  12. added a commit that references this issue on Feb 24, 2025
  13. added a commit that references this issue on Feb 24, 2025
  14. added a commit that references this issue on Feb 25, 2025
  15. added 2 commits that reference this issue on Mar 11, 2025
  16. abgros commented on Jul 8, 2025

    @abgros

    I think this should be moved to str::from_raw_parts, possibly keeping std::str::from_raw_parts and core::str::from_raw_parts as aliases, for consistency with str::from_utf8 (#131114).

  17. added
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    and removed
    T-libs-api[DEPRECATED; DO NOT USE]
    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-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions