【问题标题】:C# like verbatim string in Rust? [duplicate]C# 像 Rust 中的逐字字符串? [复制]
【发布时间】: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


    【解决方案1】:

    我猜你在找raw string literals

     let raw_string_literal = r#" line1 \n still line 1"#;
     println!("{}", raw_string_literal);
    

    【讨论】:

      【解决方案2】:

      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 字符。

      【讨论】:

      • 太糟糕了,我想在某些时候添加一些新行。但足够公平地使用 Rust 规则。感谢您的回答!
      • 要明确:看起来很奇怪,但您可以在原始文字中包含换行符。如果您阅读我提供的链接,提供了更多解释。
      猜你喜欢
      • 1970-01-01
      • 2021-07-13
      • 1970-01-01
      • 2017-04-10
      • 1970-01-01
      • 2012-03-14
      • 1970-01-01
      • 2019-03-18
      • 2014-11-22
      相关资源
      最近更新 更多