Raw identifiers

Minimum Rust version: 1.30

Rust, like many programming languages, has the concept of "keywords".These identifiers mean something to the language, and so you cannot use them inplaces like variable names, function names, and other places.Raw identifiers let you use keywords where they would not normally be allowed.

For example, match is a keyword. If you try to compile this function:

  1. fn match(needle: &str, haystack: &str) -> bool {
  2. haystack.contains(needle)
  3. }

You'll get this error:

  1. error: expected identifier, found keyword `match`
  2. --> src/main.rs:4:4
  3. |
  4. 4 | fn match(needle: &str, haystack: &str) -> bool {
  5. | ^^^^^ expected identifier, found keyword

You can write this with a raw identifier:

  1. fn r#match(needle: &str, haystack: &str) -> bool {
  2. haystack.contains(needle)
  3. }
  4. fn main() {
  5. assert!(r#match("foo", "foobar"));
  6. }

Note the r# prefix on both the function name as well as the call.

Motivation

This feature is useful for a few reasons, but the primary motivation wasinter-edition situations. For example, try is not a keyword in the 2015edition, but is in the 2018 edition. So if you have a library that is writtenin Rust 2015 and has a try function, to call it in Rust 2018, you'll needto use the raw identifier.

New keywords

The new confirmed keywords in edition 2018 are:

async and await

Here, async is reserved for use in async fn as well as in async || closures andasync { .. } blocks. Meanwhile, await is reserved to keep our options openwith respect to await!(expr) syntax. See RFC 2394 for more details.

try

The do catch { .. } blocks have been renamed to try { .. } and to supportthat, the keyword try is reserved in edition 2018.See RFC 2388 for more details.