67

Is it possible to write something like:

fn main() {
    let my_string: &str = "Testing for new lines \
                           might work like this?";
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
sdasdadas
  • 23,917
  • 20
  • 63
  • 148

5 Answers5

51

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.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • 18
    Lee is correct. Backslash can be used to extend a string constant over multiple lines. Leading whitespace is omitted from the second line. – Niko Matsakis Mar 07 '13 at 14:35
  • Thanks - my original code had a different error that led me to believe the '\' wouldn't work. – sdasdadas Mar 08 '13 at 18:21
36

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 character U+0023 (#) and a U+0022 (double-quote) character. The raw string body can contain any sequence of Unicode characters and is terminated only by another U+0022 (double-quote) character, followed by the same number of U+0023 (#) characters that preceded the opening U+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 many U+0023 (#) characters as were used to start the raw string literal) or U+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
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Moebius
  • 6,242
  • 7
  • 42
  • 54
22

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.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
porkbrain
  • 720
  • 5
  • 16
6

Every string is a multiline string in Rust.

But if you have indents in your text like:

fn my_func() {
    const MY_CONST: &str = "\
    Hi!
    This is a multiline text!
    ";
}

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

Esteis
  • 4,669
  • 2
  • 29
  • 45
DenisKolodin
  • 13,501
  • 3
  • 62
  • 65
-2

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.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125
  • 10
    This is not correct. The Rust copiler will remove the spaces from the second line when the first line is terminated with a backslash. A line break without a backslash will be kept as a line break in the string. – Yves Dorfsman Aug 12 '19 at 14:06