Skip to content

str api should offer a method to convert a &u8 slice into an &str with same lifetime #5422

Closed

Description

@nikomatsakis

Since strs and vecs share the same representation, there should be a way to "cast" a &u8 slice into an &str with the same lifetime.

There are a few FIXMEs in tydecode where this could save a copy.

Activity

  1. sanxiyn commented on Mar 18, 2013

    @sanxiyn
    Contributor

    Isn't this an unsafe operation? Not all &u8 are valid &str.

  2. nikomatsakis commented on Mar 18, 2013

    @nikomatsakis
    ContributorAuthor

    I presumed there'd be an assertion as well as an unsafe variant, as we have for str::from_bytes

  3. Kimundi commented on May 24, 2013

    @Kimundi
    Contributor

    @nikomatsakis Could you confirm the PR request did add what was wanted in this issue? I remembered that there where some difficulties to make it work because a &str slice has to cover one byte more than a &[].

  4. nikomatsakis commented on May 24, 2013

    @nikomatsakis
    ContributorAuthor

    Looks good, thanks.

  5. added 2 commits that reference this issue on May 2, 2020
  6. added a commit that references this issue on Aug 21, 2026
  7. added a commit that references this issue on Oct 19, 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-enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions