|
1 | 1 | ## Appendix A: Keywords
|
2 | 2 |
|
3 |
| -The following list contains keywords that are reserved for current or future |
4 |
| -use by the Rust language. As such, they cannot be used as identifiers (except as |
5 |
| -[raw identifiers][raw-identifiers]), including names of functions, variables, |
6 |
| -parameters, struct fields, modules, crates, constants, macros, static values, |
7 |
| -attributes, types, traits, or lifetimes. |
| 3 | +The 2018 edition of the book is no longer distributed with Rust's documentation. |
8 | 4 |
|
9 |
| -### Keywords Currently in Use |
| 5 | +If you came here via a link or web search, you may want to check out [the current |
| 6 | +version of the book](../index.html) instead. |
10 | 7 |
|
11 |
| -The following keywords currently have the functionality described. |
12 |
| - |
13 |
| -* `as` - perform primitive casting, disambiguate the specific trait containing |
14 |
| - an item, or rename items in `use` and `extern crate` statements |
15 |
| -* `break` - exit a loop immediately |
16 |
| -* `const` - define constant items or constant raw pointers |
17 |
| -* `continue` - continue to the next loop iteration |
18 |
| -* `crate` - link an external crate or a macro variable representing the crate in |
19 |
| - which the macro is defined |
20 |
| -* `dyn` - dynamic dispatch to a trait object |
21 |
| -* `else` - fallback for `if` and `if let` control flow constructs |
22 |
| -* `enum` - define an enumeration |
23 |
| -* `extern` - link an external crate, function, or variable |
24 |
| -* `false` - Boolean false literal |
25 |
| -* `fn` - define a function or the function pointer type |
26 |
| -* `for` - loop over items from an iterator, implement a trait, or specify a |
27 |
| - higher-ranked lifetime |
28 |
| -* `if` - branch based on the result of a conditional expression |
29 |
| -* `impl` - implement inherent or trait functionality |
30 |
| -* `in` - part of `for` loop syntax |
31 |
| -* `let` - bind a variable |
32 |
| -* `loop` - loop unconditionally |
33 |
| -* `match` - match a value to patterns |
34 |
| -* `mod` - define a module |
35 |
| -* `move` - make a closure take ownership of all its captures |
36 |
| -* `mut` - denote mutability in references, raw pointers, or pattern bindings |
37 |
| -* `pub` - denote public visibility in struct fields, `impl` blocks, or modules |
38 |
| -* `ref` - bind by reference |
39 |
| -* `return` - return from function |
40 |
| -* `Self` - a type alias for the type implementing a trait |
41 |
| -* `self` - method subject or current module |
42 |
| -* `static` - global variable or lifetime lasting the entire program execution |
43 |
| -* `struct` - define a structure |
44 |
| -* `super` - parent module of the current module |
45 |
| -* `trait` - define a trait |
46 |
| -* `true` - Boolean true literal |
47 |
| -* `type` - define a type alias or associated type |
48 |
| -* `unsafe` - denote unsafe code, functions, traits, or implementations |
49 |
| -* `use` - bring symbols into scope |
50 |
| -* `where` - denote clauses that constrain a type |
51 |
| -* `while` - loop conditionally based on the result of an expression |
52 |
| - |
53 |
| -### Keywords Reserved for Future Use |
54 |
| - |
55 |
| -The following keywords do not have any functionality but are reserved by Rust |
56 |
| -for potential future use. |
57 |
| - |
58 |
| -* `abstract` |
59 |
| -* `async` |
60 |
| -* `become` |
61 |
| -* `box` |
62 |
| -* `do` |
63 |
| -* `final` |
64 |
| -* `macro` |
65 |
| -* `override` |
66 |
| -* `priv` |
67 |
| -* `try` |
68 |
| -* `typeof` |
69 |
| -* `unsized` |
70 |
| -* `virtual` |
71 |
| -* `yield` |
72 |
| - |
73 |
| -### Raw identifiers |
74 |
| -[raw-identifiers]: #raw-identifiers |
75 |
| - |
76 |
| -Raw identifiers let you use keywords where they would not normally be allowed by |
77 |
| -prefixing them with `r#`. |
78 |
| - |
79 |
| -For example, `match` is a keyword. If you try to compile this function: |
80 |
| - |
81 |
| -```rust,ignore |
82 |
| -fn match(needle: &str, haystack: &str) -> bool { |
83 |
| - haystack.contains(needle) |
84 |
| -} |
85 |
| -``` |
86 |
| - |
87 |
| -You’ll get this error: |
88 |
| - |
89 |
| -```text |
90 |
| -error: expected identifier, found keyword `match` |
91 |
| - --> src/main.rs:4:4 |
92 |
| - | |
93 |
| -4 | fn match(needle: &str, haystack: &str) -> bool { |
94 |
| - | ^^^^^ expected identifier, found keyword |
95 |
| -``` |
96 |
| - |
97 |
| -You can write this with a raw identifier: |
98 |
| - |
99 |
| -```rust |
100 |
| -fn r#match(needle: &str, haystack: &str) -> bool { |
101 |
| - haystack.contains(needle) |
102 |
| -} |
103 |
| - |
104 |
| -fn main() { |
105 |
| - assert!(r#match("foo", "foobar")); |
106 |
| -} |
107 |
| -``` |
108 |
| - |
109 |
| -Note the `r#` prefix on both the function name as well as the call. |
110 |
| - |
111 |
| -#### Motivation |
112 |
| - |
113 |
| -This feature is useful for a few reasons, but the primary motivation was |
114 |
| -inter-edition situations. For example, `try` is not a keyword in the 2015 |
115 |
| -edition, but is in the 2018 edition. So if you have a library that is written |
116 |
| -in Rust 2015 and has a `try` function, to call it in Rust 2018, you’ll need |
117 |
| -to use the raw identifier. |
| 8 | +If you have an internet connection, you can [find a copy distributed with |
| 9 | +Rust |
| 10 | +1.30](https://doc.rust-lang.org/1.30.0/book/2018-edition/appendix-01-keywords.html). |
0 commit comments