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.
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!
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.
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.
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.
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";
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
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"
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With