多行字符串文字的语法是什么? [英] What is the syntax for a multiline string literal?

查看:46
本文介绍了多行字符串文字的语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难弄清楚字符串语法在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆