Skip to content

Help people find String::as_bytes() for UTF-8 #39688

Closed
@Philipp91

Description

@Philipp91

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().

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions