← All notes

Rust Strings and UTF-8 text

Answer: Rust strings use UTF-8 encoding. The common types are owned String and borrowed &str.

String and str

A String is stored as a vector of bytes guaranteed to be valid UTF-8. &str is a slice view onto valid UTF-8 bytes.

Indexing

Indexing a String with [] is discouraged: the return type of that operation is unclear. Use .chars() or .bytes() rather than ambiguous string indexing.

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources and notices: The Rust Programming Language book (rust-lang/book): Copyright (c) 2010 The Rust Project Developers; MIT OR Apache-2.0, used under MIT (https://github.com/rust-lang/book/blob/main/LICENSE-MIT). Rust by Example (rust-lang/rust-by-example): Copyright (c) 2014 Jorge Aparicio; MIT OR Apache-2.0, used under MIT (https://github.com/rust-lang/rust-by-example/blob/master/LICENSE-MIT). Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Rust Project (The Rust Programming Language book), Storing UTF-8 Encoded Text with Strings, MIT OR Apache-2.0; Rust by Example (rust-lang/rust-by-example), Strings - Rust By Example, MIT OR Apache-2.0. Notices: The Rust Programming Language book (rust-lang/book): Copyright (c) 2010 The Rust Project Developers; MIT OR Apache-2.0, used under MIT (https://github.com/rust-lang/book/blob/main/LICENSE-MIT). Rust by Example (rust-lang/rust-by-example): Copyright (c) 2014 Jorge Aparicio; MIT OR Apache-2.0, used under MIT (https://github.com/rust-lang/rust-by-example/blob/master/LICENSE-MIT).

Cite this

Permanent ID: iil:note:49vdhknr

iinventedlight. 2026. "Rust Strings and UTF-8 text." iinventedlight Library, iil:note:49vdhknr, version of 2026-11-09. https://iinventedlight.com/library/notes/rust-strings-utf8/. License: CC-BY-4.0.

@misc{iil_note_49vdhknr,
  author = {{iinventedlight}},
  title = {{Rust Strings and UTF-8 text}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-09},
  url = {https://iinventedlight.com/library/notes/rust-strings-utf8/},
  note = {Permanent ID iil:note:49vdhknr. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-strings-utf8.md.