5

I am looking for a verbatim strings in Rust (like in C# with @"This is a string and here ""I am between quotes without making a fuss""").

Is there something similar?

Natalie Perret
  • 8,013
  • 12
  • 66
  • 129

2 Answers2

8

This is surprisingly difficult to find.

In rust, raw string literals are surrounded by r"", and add # if you need to use quotes. For your example,
r#"This is a string and here "I am between quotes without making a fuss""#
should work. (Double-quoting will produce doubled quotes in the string.)

If you need something with the # symbol, you can do something like
r###"This string can have ## in as many places as I like ##, but never three in a row ##"###

However, in rust raw strings, escapes are disallowed. You cannot use \n, for example. You can, however include any UTF-8 character you want.

Zarenor
  • 1,161
  • 1
  • 11
  • 23
  • Aw too bad I thought adding some new lines at some point. But fair enough will play with the Rust rules. Thanks for your answer! – Natalie Perret Dec 04 '18 at 13:05
  • To be clear: It will look really odd, but you can include the newline character in the raw literal.. If you read the link I provided, some more explanation is provided. – Zarenor Dec 04 '18 at 13:07
3

I guess raw string literals you are looking for ?

 let raw_string_literal = r#" line1 \n still line 1"#;
 println!("{}", raw_string_literal);
Ömer Erden
  • 7,680
  • 5
  • 36
  • 45