|
| 1 | +<!-- DO NOT EDIT THIS FILE. |
| 2 | +
|
| 3 | +This file is periodically generated from the content in the `/src/` |
| 4 | +directory, so all fixes need to be made in `/src/`. |
| 5 | +--> |
| 6 | + |
| 7 | +[TOC] |
| 8 | + |
| 9 | +## Appendix A: Keywords |
| 10 | + |
| 11 | +The following lists contain keywords that are reserved for current or future |
| 12 | +use by the Rust language. As such, they cannot be used as identifiers (except |
| 13 | +as raw identifiers, as we discuss in “Raw Identifiers” on page 497). |
| 14 | +*Identifiers* are names of functions, variables, parameters, struct fields, |
| 15 | +modules, crates, constants, macros, static values, attributes, types, traits, |
| 16 | +or lifetimes. |
| 17 | + |
| 18 | +## Keywords Currently in Use |
| 19 | + |
| 20 | +The following is a list of keywords currently in use, with their functionality |
| 21 | +described. |
| 22 | + |
| 23 | +`as` perform primitive casting, disambiguate the specific trait containing an |
| 24 | +item, or rename items in `use` statements |
| 25 | + |
| 26 | +`async` return a `Future` instead of blocking the current thread |
| 27 | + |
| 28 | +`await` suspend execution until the result of a `Future` is ready |
| 29 | + |
| 30 | +`break` exit a loop immediately |
| 31 | + |
| 32 | +`const` define constant items or constant raw pointers |
| 33 | + |
| 34 | +`continue` continue to the next loop iteration |
| 35 | + |
| 36 | +`crate` in a module path, refers to the crate root |
| 37 | + |
| 38 | +`dyn` dynamic dispatch to a trait object |
| 39 | + |
| 40 | +`else` fallback for `if` and `if let` control flow constructs |
| 41 | + |
| 42 | +`enum` define an enumeration |
| 43 | + |
| 44 | +`extern` link an external function or variable |
| 45 | + |
| 46 | +`false` Boolean false literal |
| 47 | + |
| 48 | +`fn` define a function or the function pointer type |
| 49 | + |
| 50 | +`for` loop over items from an iterator, implement a trait, or specify a |
| 51 | +higher-ranked lifetime |
| 52 | + |
| 53 | +`if` branch based on the result of a conditional expression |
| 54 | + |
| 55 | +`impl` implement inherent or trait functionality |
| 56 | + |
| 57 | +`in` part of `for` loop syntax |
| 58 | + |
| 59 | +`let` bind a variable |
| 60 | + |
| 61 | +`loop` loop unconditionally |
| 62 | + |
| 63 | +`match` match a value to patterns |
| 64 | + |
| 65 | +`mod` define a module |
| 66 | + |
| 67 | +`move` make a closure take ownership of all its captures |
| 68 | + |
| 69 | +`mut` denote mutability in references, raw pointers, or pattern bindings |
| 70 | + |
| 71 | +`pub` denote public visibility in struct fields, `impl` blocks, or modules |
| 72 | + |
| 73 | +`ref` bind by reference |
| 74 | + |
| 75 | +`return` return from function |
| 76 | + |
| 77 | +`Self` a type alias for the type we are defining or implementing |
| 78 | + |
| 79 | +`self` method subject or current module |
| 80 | + |
| 81 | +`static` global variable or lifetime lasting the entire program execution |
| 82 | + |
| 83 | +`struct` define a structure |
| 84 | + |
| 85 | +`super` parent module of the current module |
| 86 | + |
| 87 | +`trait` define a trait |
| 88 | + |
| 89 | +`true` Boolean true literal |
| 90 | + |
| 91 | +`type` define a type alias or associated type |
| 92 | + |
| 93 | +`union` define a union; is a keyword only when used in a union declaration |
| 94 | + |
| 95 | +`unsafe` denote unsafe code, functions, traits, or implementations |
| 96 | + |
| 97 | +`use` bring symbols into scope |
| 98 | + |
| 99 | +`where` denote clauses that constrain a type |
| 100 | + |
| 101 | +`while` loop conditionally based on the result of an expression |
| 102 | + |
| 103 | +## Keywords Reserved for Future Use |
| 104 | + |
| 105 | +The following keywords do not yet have any functionality but are reserved by |
| 106 | +Rust for potential future use: |
| 107 | + |
| 108 | +* `abstract` |
| 109 | +* `become` |
| 110 | +* `box` |
| 111 | +* `do` |
| 112 | +* `final` |
| 113 | +* `macro` |
| 114 | +* `override` |
| 115 | +* `priv` |
| 116 | +* `try` |
| 117 | +* `typeof` |
| 118 | +* `unsized` |
| 119 | +* `virtual` |
| 120 | +* `yield` |
| 121 | + |
| 122 | +## Raw Identifiers |
| 123 | + |
| 124 | +*Raw identifiers* are the syntax that lets you use keywords where they wouldn’t |
| 125 | +normally be allowed. You use a raw identifier by prefixing a keyword with `r#`. |
| 126 | + |
| 127 | +For example, `match` is a keyword. If you try to compile the following function |
| 128 | +that uses `match` as its name: |
| 129 | + |
| 130 | +Filename: src/main.rs |
| 131 | + |
| 132 | +``` |
| 133 | +fn match(needle: &str, haystack: &str) -> bool { |
| 134 | + haystack.contains(needle) |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +you’ll get this error: |
| 139 | + |
| 140 | +``` |
| 141 | +error: expected identifier, found keyword `match` |
| 142 | + --> src/main.rs:4:4 |
| 143 | + | |
| 144 | +4 | fn match(needle: &str, haystack: &str) -> bool { |
| 145 | + | ^^^^^ expected identifier, found keyword |
| 146 | +``` |
| 147 | + |
| 148 | +The error shows that you can’t use the keyword `match` as the function |
| 149 | +identifier. To use `match` as a function name, you need to use the raw |
| 150 | +identifier syntax, like this: |
| 151 | + |
| 152 | +Filename: src/main.rs |
| 153 | + |
| 154 | +``` |
| 155 | +fn r#match(needle: &str, haystack: &str) -> bool { |
| 156 | + haystack.contains(needle) |
| 157 | +} |
| 158 | +
|
| 159 | +fn main() { |
| 160 | + assert!(r#match("foo", "foobar")); |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +This code will compile without any errors. Note the `r#` prefix on the function |
| 165 | +name in its definition as well as where the function is called in `main`. |
| 166 | + |
| 167 | +Raw identifiers allow you to use any word you choose as an identifier, even if |
| 168 | +that word happens to be a reserved keyword. This gives us more freedom to |
| 169 | +choose identifier names, as well as lets us integrate with programs written in |
| 170 | +a language where these words aren’t keywords. In addition, raw identifiers |
| 171 | +allow you to use libraries written in a different Rust edition than your crate |
| 172 | +uses. For example, `try` isn’t a keyword in the 2015 edition but is in the 2018 |
| 173 | +and 2021 editions. If you depend on a library that is written using the 2015 |
| 174 | +edition and has a `try` function, you’ll need to use the raw identifier syntax, |
| 175 | +`r#try` in this case, to call that function from your 2021 edition code. See |
| 176 | +Appendix E for more information on editions. |
| 177 | + |
0 commit comments