x-i18n: generated_at: “2026-03-01T09:10:28Z” model: gemini-3-flash-preview provider: google-gemini-cli source_hash: 9178c0420eb760c7c8fc7dd3a5c6d790c133f66968a9cac90f71c743c65ed8d6 source_path: appendix-01-keywords.md workflow: 16
附录 A:关键字 (Appendix A: Keywords)
Appendix A: Keywords
以下列表包含为 Rust 语言当前或未来使用而保留的关键字。因此,它们不能用作标识符(除非作为原始标识符,正如我们在“原始标识符”部分所讨论的那样)。标识符 (Identifiers) 是函数、变量、参数、结构体字段、模块、Crates、常量、宏、静态值、属性、类型、Traits 或生命周期的名称。
The following lists contain keywords that are reserved for current or future use by the Rust language. As such, they cannot be used as identifiers (except as raw identifiers, as we discuss in the “Raw Identifiers” section). Identifiers are names of functions, variables, parameters, struct fields, modules, crates, constants, macros, static values, attributes, types, traits, or lifetimes.
目前使用的关键字 (Keywords Currently in Use)
Keywords Currently in Use
以下是目前正在使用的关键字列表,及其功能的简要描述。
The following is a list of keywords currently in use, with their functionality described.
as:执行原始类型转换、消除包含某项的特定 trait 的歧义,或在use语句中重命名项。as: Perform primitive casting, disambiguate the specific trait containing an item, or rename items inusestatements.async:返回一个Future而不是阻塞当前线程。async: Return aFutureinstead of blocking the current thread.await:暂停执行,直到Future的结果就绪。await: Suspend execution until the result of aFutureis ready.break:立即退出循环。break: Exit a loop immediately.const:定义常量条目或常量原始指针。const: Define constant items or constant raw pointers.continue:继续下一次循环迭代。continue: Continue to the next loop iteration.crate:在模块路径中,引用 crate 根。crate: In a module path, refers to the crate root.dyn:动态分发到 trait 对象。dyn: Dynamic dispatch to a trait object.else:if和if let控制流结构的备选分支。else: Fallback forifandif letcontrol flow constructs.enum:定义枚举。enum: Define an enumeration.extern:链接外部函数或变量。extern: Link an external function or variable.false:布尔假字面值。false: Boolean false literal.fn:定义函数或函数指针类型。fn: Define a function or the function pointer type.for:遍历迭代器中的项、实现 trait 或指定高阶生命周期 (higher ranked lifetime)。for: Loop over items from an iterator, implement a trait, or specify a higher ranked lifetime.if:基于条件表达式的结果进行分支。if: Branch based on the result of a conditional expression.impl:实现固有功能或 trait 功能。impl: Implement inherent or trait functionality.in:for循环语法的一部分。in: Part offorloop syntax.let:绑定变量。let: Bind a variable.loop:无条件循环。loop: Loop unconditionally.match:将值与模式匹配。match: Match a value to patterns.mod:定义模块。mod: Define a module.move:使闭包获取其所有捕获变量的所有权。move: Make a closure take ownership of all its captures.mut:表示引用、原始指针或模式绑定中的可变性。mut: Denote mutability in references, raw pointers, or pattern bindings.pub:表示结构体字段、impl块或模块中的公有可见性。pub: Denote public visibility in struct fields,implblocks, or modules.ref:通过引用绑定。ref: Bind by reference.return:从函数返回。return: Return from function.Self:正在定义或实现的类型的类型别名。Self: A type alias for the type we are defining or implementing.self:方法主体或当前模块。self: Method subject or current module.static:全局变量或在整个程序执行期间持续的生命周期。static: Global variable or lifetime lasting the entire program execution.struct:定义结构体。struct: Define a structure.super:当前模块的父模块。super: Parent module of the current module.trait:定义 trait。trait: Define a trait.true:布尔真字面值。true: Boolean true literal.type:定义类型别名或关联类型。type: Define a type alias or associated type.union:定义 联合体 (union);仅在联合体声明中使用时才是关键字。union: Define a union; is a keyword only when used in a union declaration.unsafe:表示不安全的代码、函数、Traits 或实现。unsafe: Denote unsafe code, functions, traits, or implementations.use:将符号引入作用域。use: Bring symbols into scope.where:表示约束类型的子句。where: Denote clauses that constrain a type.while:基于表达式结果的有条件循环。while: Loop conditionally based on the result of an expression.
保留供未来使用的关键字 (Keywords Reserved for Future Use)
Keywords Reserved for Future Use
以下关键字目前还没有任何功能,但被 Rust 保留用于潜在的未来用途:
The following keywords do not yet have any functionality but are reserved by Rust for potential future use:
abstractbecomeboxdofinalgenmacrooverrideprivtrytypeofunsizedvirtualyield
原始标识符 (Raw Identifiers)
Raw Identifiers
原始标识符 (Raw identifiers) 是允许你在通常不允许使用关键字的地方使用关键字的语法。你可以通过在关键字前加上前缀 r# 来使用原始标识符。
Raw identifiers are the syntax that lets you use keywords where they wouldn’t
normally be allowed. You use a raw identifier by prefixing a keyword with r#.
例如,match 是一个关键字。如果你尝试编译以下使用 match 作为名称的函数:
For example, match is a keyword. If you try to compile the following function
that uses match as its name:
文件名: src/main.rs
Filename: src/main.rs
fn match(needle: &str, haystack: &str) -> bool {
haystack.contains(needle)
}
你会得到这个错误:
you’ll get this error:
error: expected identifier, found keyword `match`
--> src/main.rs:4:4
|
4 | fn match(needle: &str, haystack: &str) -> bool {
| ^^^^^ expected identifier, found keyword
该错误显示你不能使用关键字 match 作为函数标识符。要将 match 用作函数名,你需要使用原始标识符语法,如下所示:
The error shows that you can’t use the keyword match as the function
identifier. To use match as a function name, you need to use the raw
identifier syntax, like this:
文件名: src/main.rs
Filename: src/main.rs
fn r#match(needle: &str, haystack: &str) -> bool {
haystack.contains(needle)
}
fn main() {
assert!(r#match("foo", "foobar"));
}
这段代码将编译通过而不会出现任何错误。请注意在函数定义的函数名上以及在 main 中调用该函数的地方都使用了 r# 前缀。
This code will compile without any errors. Note the r# prefix on the function
name in its definition as well as where the function is called in main.
原始标识符允许你使用任何你选择的单词作为标识符,即使该单词恰好是一个保留关键字。这给了我们选择标识符名称的更多自由,同时也让我们能够与使用非关键字语言编写的程序进行集成。此外,原始标识符还允许你使用与你的 crate 使用的不同 Rust 版本 (edition) 编写的库。例如,try 在 2015 版本中不是关键字,但在 2018、2021 和 2024 版本中是。如果你依赖一个使用 2015 版本编写且具有 try 函数的库,在更高版本的代码中调用该函数时,你需要使用原始标识符语法(在本例中为 r#try)。有关版本的更多信息,请参阅附录 E。
Raw identifiers allow you to use any word you choose as an identifier, even if
that word happens to be a reserved keyword. This gives us more freedom to choose
identifier names, as well as lets us integrate with programs written in a
language where these words aren’t keywords. In addition, raw identifiers allow
you to use libraries written in a different Rust edition than your crate uses.
For example, try isn’t a keyword in the 2015 edition but is in the 2018, 2021,
and 2024 editions. If you depend on a library that is written using the 2015
edition and has a try function, you’ll need to use the raw identifier syntax,
r#try in this case, to call that function from your code on later editions.
See Appendix E for more information on editions.