我正在寻找Rust中的逐字字符串(类似于C#中使用@"This is a string and here ""I am between quotes without making a fuss"""
)。
是否有类似的功能?
我正在寻找Rust中的逐字字符串(类似于C#中使用@"This is a string and here ""I am between quotes without making a fuss"""
)。
是否有类似的功能?
这个 竟然很难找到。
在 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 字符。
let raw_string_literal = r#" line1 \n still line 1"#;
println!("{}", raw_string_literal);