4 ms·
Are raw identifiers "compatible" with normal ones? E.g. if i define fn r#something(), can I call it just as something()? Besides the backward compatibility rea
by Matthias247 8y ago
Are raw identifiers "compatible" with normal ones? E.g. if i define fn r#something(), can I call it just as something()?
Besides the backward compatibility reason they might also be good for codegen tools. I encountered the situation multiple times that some identifier was used in an IDL (e.g. Thrift, Protobuf, etc) that then got exported into a keyword in a target language which prevented it from compiling. To word around that sometimes the IDL definition needed to be changed, which wasn't ideal. By generating only raw identifiers this can be avoided.
- steveklabnik 8y agoThe only reason to define it as `r#something` is if something is a keyword, and so regular-old `something` doesn't work. But if you happened to do it anyway, then yeah: https://play.rust-lang.org/?version=beta&mode=debug&edition=2015&gist=e73bd3ed13cf9e28e3b0e8e8145be957 https://play.rust-lang.org/?version=beta&mode=debug&edition=... But yes, the `r#` isn't part of the name; if I define a Rust 2015 function as `fn async()`, in 2015 i can call it as just regular async() in 2015 code, and only need r#async() in Rust 2018 code. And yeah, this is useful for codegen tools as well.