在一行原始字符串文字中包含换行符?

问题描述 投票:0回答:1

为了 Elasticsearch 构建用于批量发布的字符串,我需要符合特定的换行格式。像这样的原始字符串文字可以完成这项工作:

    let json = format!(r#"{{"index":{{"_id":"{}"}}}}
{}
"#, *num, self.ldoc_texts[sequence_type]);
    self.bulk_post_str.push_str(&json);

上面的第 2 行和第 3 行故意没有缩进,以免在行首引入虚假空格。

有什么办法可以把它写在同一行吗?

let json = format!(r#"{{"index":{{"_id":"{}"}}}}\n{}\n"#, *num, self.ldoc_texts[sequence_type]);
self.bulk_post_str.push_str(&json);

上面的方法不起作用:文字序列“ 在结果字符串中找到“,而不是换行符。

string rust newline rawstring
1个回答
0
投票

您可以使用

concat!
将原始文字和常规文字粘在一起。

print!(concat!(r#"This is a "raw" literal"#, "\n", r#"continued after "#, "\n"))
© www.soinside.com 2019 - 2024. All rights reserved.