-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
String::from_utf16_lossy
signature differs from from_utf8_lossy
#32874
Comments
The conversion from utf-16 can never result in a &str, since the representation of the data changes from using u16 units to u8. |
Moreover you can trivially convert an owned datatype to Cow with let x: Cow<str> = String::from_utf16_lossy(&[]).into(); |
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! |
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. |
Opened a PR for this: #44572 |
… r=QuietMisdreavus Clarify return type of `String::from_utf16_lossy`. Fixes rust-lang#32874
String::from_utf8_lossy
returns aCow<'a, str>
whileString::from_utf16_lossy
returns simplyString
. It would be useful for their signature to both match.String::from_utf16_lossy
should returnCow<'a, str>
as well, though this is obviously a breaking, but easily solved, change.The text was updated successfully, but these errors were encountered: