liveBook · Manning

String

As explained in Rust in Action: Systems programming concepts and techniques

String is Rust's owned, growable UTF-8 string type whose character data is stored in heap-allocated memory.

`String` is Rust’s growable text type. Its character data lives in heap-allocated memory, and Rust strings use UTF-8. That encoding lets a greeting include text such as “Grüß Gott!” or “ハロー・ワールド”. section 2.9 section 6.3.1 section 1.3.2

`String` and `&str` both represent text, but they are different types. A `String` can have text appended to it; a `str` cannot expand or shrink. You will often encounter `str` as `&str`, called a string slice. If the distinction causes type errors while you are learning, converting data to `String` is often the simpler path. section 2.9

A search starts with string literals

A small search program stores a term and a passage as string literals, walks through the passage with `.lines()`, and tests each line with `.contains(search_term)`. These methods show what text values can do, though the literals are not themselves growable `String` values. section 2.9

The type distinction also matters at function boundaries: a function accepting only `String` cannot directly take a string literal. For read-only access, a parameter constrained by `AsRef<str>` can accept either `String` or `&str`. This avoids forcing callers to use just one representation. section 6.3.1

fn is_strong<T: AsRef<str>>(password: T) -> bool {     #1
    password.as_ref().len() > 5
}

The password check calls `.as_ref()` before `.len()`, allowing the function to read text through a shared `str` view. section 6.3.1

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Explains growth, common operations, and heap allocation, then shows a generic read-only parameter without detailing String’s internal layout.
Examples
Uses multilingual greetings, a line-by-line text search, and a password-length function that accepts different text types.
What it assumes
Introduces text through a practical search after basic numeric work and anticipates beginner confusion about Rust’s string types.
What you can do after
Apply it via Rust’s String and AsRef<str> to handle growable text and write read-only functions accepting String or &str.
How it connects
Connects String to UTF-8, string slices, heap allocation, generics, and AsRef<str>.

Also explained in

Compare how 24 books explain string →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.