アレックス アレックス - 5 months ago 19
JSON Question

The operator r#""# in Rust

I accidentally saw an operator r#"" in Rust but I can't find what it does. Nonetheless, it came in handy for creating JSON:

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


My questions are: what's the name of the operator
r#""
and how do I make
var1
evaluate?

Answer

I can't find what it does

It has to do with string literals & 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.
I don't have the answer for the 2nd part of your question but I hope this was useful to you.