Escape sequencesLiteral
Explanation
Inside a (non-raw) string, character, or byte literal, a backslash introduces an escape sequence:
- Quote escapes:
\',\" - Common ASCII escapes:
\n(newline),\r(carriage return),\t(tab),\\(backslash),\0(null) - Numeric byte escape:
\xNN— two hex digits; in achar/strcontext restricted to\x00–\x7F(7-bit —\xdenotes a raw byte, and values≥ 0x80would be ambiguous between a byte and a code point, so Rust requires\u{...}there instead), but a full 8-bit\x00–\xFFin ab'...'/b"..."byte context - Unicode escape:
\u{7FFF}— up to six hex digits in braces, representing any Unicode scalar value; legal inchar/strandc"..."contexts, but not in byte literals - Line-continuation escape: a backslash immediately followed by a newline strips the newline and any leading whitespace on the next line, letting a long string literal be wrapped across source lines without embedding the line break itself
None of these are processed inside a raw string/byte-string literal — see raw string literal.
Usage examples
Embedding escape sequences in a string
let s = "tab\tnewline\n";
// ^^ ^^ escape sequences, processed at compile time
A \xNN byte escape inside a char/str context is
limited to \x00–\x7F (7-bit) — the full 8-bit range \x00–\xFF is
only legal inside a byte (b'...'/b"...") literal.
Working with text
Embedding \n/\t directly inside one string literal keeps a short
multi-line template as a single readable value.
fn format_receipt(item: &str, qty: u32, price_cents: u32) -> String {
format!("{item}\n qty: {qty}\n price: ${price_cents}\n") // <- `\n` escape sequences: line breaks in one literal
}
print!("{}", format_receipt("widget", 3, 499));
Embedding \n/\t inside one literal keeps the
template readable as "the shape of the output" in one place, rather than
splicing separate pieces together — the escapes are resolved once, at
compile time, into the exact bytes the string holds.
Serializing and deserializing
Rust's compile-time escape rules and JSON's runtime string-escaping rules look similar but aren't identical — worth knowing before hand-building any JSON text.
// Rust's `\u{...}` escape (braced, variable-length hex) is *literal* syntax,
// resolved by rustc at compile time -- it is not the same thing as JSON's escape.
let heart: char = '\u{2764}'; // <- Rust escape sequence: braced hex, resolved when this file compiles
// JSON escapes the same code point as `❤` -- exactly four hex digits, no braces --
// and that escaping is *data*, checked by a JSON parser at runtime, not by rustc.
// The raw string below holds that JSON text verbatim; rustc never interprets the `❤`.
let json_fragment = r#"{"symbol": "❤"}"#; // <- `❤` here is JSON syntax, untouched by Rust
Rust's escape rules and JSON's string-escape rules
differ in small but real ways (braced vs unbraced \u, no \x byte
escape in JSON at all) — the practical consequence is to let a JSON
library like serde_json own all JSON escaping rather
than hand-building JSON text with Rust string literals.