Skip to content
Tutorials Center
My learning

Topics

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.

Rust
let r#gen = 7;       // legal: raw identifier
// let gen = 7;      // error in edition 2024

Try it

Rust Using a reserved word the right way

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.

Rust What the compiler says

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. gen is only reserved in edition 2024 and later; in edition 2021 code it is a normal identifier. cargo fix --edition rewrites such names to r#gen.
  • try is reserved in 2018 and later. This breaks old code that had a function called try, which is why the try! macro was written r#try!.
  • These are the reserved words only. Real keywords (fn, let, match...) are in their own pages, and weak keywords (union, 'static, macro_rules, dyn in 2015) are only special in certain positions.
  • Names that cross crate boundaries: a field named r#final is still spelled final in other languages and in serialized output; with serde you can use #[serde(rename = "final")].
  • Pick a different name when you can. is_final, kind or generation read better than raw identifiers.
  • Macros see the raw form: a $name:ident fragment accepts r#gen, but not a bare gen.