What is `r#variable`?

I saw r#variable in here. Looks like it's Rust syntax.

  • What does it do?
  • What's the difference from the normal variable?
  • How come r#variable allowed to use (f#variable or other alphabet does not work)?
  • Is it related to r#""# ?

Thanks

they are called raw identifiers:

https://doc.rust-lang.org/stable/reference/identifiers.html#raw-identifiers

1 Like

Got it. I didn't know I can use it for variables as well as string like let r#const = "test";.
do you know what else we can use it for (variables, string text etc)?
Thanks!

Everything that used identifiers for a name. Such as the names of variables, function and closure parameters, bindings introduced by match or if let or for, type synonyms, enums, structs, traits, modules; the names of functions, associated items (e. g. associated types), constants (i. e. const items), macros.

1 Like

r#ident and r#"string"# are different constructs. The former is an identifier and the latter is a string literal. They are designed to look similar to avoid weirdness, but the r# prefix doesn't have to be (and isn't) universally applicable to any other kind of language construct.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.