Protected names
Rust groups keywords into two categories: weak and strict.
Strict keywords
Strict keywords are keywords which can only be used in their correct contexts, which is to say they cannot be used as the names of items, variables, fields and variants, type parameters, macros or crates.
Generated files, modules, types and variables may not use strict keywords.
| Keywords | |||||
|---|---|---|---|---|---|
as | break | const | continue | crate | else |
enum | extern | false | fn | for | if |
impl | in | let | loop | match | mod |
move | mut | pub | ref | return | self |
Self | static | struct | super | trait | true |
type | unsafe | use | where | while | async |
await | dyn | abstract | become | box | do |
final | macro | override | priv | typeof | unsized |
virtual | yield | try |
Strict keywords will be escaped in the generated Rust code by appending an
underscore to the name, e.g. module_.
Weak keywords
Weak keywords have special meaning only in certain contexts. None of the current weak keywords affect the naming of types or variables. Thus, weak keywords are not reserved and may be freely used.
| Keyword | Context |
|---|---|
union | Only a keyword when used in a union declaration. |
'static | Cannot be used as a generic lifetime parameter or loop label. |
For example, the following is perfectly valid Rust code:
#![allow(unused)]
fn main() {
fn union() {
union union<'union> {
union: &'union union<'union>
}
}
}