]>
Commit | Line | Data |
---|---|---|
450edc1f XL |
1 | # Raw identifiers |
2 | ||
532ac7d7 | 3 | ![Minimum Rust version: 1.30](https://img.shields.io/badge/Minimum%20Rust%20Version-1.30-brightgreen.svg) |
450edc1f XL |
4 | |
5 | Rust, like many programming languages, has the concept of "keywords". | |
6 | These identifiers mean something to the language, and so you cannot use them in | |
7 | places like variable names, function names, and other places. | |
8 | Raw identifiers let you use keywords where they would not normally be allowed. | |
9 | ||
10 | For example, `match` is a keyword. If you try to compile this function: | |
11 | ||
12 | ```rust,ignore | |
13 | fn match(needle: &str, haystack: &str) -> bool { | |
14 | haystack.contains(needle) | |
15 | } | |
16 | ``` | |
17 | ||
18 | You'll get this error: | |
19 | ||
20 | ```text | |
21 | error: expected identifier, found keyword `match` | |
22 | --> src/main.rs:4:4 | |
23 | | | |
24 | 4 | fn match(needle: &str, haystack: &str) -> bool { | |
25 | | ^^^^^ expected identifier, found keyword | |
26 | ``` | |
27 | ||
28 | You can write this with a raw identifier: | |
29 | ||
30 | ```rust | |
31 | fn r#match(needle: &str, haystack: &str) -> bool { | |
32 | haystack.contains(needle) | |
33 | } | |
34 | ||
35 | fn main() { | |
36 | assert!(r#match("foo", "foobar")); | |
37 | } | |
38 | ``` | |
39 | ||
40 | Note the `r#` prefix on both the function name as well as the call. | |
41 | ||
42 | ## Motivation | |
43 | ||
44 | This feature is useful for a few reasons, but the primary motivation was | |
45 | inter-edition situations. For example, `try` is not a keyword in the 2015 | |
46 | edition, but is in the 2018 edition. So if you have a library that is written | |
47 | in Rust 2015 and has a `try` function, to call it in Rust 2018, you'll need | |
48 | to use the raw identifier. | |
49 | ||
50 | ## New keywords | |
51 | ||
52 | The new confirmed keywords in edition 2018 are: | |
53 | ||
54 | ### `async` and `await` | |
55 | ||
56 | [RFC 2394]: https://github.com/rust-lang/rfcs/blob/master/text/2394-async_await.md#final-syntax-for-the-await-expression | |
57 | ||
58 | Here, `async` is reserved for use in `async fn` as well as in `async ||` closures and | |
59 | `async { .. }` blocks. Meanwhile, `await` is reserved to keep our options open | |
60 | with respect to `await!(expr)` syntax. See [RFC 2394] for more details. | |
61 | ||
62 | ### `try` | |
63 | ||
64 | [RFC 2388]: https://github.com/rust-lang/rfcs/pull/2388 | |
65 | ||
66 | The `do catch { .. }` blocks have been renamed to `try { .. }` and to support | |
67 | that, the keyword `try` is reserved in edition 2018. | |
68 | See [RFC 2388] for more details. |