Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Embed double quotes in string

  • String in Rust are inside double quotes.
  • It is easy to inlcude single quote in a string as it is not special.
  • In order to include a double quote we need to add the escape character, the back-slash, in-front of it.
  • Alternatively we can start the string using r#" and then we can end it with "#. This allows us to freely include double-quote in the string.
fn main() {
    let name = String::from("Foo");

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