Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

附录 A:关键字

Appendix A: Keywords

以下列表包含了 Rust 语言中为当前或未来使用而保留的关键字。因此,它们不能被用作标识符(除非作为原始标识符,我们将在 “原始标识符” 部分进行讨论)。标识符 是函数、变量、参数、结构体字段、模块、crate、常量、宏、静态值、属性、类型、trait 或生命周期的名称。

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

以下是当前使用的关键字列表,并描述了它们的功能。

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 in use statements.

  • async: 返回一个 Future 而不是阻塞当前线程。

  • async: Return a Future instead of blocking the current thread.

  • await: 暂停执行直到 Future 的结果就绪。

  • await: Suspend execution until the result of a Future is 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: ifif let 控制流结构的备选分支。

  • else: Fallback for if and if let control 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,或指定高阶生命周期。

  • 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 of for loop 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, impl blocks, 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: Define a union; is a keyword only when used in a union declaration.

  • unsafe: 表示不安全的代码、函数、trait 或实现。

  • 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

以下关键字目前还没有任何功能,但被 Rust 保留以备未来可能的使用:

The following keywords do not yet have any functionality but are reserved by Rust for potential future use:

  • abstract
  • become
  • box
  • do
  • final
  • gen
  • macro
  • override
  • priv
  • try
  • typeof
  • unsized
  • virtual
  • yield

原始标识符

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

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

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.