Previous discussion:
#14131
#17438
Still a minor thing, so probably not worth an RFC.
as_bytes() is a fine name, but the function is not easy to discover. I have video evidence of Rust beginners who know and use from_utf8(), but can't find the inverse, which is as_bytes(). The documentation provides no links between the two.
Possible mitigations (could even apply multiple of these):
- Offer
.as_utf8() or .to_utf8(), and .into_utf8() and mention in the documentation that it's simply an alias.
- Put "UTF-8" or "utf8" in the documentation of
.as_bytes() and friends, so it can at least be found through search engines. Currently, queries for "rust string to utf8" or "rust utf8 bytes from string" only take you to the from_utf8() function, which works the wrong way round.
- Reference
.as_bytes() and .from_utf8() from each other and mention that they are (roughly) inverses (modulo error handling, modulo ownership stuff).
Currently, http://stackoverflow.com/questions/27195506/convert-an-ascii-string-literal-to-u8-in-rust#comment42877192_27195762 might be the hint that helps most people discover as_bytes().
Previous discussion:
#14131
#17438
Still a minor thing, so probably not worth an RFC.
as_bytes()is a fine name, but the function is not easy to discover. I have video evidence of Rust beginners who know and usefrom_utf8(), but can't find the inverse, which isas_bytes(). The documentation provides no links between the two.Possible mitigations (could even apply multiple of these):
.as_utf8()or.to_utf8(), and.into_utf8()and mention in the documentation that it's simply an alias..as_bytes()and friends, so it can at least be found through search engines. Currently, queries for "rust string to utf8" or "rust utf8 bytes from string" only take you to thefrom_utf8()function, which works the wrong way round..as_bytes()and.from_utf8()from each other and mention that they are (roughly) inverses (modulo error handling, modulo ownership stuff).Currently, http://stackoverflow.com/questions/27195506/convert-an-ascii-string-literal-to-u8-in-rust#comment42877192_27195762 might be the hint that helps most people discover
as_bytes().