What is r#""# in Rust? Complete Breakdown of the Magic Operator

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What Lies Behind the Mysterious r#""# in Rust? Full Explanation with Code



Have you ever encountered a strange notation like r#"..."# in Rust code and thought: "What kind of magic operator is this? Is it some secret syntax for the chosen few?" — you are not alone. Thousands of developers have asked this question on Stack Overflow, and today we will finally figure out what it is, why it's needed, and how to use raw string literals in Rust.



In this article, we won't just give a dry definition. We'll go from problem to solution, write real code, see how r#""# saves you from headaches, and learn where else this syntax is used. Let's go!



The Problem: When Regular Strings Break Your Code



Imagine you're writing a JSON parser or a regular expression. Inside the string, you need to use quotes and backslashes. In regular Rust code, this looks like a nightmare:



let json_string = "{\\"name\\": \\"Rust\\", \\"year\\": 2015}";let regex = "\\\\d{2}-\\\\d{2}-\\\\d{4}"; // date in DD-MM-YYYY format


See that? Every time you need a quote inside a string, you put a backslash before it (\\"). And if you need the backslash itself — you write \\\\. The code quickly turns into a "ladder" of slashes that's hard to read and debug.



But that's just the beginning. What if you need to insert a tab character into a string? Then you write \\t. And if you need the literal text \\t? You have to escape it: \\\\t. Your eyes start hurting by the second line.



The Solution: Raw Strings — r#""#



Rust offers an elegant solution — raw string literals. They are denoted by the prefix r and enclosed in #:



// Raw string: everything inside is treated as literal textlet raw_json = r#"{"name": "Rust", "year": 2015}"#;let raw_regex = r#"\\d{2}-\\d{2}-\\d{4}"#;


Feel the difference? No escaping! Quotes inside are just quotes. Backslashes are just slashes. The Rust compiler treats everything between r#" and "# as plain text, without trying to interpret escape sequences.



This is especially useful when working with:

  • Regular expressions — they are full of slashes and special characters.
  • JSON, XML, HTML — strings with lots of quotes.
  • SQL queries — especially with nested quotes.
  • Windows file paths — C:\\Users\\Name instead of C:\\\\Users\\\\Name.
  • Any data where the backslash should remain as is.


How Does It Work Under the Hood?



When Rust encounters r#"..."#, it simply copies everything between the opening r#" and the closing "# into the string without any processing. No turns into a newline, no \\t becomes a tab. It's a literal slice of text.



But there's a nuance: what if the string itself contains the sequence "#? For example, you want to insert the text #"hello"#. If you write r#"#"hello"#"#, the compiler will get confused — where does the literal end?



For this, Rust invented multiple hashes. You can use r##"..."##, r###"..."###, and so on. The number of # on both sides must match:



// String contains "# inside — use double hasheslet tricky = r##"String with "# inside"##;println!("{}", tricky); // Outputs: String with "# inside

// And if it contains "## inside? Then triple hashes:let very_tricky = r###"Text with "## ins

Blogs

Book Recommendations