]> git.proxmox.com Git - rustc.git/blame - src/doc/edition-guide/src/rust-2018/module-system/raw-identifiers.md
New upstream version 1.53.0+dfsg1
[rustc.git] / src / doc / edition-guide / src / rust-2018 / module-system / raw-identifiers.md
CommitLineData
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
5Rust, like many programming languages, has the concept of "keywords".
6These identifiers mean something to the language, and so you cannot use them in
7places like variable names, function names, and other places.
8Raw identifiers let you use keywords where they would not normally be allowed.
9
10For example, `match` is a keyword. If you try to compile this function:
11
12```rust,ignore
13fn match(needle: &str, haystack: &str) -> bool {
14 haystack.contains(needle)
15}
16```
17
18You'll get this error:
19
20```text
21error: expected identifier, found keyword `match`
22 --> src/main.rs:4:4
23 |
244 | fn match(needle: &str, haystack: &str) -> bool {
25 | ^^^^^ expected identifier, found keyword
26```
27
28You can write this with a raw identifier:
29
30```rust
31fn r#match(needle: &str, haystack: &str) -> bool {
32 haystack.contains(needle)
33}
34
35fn main() {
36 assert!(r#match("foo", "foobar"));
37}
38```
39
40Note the `r#` prefix on both the function name as well as the call.
41
42## Motivation
43
44This feature is useful for a few reasons, but the primary motivation was
45inter-edition situations. For example, `try` is not a keyword in the 2015
46edition, but is in the 2018 edition. So if you have a library that is written
47in Rust 2015 and has a `try` function, to call it in Rust 2018, you'll need
48to use the raw identifier.
49
50## New keywords
51
52The 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
58Here, `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
60with 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
66The `do catch { .. }` blocks have been renamed to `try { .. }` and to support
67that, the keyword `try` is reserved in edition 2018.
68See [RFC 2388] for more details.