Text
String owns valid UTF-8. A string literal converts directly to String:
Dynamic bytes must be validated. from_utf8 copies valid input and reports the exact byte position of invalid
input. Use from_utf8_unchecked only when an earlier boundary has already established the UTF-8 invariant:
bytes() returns a borrowed view, byte_length() is O(1), and char_count() counts Unicode scalar values in
O(n). Byte search methods such as byte_index_of return byte offsets, not character or grapheme indices.
Use StringBuilder for repeated appends. into_string() consumes the builder and transfers its storage in O(1):
append_utf8 is recoverable and leaves the builder unchanged when validation fails. The unchecked append has the
same safety requirement as String.from_utf8_unchecked.
append_utf8_lossy(bytes) replaces invalid sequences with U+FFFD (�). Ordinary append(bytes)
requires valid UTF-8 and panics on invalid input.
String Interpolation
Section titled “String Interpolation”Use \(expression) inside a string to append a value. The underscore below is ordinary text:
Interpolation produces an owned String using StringBuilder.append overloads.
See Output for printing and custom Formattable values.