Skip to content

Rename &str::as_bytes() to &str::as_utf8() #14131

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

Closed
SimonSapin opened this issue May 12, 2014 · 9 comments
Closed

Rename &str::as_bytes() to &str::as_utf8() #14131

SimonSapin opened this issue May 12, 2014 · 9 comments

Comments

@SimonSapin
Copy link
Contributor

This seemed small enough to not be worth an RFC. Please correct me if it’s not.

Similar to how the str::from_bytes function was renamed to str::from_utf8, the &str::as_bytes method should be renamed to something that indicate UTF-8, possibly &str::as_utf8 or &str::as_utf8_bytes.

The &str::bytes and &str::bytes_rev iterators, the ~str::into_bytes method, and the StrBuf equivalent to all of the above should also be renamed accordingly.

@SimonSapin
Copy link
Contributor Author

The iterator yield one byte at a time, so it probably makes sense for them to keep "bytes" in the name: &str::utf8_bytes and &str::utf8_bytes_rev. In other methods I think it’s redundant: as_utf8 and into_utf8 are enough.

@lilyball
Copy link
Contributor

.as_utf8() suggests it might do some conversion. .as_bytes() tells me it's returning a &[u8] that represents the string.

@SimonSapin
Copy link
Contributor Author

@kballard I think that as in the name implies something cheap, or even O(1). A conversion method would be to_utf8.

@aturon
Copy link
Member

aturon commented May 28, 2014

@SimonSapin Yes, small library changes like this don't need to go through the RFC process. +1 for me on the suggested renaming; I think the guidelines are pretty clear about as being cheap.

@liigo
Copy link
Contributor

liigo commented May 29, 2014

I don't like this rename, &str is obviously utf-8 encoded, don't need
repeat this any more.

@huonw
Copy link
Member

huonw commented May 29, 2014

+1 for renaming.

(Outside chance for the iterator: utf8_codeunits, however I'm not a fan of it.)

@lilyball
Copy link
Contributor

@liigo has a good point. &str is, by definition, UTF-8 encoded. as_utf8() would give the impression that this is not true, because it suggests that it's converting from something that is not, in fact, "utf8".

@SimonSapin
Copy link
Contributor Author

@aturon Does this still sound like something you want?

@aturon
Copy link
Member

aturon commented Nov 19, 2014

@SimonSapin Sorry for the slow response. I'm going to mark this as closed, since String stabilization has already landed and made a decision about this issue.

(Personally, I'm happy either way, but we needed to make a decision and I think the stabilization gives a reasonable rationale.)

@aturon aturon closed this as completed Nov 19, 2014
lnicola pushed a commit to lnicola/rust that referenced this issue Feb 13, 2023
fix: don't include `r#` prefix in filesystem changes

Fixes rust-lang#14131

In addition to fix for rust-lang#14131, this PR adds raw ident validity checks in rename functionality that we've been missing.
flip1995 pushed a commit to flip1995/rust that referenced this issue Feb 6, 2025
A `ref` pattern applied to an argument is not ignored. It creates a
reference as expected, but still requires the function to take ownership
of the argument given to it.

Fix rust-lang#14131

changelog: [`toplevel_ref_arg`]: use a clearer lint message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants