What is the syntax for a multiline string literal?

后端 未结 5 1148
隐瞒了意图╮
隐瞒了意图╮ 2020-12-12 14:26

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.

5条回答
  •  甜味超标
    2020-12-12 14:44

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

提交回复
热议问题