Skip to content

String::from_utf16_lossy signature differs from from_utf8_lossy #32874

Closed

Description

@VoidStarKat

String::from_utf8_lossy returns a Cow<'a, str> while String::from_utf16_lossy returns simply String. It would be useful for their signature to both match. String::from_utf16_lossy should return Cow<'a, str> as well, though this is obviously a breaking, but easily solved, change.

Activity

  1. bluss commented on Apr 11, 2016

    @bluss
    Contributor

    The conversion from utf-16 can never result in a &str, since the representation of the data changes from using u16 units to u8.

  2. nagisa commented on Apr 11, 2016

    @nagisa
    Member

    Moreover you can trivially convert an owned datatype to Cow with into:

    let x: Cow<str> = String::from_utf16_lossy(&[]).into();
  3. alexcrichton commented on Apr 11, 2016

    @alexcrichton
    Member

    Yeah unfortunately these signatures can't change (as they're stable), but they're both intended to return "string-like types" in whichever way is most suitable, but perhaps the docs can be bolstered up to emphasize that!

  4. added
    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools
    and removed
    T-libs-api[DEPRECATED; DO NOT USE]
    on Jun 16, 2017
  5. Mark-Simulacrum commented on Jun 16, 2017

    @Mark-Simulacrum
    Member

    Relabeling as a docs issue since we can't change these APIs. However, I'm not entirely sure what needs to be changed here. Specifically, we should mention that conversion from UTF-16 can never be done without allocating, so we don't return a Cow. I'm personally not sure this is a good idea, but I can see the benefits, since it explains the API inconsistency.

  6. self-assigned this
    on Sep 13, 2017
  7. frewsxcv commented on Sep 14, 2017

    @frewsxcv
    Contributor

    Opened a PR for this: #44572

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-enhancementCategory: An issue proposing an enhancement or a PR with one.P-lowLow priority

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions