What Is a String?
We’ll first define what we mean by the term string. Rust has only one string type in the core language, which is the string slice
str
that is usually seen in its borrowed form&str
. In Chapter 4, we talked about string slices, which are references to some UTF-8 encoded string data stored elsewhere. String literals, for example, are stored in the program’s binary and are therefore string slices.
TheString
type, which is provided by Rust’s standard library rather than coded into the core language, is a growable, mutable, owned, UTF-8 encoded string type. When Rustaceans refer to “strings” in Rust, they might be referring to either theString
or the string slice&str
types, not just one of those types. Although this section is largely aboutString
, both types are used heavily in Rust’s standard library, and bothString
and string slices are UTF-8 encoded.