Rust 中的 r#""# 运算符是什么? [英] What is the r#""# operator in Rust?

查看:206
本文介绍了Rust 中的 r#""# 运算符是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Rust 中看到了运算符 r#"" 但我找不到它的作用.它在创建 JSON 时派上用场:

let var1 = "test1";让 json = r#"{"type": "type1", "type2": var1}"#;println!("{}", json)//=>{"type2": "type1", "type2": var1}

r#"" 运算符的名称是什么?如何让 var1 求值?

解决方案

我找不到它的作用

它与字符串文字和原始字符串有关.我认为 这部分文档,在发布的代码块中,您可以看到它的作用:

<块引用>

"foo";r"foo";//富"\"foo\"";r#""foo""#;//"富""foo #\"# bar";r##"foo #"# bar"##;//foo #"# bar"\x52";"R";r"R";//R"\\x52";r"\x52";//\x52

它不需要转义字符串中的特殊字符.

I saw the operator r#"" in Rust but I can't find what it does. It came in handy for creating JSON:

let var1 = "test1";
let json = r#"{"type": "type1", "type2": var1}"#;
println!("{}", json) // => {"type2": "type1", "type2": var1}

What's the name of the operator r#""? How do I make var1 evaluate?

解决方案

I can't find what it does

It has to do with string literals and raw strings. I think it is explained pretty well in this part of the documentation, in the code block that is posted there you can see what it does:

"foo"; r"foo";                     // foo
"\"foo\""; r#""foo""#;             // "foo"

"foo #\"# bar";
r##"foo #"# bar"##;                // foo #"# bar

"\x52"; "R"; r"R";                 // R
"\\x52"; r"\x52";                  // \x52

It negates the need to escape special characters inside the string.

这篇关于Rust 中的 r#""# 运算符是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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