string 多行字符串文字的语法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29483365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:22:20  来源:igfitidea点击:

What is the syntax for a multiline string literal?

stringrust

提问by Dumbapples

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.

我很难弄清楚字符串语法在 Rust 中是如何工作的。具体来说,我想弄清楚如何制作多行字符串。

回答by huon

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 \nnewline 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:

是一个两行的字符串,与"line one\nline two"(当然也可以\n直接使用换行符转义)相同。如果出于格式化原因,您只想将一个字符串分成多行,您可以使用 ; 转义换行符和前导空格\。例如:

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

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

与 相同"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";

它与 "multiple\nlines\nwith\nindentation";

回答by c0g

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##"#
"###;

回答by dtolnay

Huon's answeris correct but if the indentation bothers you, consider using Indocwhich 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.

Huon 的回答是正确的,但如果缩进让您感到困扰,请考虑使用Indoc,它是用于缩进多行字符串的程序宏。它代表“缩进文档”。它提供了一个名为的宏indoc!(),它接受一个多行字符串文字并取消缩进,因此最左边的非空格字符位于第一列中。

let s = indoc!("
          line one
          line two");

The result is "line one\nline two".

结果是"line one\nline two"

There are a few equivalent ways to format the same thing if you prefer, so choose one you like. The following both result in the same string as above. The content can be indented as much as you like – it does not have to be a specific number of spaces.

如果您愿意,有几种等效的方法可以格式化相同的内容,因此请选择您喜欢的一种。以下两者都产生与上述相同的字符串。内容可以任意缩进——它不必是特定数量的空格。

let s = indoc!(
         "line one
          line two");

let s = indoc!("line one
                line two");

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:

相对于文档中最左边的非空格字符保留了空白,因此以下第二行相对于第一行缩进了 3 个空格:

let s = indoc!("
          line one
             line two");

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

结果是"line one\n line two"

回答by Igor

In case you want to indent multiline text in your code:

如果您想在代码中缩进多行文本:

let s = "first line\n\
    second line\n\
    third line";

println!("Multiline text goes next:\n{}", s);

The result will be the following:

结果如下:

Multiline text goes next:
first line
second line
third line