Keywords
Rust divides keywords into three categories:
Strict keywords
These keywords can only be used in their correct contexts. They cannot be used as the names of:
- Items
- Variables and function parameters
- Fields and variants
- Type parameters
- Lifetime parameters or loop labels
- Macros or attributes
- Macro placeholders
- Crates
The following keywords are in all editions:
asbreakconstcontinuecrateelseenumexternfalsefnforifimplinletloopmatchmodmovemutpubrefreturnselfSelfstaticstructsupertraittruetypeunsafeusewherewhile
The following keywords were added beginning in the 2018 edition.
asyncawaitdyn
Reserved keywords
These keywords aren’t used yet, but they are reserved for future use. They have the same restrictions as strict keywords. The reasoning behind this is to make current programs forward compatible with future versions of Rust by forbidding them to use these keywords.
abstractbecomeboxdofinalmacrooverrideprivtypeofunsizedvirtualyield
The following keywords are reserved beginning in the 2018 edition.
try
The following keywords are reserved beginning in the 2024 edition.
gen
Weak keywords
These keywords have special meaning only in certain contexts. For example, it
is possible to declare a variable or method with the name union.
'staticmacro_rulesrawsafeunion
macro_rulesis used to create custom macros.
unionis used to declare a union and is only a keyword when used in a union declaration.
-
'staticis used for the static lifetime and cannot be used as a generic lifetime parameter or loop label// error[E0262]: invalid lifetime parameter name: `'static` fn invalid_lifetime_parameter<'static>(s: &'static str) -> &'static str { s }
safeis used for functions and statics, which has meaning in external blocks.
rawis used for raw borrow operators, and is only a keyword when matching a raw borrow operator form (such as&raw const expror&raw mut expr).
2018 Edition differences
In the 2015 edition,
dynis a keyword when used in a type position followed by a path that does not start with::or<, a lifetime, a question mark, aforkeyword or an opening parenthesis.Beginning in the 2018 edition,
dynhas been promoted to a strict keyword.