03-关键字
2 分钟阅读
译文 · 基于 The Rust Reference
r[lex.keywords]
关键字
Rust 将关键字分为三类:
r[lex.keywords.strict]
严格关键字
r[lex.keywords.strict.intro] 这些关键字只能在其正确的上下文中使用。它们不能用作下列名称:
r[lex.keywords.strict.list] 下列关键字在所有 edition 中都存在:
_asasyncawaitbreakconstcontinuecratedynelseenumexternfalsefnforifimplinletloopmatchmodmovemutpubrefreturnselfSelfstaticstructsupertraittruetypeunsafeusewherewhile
r[lex.keywords.strict.edition2018]
[!EDITION-2018] 下列关键字是在 2018 edition 中加入的:
asyncawaitdyn
r[lex.keywords.reserved]
保留关键字
r[lex.keywords.reserved.intro] 这些关键字尚未使用,但为将来使用而保留。它们与严格关键字有相同的限制。这样做的理由是:通过禁止当前程序使用这些关键字,使当前程序与未来版本的 Rust 向前兼容。
r[lex.keywords.reserved.list]
abstractbecomeboxdofinalgenmacrooverrideprivtrytypeofunsizedvirtualyield
r[lex.keywords.reserved.edition2018]
[!EDITION-2018]
try关键字在 2018 edition 中被加入为保留关键字。
r[lex.keywords.reserved.edition2024]
[!EDITION-2024]
gen关键字在 2024 edition 中被加入为保留关键字。
r[lex.keywords.weak]
弱关键字
r[lex.keywords.weak.intro]
这些关键字仅在特定上下文中具有特殊含义。例如,可以声明名为 union 的变量或方法。
'staticmacro_rulesrawsafeunion
r[lex.keywords.weak.macro_rules]
macro_rules用于创建自定义 宏。
r[lex.keywords.weak.union]
union用于声明 联合体,且仅在联合体声明中使用时才是关键字。
r[lex.keywords.weak.lifetime-static]
'static用于静态生命周期,不能用作 泛型生命周期参数 或 循环标签// error[E0262]: 无效的生命周期参数名:`'static` fn invalid_lifetime_parameter<'static>(s: &'static str) -> &'static str { s }
r[lex.keywords.weak.safe]
safe用于函数和静态项,在 外部块 中具有含义。
r[lex.keywords.weak.raw]
raw用于 原始借用运算符,且仅在匹配原始借用运算符形式(如&raw const expr或&raw mut expr)时才是关键字。
r[lex.keywords.weak.dyn.edition2018]
[!EDITION-2018] 在 2015 edition 中,当
dyn用在类型位置,且其后跟随不以::或<开头的路径、生命周期、问号、for关键字或开括号时,它是关键字。从 2018 edition 开始,
dyn已被提升为严格关键字。