What escaping is
Some characters have a job inside a string literal. The double quote marks where the literal starts and ends, and the backslash begins an escape sequence. When you want one of these characters to appear as ordinary text, or want to write a character you can't type directly, such as a line break, you write a backslash followed by a code. Rust reads the two characters together and produces a single character in the string.
The escapes you will use most often are:
| Escape | Produces |
|---|---|
\" | a double quote |
\\ | one backslash |
\n | a new line |
\t | a tab |
\' | a single quote (needed in a char literal, optional in a string) |
\u{e9} | the character with that Unicode code point, here é |
Quotes inside a string
Suppose you want to print I am John "the coder" Doe. Writing the quotes as they are does not work:
println!("I am John "the coder" Doe");
The literal ends at the second quote, so the text the coder is no longer inside a string, and the program does not compile. The compiler complains about an unknown prefix and an expected comma. Putting a backslash before each inner quote tells Rust that the quote is part of the text:
println!("I am John \"the coder\" Doe");
I am John "the coder" Doe
A single quote inside a double-quoted string needs no escape: "Let's go" is fine.
Printing a backslash
Because the backslash starts an escape, a lone backslash is an error in Rust. Rust accepts only a fixed set of escape sequences, and anything else is rejected when the program is compiled. This line does not build:
println!("C:\Desktop\Games");
The compiler reports unknown character escape: `D` and then the same for G. To print a real backslash, double it:
println!("C:\\Desktop\\Games");
C:\Desktop\Games
New lines
The same mechanism gives meaning to an ordinary letter. A backslash before n is a line break:
print!("Hello world!\nWhat a good day for coding!\n");
print!("Let's have a blast!\n");
Hello world!
What a good day for coding!
Let's have a blast!
println! adds a line break after its output by itself, while print! does not. That is why the examples above end their text with \n when they use print!.
Raw strings
When a text has many backslashes or quotes, Rust offers a way to skip escaping altogether: a raw string, written with an r before the opening quote. Nothing inside it is treated as an escape:
println!("{}", r"C:\Users\Andy\notion");
println!("{}", r#"He said "hi""#);
C:\Users\Andy\notion
He said "hi"
If the text itself contains a double quote, add one or more # characters on each side of the quotes, as in the second line. The lesson uses ordinary escapes, but raw strings are the tool to reach for when a path or a pattern is full of backslashes.
A note on {}
The text passed to println! is a format string, so it has one more special pair of characters: { and }. To print a literal brace, double it: println!("{{}} braces {}", 5) prints {} braces 5. Passing a message as an argument, as in println!("{}", message), avoids the problem, because the message is inserted as it is and its braces are not interpreted.
The lesson's exercise
The exercise has three prints, each using one of the ideas above.
- Quotes. The message
I'll study "Coding 101" on AlgoCademygoes in as the second argument ofprintln!("{}", ...). The inner double quotes are escaped as\". The apostrophe inI'llneeds nothing. - Two lines from one statement. A
\nbetween the two phrases splits them into separate lines. - A file path. Each backslash in
C:\Users\Andy\notionis doubled so that it prints as one backslash.
println!("{}", "I'll study \"Coding 101\" on AlgoCademy");
println!("I will learn this\nNo matter what");
println!("C:\\Users\\Andy\\notion");
Key takeaways
- A backslash followed by a code is an escape sequence; the pair becomes one character.
- Use
\"for a double quote inside a string and\\for a backslash. - An unknown escape such as
\Dis a compile error in Rust, not a silently dropped character. \nstarts a new line;println!already adds one at the end, andprint!does not.- A raw string,
r"...", turns escapes off.
