Print double quotes in a string in Rust (")

r # \ escape double quotes backslash hashtag number sign

We can include basically any character in a string in Rust, but we cannot include a double-quote (") as that marks the end of the string. There are two ways: escaping the character or using raw strings

In the following example we try to print some text and include the content of the variable. The first print statement works as expected.

What if we would like to put that value in quotes? There is no problem in putting single-quotes around that value as single-quotes have no special treatment in a string in Rust.

However, if we would like to put the value in double-quotes we need to work a bit harder.

In the 3rd example we put a back-slash in-front of the quote character to tell Rust to forget about the special meaning of the double quote. We escaped it.

The back-slash \ is called the escape character in Rust, just as it is in probably every other programming language.

In the 4th example we can see the use of raw string literal

We start the string with r#" and terminate it with "#. We could have used any number of the pound-characters there (#) between 1-255 we only need to make sure the numbers are matching on both sides of the string. You can see this in the 5th example.

Now that we changed the end-of-string from a single " to a series of characters, we can freely use the double-quote " inside the string.

This is especially valuable as we need to add more and more of these double-quotes to our string.

The code


fn main() {
    let name = "Foo Bar";
    println!("Hello, {}, how are you?", name);
    println!("Hello, '{}', how are you?", name);
    println!("Hello, \"{}\", how are you?", name);
    println!(r#"Hello, "{}", how are you?"#, name);

    println!(r###"Hello, "{}", how are you?"###, name);

The output

Hello, Foo Bar, how are you?
Hello, 'Foo Bar', how are you?
Hello, "Foo Bar", how are you?
Hello, "Foo Bar", how are you?
Hello, "Foo Bar", how are you?


Gabor Szabo (szabgab)

Gabor Szabo, the author of the Rust Maven web site maintains several Open source projects in Rust and while he still feels he has tons of new things to learn about Rust he already offers training courses in Rust and still teaches Python, Perl, git, GitHub, GitLab, CI, and testing.

Gabor Szabo