多行字符串文字的语法是什么? [英] What is the syntax for a multiline string literal?
问题描述
我很难弄清楚字符串语法在 Rust 中是如何工作的.具体来说,我想弄清楚如何制作多行字符串.
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.
推荐答案
所有字符串文字都可以分成几行;例如:
All string literals can be broken across several lines; for example:
let string = "line one
line two";
是两行字符串,与"line one\nline two"
相同(当然也可以直接使用\n
换行符转义).如果出于格式原因,您只想将一个字符串分成多行,您可以使用 \
转义换行符和前导空格;例如:
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";
与一行写在几行"
相同.
如果你想在字符串中换行,你可以在 \
之前添加它们:
If you want linebreaks in the string you can add them before the \
:
let string = "multiple\n\
lines\n\
with\n\
indentation";
同"multiple\nlines\nwith\nindentation";
这篇关于多行字符串文字的语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!