Multiline Strings in Rust: Syntax, Examples, and Secrets

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

Multiline Strings in Rust: How Not to Go Crazy with Quotes and Backslashes

\n\n

Hi there! If you've ever written in Python, you know that creating a multiline string there is a piece of cake: three quotes """...""" — and you're done. But as soon as you switched to Rust, you suddenly realized that the familiar trick doesn't work. And now you're Googling: "What is the syntax for a multiline string literal?". Sound familiar?

\n\n

Don't worry, this is the most popular question among Rust beginners, which has gathered thousands of views on Stack Overflow. And today we'll finally figure out how to work with long strings in Rust without breaking your brain. Let's go!

\n\n

Why Doesn't Rust Like Triple Quotes (Like Python)?

\n\n

Rust is a systems language and it's strict. It doesn't tolerate liberties. Triple quotes """ in Rust are not syntax for multiline strings, but the start of a comment or documentation (if it's ///). So if you try to write:

\n\n
// ❌ This doesn't work!\nlet text = """This\nis a multiline\nstring""";\n
\n\n

The compiler will look at you reproachfully and give an error. Rust uses a different approach for multiline strings — and it's even cooler than in Python!

\n\n

Method #1: Regular Quotes and Line Breaks

\n\n

The simplest way is to just break the string in the code using the \\n character inside the string. But that's boring and unreadable. Much more interesting is to use an actual line break inside double quotes. Rust allows this!

\n\n
// ✅ Works!\nlet poem = "Here's an example of a string,\nthat spans two lines,\nand this is perfectly legal in Rust.";\nprintln!("{}", poem);\n
\n\n

The output will contain line break characters where you placed them in the code. But there's a nuance: if you want to preserve indentation (spaces at the beginning of lines), they will also end up in the string. Sometimes this is useful, sometimes not.

\n\n

Method #2: Escaping and the Backslash — Your Friend (and Enemy)

\n\n

If you need to write a long string in the code but not add line breaks to it (for readability, for example), use a backslash at the end of the line:

\n\n
// ✅ String without line breaks, but readable in code\nlet long_string = "This is a very long string, \\\nwhich is broken into several lines in the code, \\\nbut in memory will be one string without \\n.";\nprintln!("{}", long_string);\n
\n\n

Note: there should be no spaces after the backslash — press Enter immediately. Otherwise, Rust will think you're escaping a space and everything will break.

\n\n

Method #3: Raw String Literals — Magic Without Escaping

\n\n

Now this is Rust's secret weapon. If you need to write a string containing many quotes, backslashes, or other special characters (e.g., JSON, a regular expression, or a Windows path), use r"" — raw string literals.

\n\n

Inside r"...", all characters are taken literally. No escaping!

\n\n
// ✅ Raw string — no backslashes!\nlet json = r#"{\n    "name": "Rust",\n    "version": 1.75,\n    "features": ["safety", "speed"]\n}"#;\nprintln!("{}", json);\n
\n\n

But what if the string contains "#? For this, Rust allows you to increase the number of # around the quotes: r##"..."##, r###"..."###, and so on to infinity. Just choose a number of hashes that doesn't appear inside the string.

\n\n
// ✅ Raw string with hash escaping\nlet tricky = r###"A string with "quotes" and even "#" inside!"###;\nprintln!("{}", tricky);\n
\n\n

Method #4: Multiline Raw Strings — A Combine for Complex Data

\n\n

Now let's put it all together. If you need to write a multiline block of text with quotes, backslashes, and indentation, use r#""# with line breaks directly inside:

Blogs

Book Recommendations