How do I write a multi-line string in Rust?
If I'm reading the language reference correctly, then it looks like that should work. The language ref states that \n
etc. are supported (as common escapes, for inserting line breaks into your string), along with "additional escapes" including LF, CR, and HT.
Another way to do this is to use a raw string literal:
Raw string literals do not process any escapes. They start with the character
U+0072
(r
), followed by zero or more of the characterU+0023
(#
) and aU+0022
(double-quote) character. The raw string body can contain any sequence of Unicode characters and is terminated only by anotherU+0022
(double-quote) character, followed by the same number ofU+0023
(#) characters that preceded the openingU+0022
(double-quote) character.All Unicode characters contained in the raw string body represent themselves, the characters
U+0022
(double-quote) (except when followed by at least as manyU+0023
(#
) characters as were used to start the raw string literal) orU+005C
(\
) do not have any special meaning.Examples for string literals:
"foo"; r"foo"; // foo "\"foo\""; r#""foo""#; // "foo" "foo #\"# bar"; r##"foo #"# bar"##; // foo #"# bar "\x52"; "R"; r"R"; // R "\\x52"; r"\x52"; // \x52