Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# like verbatim string in Rust? [duplicate]

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?

like image 913
Natalie Perret Avatar asked Dec 24 '22 02:12

Natalie Perret


2 Answers

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.

like image 159
Zarenor Avatar answered Dec 26 '22 01:12

Zarenor


I guess raw string literals you are looking for ?

 let raw_string_literal = r#" line1 \n still line 1"#;
 println!("{}", raw_string_literal);
like image 22
Ömer Erden Avatar answered Dec 26 '22 01:12

Ömer Erden