Is it possible to write something like:
fn main() {
let my_string: &str = \"Testing for new lines \\
might work like this?\";
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
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.
If you'd like to avoid having newline characters and extra spaces, you can use the concat! macro. It concatenates string literals at compile time.
let my_string = concat!(
"Testing for new lines ",
"might work like this?",
);
assert_eq!(my_string, "Testing for new lines might work like this?");
The accepted answer with the backslash also removes the extra spaces.
Every line is a multiline in Rust.
But if you have indents in your text like:
fn my_func() {
const MY_CONST: &str = "\
Hi!
This is a multiline text!
";
}
But you will get unnecessary spaces. To remove them you can use indoc!
macros from indoc
crate to remove all indents: https://github.com/dtolnay/indoc
There are two ways of writing multi-line strings in Rust that have different results. You should choose between them with care depending on what you are trying to accomplish.
Method 1: Dangling whitespace
If a string starting with "
contains a literal line break, the Rust compiler will "gobble up" all whitespace between the last non-whitespace character of the line and the first non-whitespace character of the next line, and replace them with a single .
Example:
fn test() {
println!("{}", "hello
world");
}
No matter how many literal (blank space) characters (zero or a hundred) appear after
hello
, the output of the above will always be hello world
.
Method 2: Backslash line break
This is the exact opposite. In this mode, all the whitespace before a literal \
on the first line is preserved, and all the subsequent whitespace on the next line is also preserved.
Example:
fn test() {
println!("{}", "hello \
world");
}
In this example, the output is hello world
.
Additionally, as mentioned in another answer, Rust has "raw literal" strings, but they do not enter into this discussion as in Rust (unlike some other languages that need to resort to raw strings for this) supports literal line breaks in quoted content without restrictions, as we can see above.