Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write a multi-line string in Rust? [duplicate]

Tags:

rust

People also ask

How do I print a string with multiple lines?

Use triple quotes to create a multiline string It is the simplest method to let a long string split into different lines. You will need to enclose it with a pair of Triple quotes, one at the start and second in the end. Anything inside the enclosing Triple quotes will become part of one multiline string.

Can double quote strings span multiple lines?

A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. Not only does this allow multiple lines, but it also turns off escaping.

What are multi line strings?

A multiline string in Python begins and ends with either three single quotes or three double quotes. Any quotes, tabs, or newlines in between the “triple quotes” are considered part of the string. Python's indentation rules for blocks do not apply to lines inside a multiline string.

How do you make a string a literal in Rust?

The Rust Reference defines a raw string literal as starting with the character U+0072 (r), followed by zero or more of the character U+0023 (#) and a U+0022 (double-quote) character.


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 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

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.


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.


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