【发布时间】:2019-05-05 21:38:50
【问题描述】:
我正在寻找 Rust 中的逐字字符串(例如在 C# 中使用 @"This is a string and here ""I am between quotes without making a fuss""")。
有类似的吗?
【问题讨论】:
标签: rust verbatim-string
我正在寻找 Rust 中的逐字字符串(例如在 C# 中使用 @"This is a string and here ""I am between quotes without making a fuss""")。
有类似的吗?
【问题讨论】:
标签: rust verbatim-string
我猜你在找raw string literals?
let raw_string_literal = r#" line1 \n still line 1"#;
println!("{}", raw_string_literal);
【讨论】:
This 出奇地难找。
在 rust 中,原始字符串文字被 r"" 包围,如果需要使用引号,请添加 #。
对于你的例子,
r#"This is a string and here "I am between quotes without making a fuss""#
应该工作。 (双引号会在字符串中产生双引号。)
如果您需要带有# 符号的东西,您可以执行类似的操作
r###"This string can have ## in as many places as I like ##, but never three in a row ##"###
但是,在 rust 原始字符串中,不允许转义。例如,您不能使用\n。但是,您可以包含任何所需的 UTF-8 字符。
【讨论】: