Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the syntax for a multiline string literal?

I'm having a hard time figuring out how string syntax works in Rust. Specifically, I'm trying to figure out how to make a multiple line string.

like image 288
Dumbapples Avatar asked Oct 15 '22 15:10

Dumbapples


People also ask

How do you define multi-line string literals?

We can use string literal concatenation. Multiple string literals in a row are joined together: char* my_str = "Here is the first line." "Here is the second line."; But wait!

What is the syntax of string literal?

A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string. You must always prefix wide-string literals with the letter L.

How do you write multiline strings?

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.

How do you write a multiline string in C++?

Using Backslash If we place a backslash at the end of each line, the compiler removes the new-line and preceding backslash character. This forms the multiline string.


3 Answers

All string literals can be broken across several lines; for example:

let string = "line one
line two";

is a two line string, the same as "line one\nline two" (of course one can use the \n newline escape directly too). If you wish to just break a string across multiple lines for formatting reasons you can escape the newline and leading whitespace with a \; for example:

let string = "one line \
    written over \
    several";

is the same as "one line written over several".

If you want linebreaks in the string you can add them before the \:

let string = "multiple\n\
              lines\n\
              with\n\
              indentation";

It's the same as "multiple\nlines\nwith\nindentation";

like image 361
huon Avatar answered Oct 20 '22 22:10

huon


In case you want to do something a bit longer, which may or may not include quotes, backslashes, etc., use the raw string literal notation:

let shader = r#"
    #version 330

    in vec4 v_color;
    out vec4 color;

    void main() {
        color = v_color;
    };
"#;

If you have sequences of double quotes and hash symbols within your string, you can denote an arbitrary number of hashes as a delimiter:

let crazy_raw_string = r###"
    My fingers #"
    can#"#t stop "#"" hitting
    hash##"#
"###;

Outputs:


    #version 330

    in vec4 v_color;
    out vec4 color;

    void main() {
        color = v_color;
    };

Playground link

like image 192
c0g Avatar answered Oct 20 '22 23:10

c0g


Huon's answer is correct but if the indentation bothers you, consider using Indoc which is a procedural macro for indented multi-line strings. It stands for "indented document." It provides a macro called indoc!() that takes a multiline string literal and un-indents it so the leftmost non-space character is in the first column.

let s = indoc! {"
    line one
    line two
"};

The result is "line one\nline two\n".

Whitespace is preserved relative to the leftmost non-space character in the document, so the following has line two indented 3 spaces relative to line one:

let s = indoc! {"
    line one
       line two
"};

The result is "line one\n line two\n".

like image 94
dtolnay Avatar answered Oct 20 '22 22:10

dtolnay