← All notes

Rust String and &str: two UTF-8 string types

Answer: The core language string type is the string slice str, usually seen as &str. The standard library also provides String, which is growable, mutable, owned, and UTF-8 encoded.

Two types, both UTF-8

Rust has only one string type in the core language: the string slice str, usually written in borrowed form as &str.

String literals live in the program's binary, so they are string slices.

String comes from the standard library; it is not coded into the core language.

String and string slices are both UTF-8 encoded. Talk of strings in Rust can mean either type.

How String is stored

The two most used string types, according to Rust by Example, are String and &str.

String holds a vector of bytes and is guaranteed to always be a valid UTF-8 sequence.

It is heap allocated and growable, and it is not null terminated.

A &str slice always points to valid UTF-8 and can view into a String, much as &[T] views a Vec<T>.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Rust Programming Language, Storing UTF-8 Encoded Text with Strings; Rust by Example, Strings. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/rust-by-example/master/LICENSE-MIT). Copyright the Rust Project Developers. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Rust Programming Language, Storing UTF-8 Encoded Text with Strings; Rust by Example, Strings. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/rust-by-example/master/LICENSE-MIT). Copyright the Rust Project Developers.

Cite this

Permanent ID: iil:note:ry2p5af5

iinventedlight. 2026. "Rust String and &str: two UTF-8 string types." iinventedlight Library, iil:note:ry2p5af5, version of 2026-11-23. https://iinventedlight.com/library/notes/rust-string-vs-str/. License: CC-BY-4.0.

@misc{iil_note_ry2p5af5,
  author = {{iinventedlight}},
  title = {{Rust String and \&str: two UTF-8 string types}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-23},
  url = {https://iinventedlight.com/library/notes/rust-string-vs-str/},
  note = {Permanent ID iil:note:ry2p5af5. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

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