Rust Reference ยท Keywords and Statements
Reserved keywords
Reference for Rust's reserved keywords: abstract, become, box, do, final, gen, macro, override, priv, try, typeof, unsized, virtual and yield, with edition notes.
Rust keeps a list of words that do nothing today but cannot be used as identifiers. They are reserved so that the language can grow into them later without breaking existing code. Using one as a variable, function, field or type name is a compile error.
The reserved list is: abstract, become, box, do, final, macro, override, priv, typeof, unsized, virtual, yield, plus try (reserved since edition 2018) and gen (reserved since edition 2024, for future generator blocks). If you need one of these names, use a raw identifier such as r#gen.
let r#gen = 7; // legal: raw identifier // let gen = 7; // error in edition 2024
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
There is no syntax to learn: these words simply cannot be written as plain identifiers. The table lists why each is reserved.
| Word | Likely future use |
|---|---|
abstract, final, override, virtual |
Inheritance-style features, reserved defensively. |
become |
Guaranteed tail calls. |
box |
Box patterns and syntax (the old box expression was removed). |
do |
A possible do notation for monads or do yeet-like constructs. |
macro |
A future declarative macro system (macros 2.0). |
priv |
Historical (old visibility keyword). |
try |
try blocks, which short-circuit with ?. |
typeof |
A type-of-expression operator. |
unsized |
Possible unsized-type syntax. |
yield |
Generators and coroutines. |
gen |
gen blocks and generator functions (reserved in edition 2024). |
Errors: a plain use gives expected identifier, found reserved keyword.
Usage notes
- Edition-dependent.
genis only reserved in edition 2024 and later; in edition 2021 code it is a normal identifier.cargo fix --editionrewrites such names tor#gen. tryis reserved in 2018 and later. This breaks old code that had a function calledtry, which is why thetry!macro was writtenr#try!.- These are the reserved words only. Real keywords (
fn,let,match...) are in their own pages, and weak keywords (union,'static,macro_rules,dynin 2015) are only special in certain positions. - Names that cross crate boundaries: a field named
r#finalis still spelledfinalin other languages and in serialized output; with serde you can use#[serde(rename = "final")]. - Pick a different name when you can.
is_final,kindorgenerationread better than raw identifiers. - Macros see the raw form: a
$name:identfragment acceptsr#gen, but not a baregen.
Related
- Raw identifiers,
fn,asyncandawait - Lessons: Variables, Syntax, What's new