x-i18n: generated_at: “2026-03-01T09:11:45Z” model: gemini-3-flash-preview provider: google-gemini-cli source_hash: bc33afa09cd57576dacd399d348950fa5bc8a3642818c1bc05ddbc088c0c5514 source_path: appendix-02-operators.md workflow: 16
附录 B:运算符与符号 (Appendix B: Operators and Symbols)
Appendix B: Operators and Symbols
本附录包含 Rust 语法的术语表,包括运算符以及单独出现或出现在路径、泛型、Trait 约束、宏、属性、注释、元组和括号上下文中的其他符号。
This appendix contains a glossary of Rust’s syntax, including operators and other symbols that appear by themselves or in the context of paths, generics, trait bounds, macros, attributes, comments, tuples, and brackets.
运算符 (Operators)
Operators
表 B-1 包含了 Rust 中的运算符,这些运算符在上下文中的示例,简要说明,以及该运算符是否可重载。如果一个运算符是可重载的,则列出了用于重载该运算符的相关 trait。
Table B-1 contains the operators in Rust, an example of how the operator would appear in context, a short explanation, and whether that operator is overloadable. If an operator is overloadable, the relevant trait to use to overload that operator is listed.
表 B-1:运算符 (Table B-1: Operators)
Table B-1: Operators
| 运算符 (Operator) | 示例 (Example) | 说明 (Explanation) | 是否可重载? (Overloadable?) |
|---|---|---|---|
! | ident!(...), ident!{...}, ident![...] | 宏展开 (Macro expansion) | |
! | !expr | 按位或逻辑取反 (Bitwise or logical complement) | Not |
!= | expr != expr | 不等比较 (Nonequality comparison) | PartialEq |
% | expr % expr | 算术取余 (Arithmetic remainder) | Rem |
%= | var %= expr | 算术取余并赋值 (Arithmetic remainder and assignment) | RemAssign |
& | &expr, &mut expr | 借用 (Borrow) | |
& | &type, &mut type, &'a type, &'a mut type | 借用指针类型 (Borrowed pointer type) | |
& | expr & expr | 按位与 (Bitwise AND) | BitAnd |
&= | var &= expr | 按位与并赋值 (Bitwise AND and assignment) | BitAndAssign |
&& | expr && expr | 短路逻辑与 (Short-circuiting logical AND) | |
* | expr * expr | 算术乘法 (Arithmetic multiplication) | Mul |
*= | var *= expr | 算术乘法并赋值 (Arithmetic multiplication and assignment) | MulAssign |
* | *expr | 解引用 (Dereference) | Deref |
* | *const type, *mut type | 原始指针 (Raw pointer) | |
+ | trait + trait, 'a + trait | 复合类型约束 (Compound type constraint) | |
+ | expr + expr | 算术加法 (Arithmetic addition) | Add |
+= | var += expr | 算术加法并赋值 (Arithmetic addition and assignment) | AddAssign |
, | expr, expr | 参数和元素分隔符 (Argument and element separator) | |
- | - expr | 算术取负 (Arithmetic negation) | Neg |
- | expr - expr | 算术减法 (Arithmetic subtraction) | Sub |
-= | var -= expr | 算术减法并赋值 (Arithmetic subtraction and assignment) | SubAssign |
-> | fn(...) -> type, |…| -> type | 函数和闭包返回类型 (Function and closure return type) | |
. | expr.ident | 字段访问 (Field access) | |
. | expr.ident(expr, ...) | 方法调用 (Method call) | |
. | expr.0, expr.1, and so on | 元组索引 (Tuple indexing) | |
.. | .., expr.., ..expr, expr..expr | 右开区间字面值 (Right-exclusive range literal) | PartialOrd |
..= | ..=expr, expr..=expr | 右闭区间字面值 (Right-inclusive range literal) | PartialOrd |
.. | ..expr | 结构体字面值更新语法 (Struct literal update syntax) | |
.. | variant(x, ..), struct_type { x, .. } | “以及其余部分”模式绑定 (“And the rest” pattern binding) | |
... | expr...expr | (已废弃,请改用 ..=)在模式中:闭区间模式 ((Deprecated, use ..= instead) In a pattern: inclusive range pattern) | |
/ | expr / expr | 算术除法 (Arithmetic division) | Div |
/= | var /= expr | 算术除法并赋值 (Arithmetic division and assignment) | DivAssign |
: | pat: type, ident: type | 约束 (Constraints) | |
: | ident: expr | 结构体字段初始化器 (Struct field initializer) | |
: | 'a: loop {...} | 循环标签 (Loop label) | |
; | expr; | 语句和项终止符 (Statement and item terminator) | |
; | [...; len] | 固定大小数组语法的一部分 (Part of fixed-size array syntax) | |
<< | expr << expr | 左移 (Left-shift) | Shl |
<<= | var <<= expr | 左移并赋值 (Left-shift and assignment) | ShlAssign |
< | expr < expr | 小于比较 (Less than comparison) | PartialOrd |
<= | expr <= expr | 小于或等于比较 (Less than or equal to comparison) | PartialOrd |
= | var = expr, ident = type | 赋值/等值 (Assignment/equivalence) | |
== | expr == expr | 相等比较 (Equality comparison) | PartialEq |
=> | pat => expr | match 臂语法的一部分 (Part of match arm syntax) | |
> | expr > expr | 大于比较 (Greater than comparison) | PartialOrd |
>= | expr >= expr | 大于或等于比较 (Greater than or equal to comparison) | PartialOrd |
>> | expr >> expr | 右移 (Right-shift) | Shr |
>>= | var >>= expr | 右移并赋值 (Right-shift and assignment) | ShrAssign |
@ | ident @ pat | 模式绑定 (Pattern binding) | |
^ | expr ^ expr | 按位异或 (Bitwise exclusive OR) | BitXor |
^= | var ^= expr | 按位异或并赋值 (Bitwise exclusive OR and assignment) | BitXorAssign |
| | pat | pat | 模式替代方案 (Pattern alternatives) | |
| | expr | expr | 按位或 (Bitwise OR) | BitOr |
|= | var |= expr | 按位或并赋值 (Bitwise OR and assignment) | BitOrAssign |
|| | expr || expr | 短路逻辑或 (Short-circuiting logical OR) | |
? | expr? | 错误传播 (Error propagation) |
非运算符符号 (Non-operator Symbols)
Non-operator Symbols
以下表格包含了所有不作为运算符使用的符号;也就是说,它们的行为不像函数或方法调用。
The following tables contain all symbols that don’t function as operators; that is, they don’t behave like a function or method call.
表 B-2 显示了单独出现且在多个位置有效的符号。
Table B-2 shows symbols that appear on their own and are valid in a variety of locations.
表 B-2:独立语法 (Table B-2: Stand-alone Syntax)
Table B-2: Stand-alone Syntax
| 符号 (Symbol) | 说明 (Explanation) |
|---|---|
'ident | 具名生命周期或循环标签 (Named lifetime or loop label) |
紧随其后的是 u8、i32、f64、usize 等的数字 (Digits immediately followed by u8, i32, f64, usize, and so on) | 特定类型的数值字面值 (Numeric literal of specific type) |
"..." | 字符串字面值 (String literal) |
r"...", r#"..."#, r##"..."##, 等 (and so on) | 原始字符串字面值;不处理转义字符 (Raw string literal; escape characters not processed) |
b"..." | 字节字符串字面值;构造字节数组而不是字符串 (Byte string literal; constructs an array of bytes instead of a string) |
br"...", br#"..."#, br##"..."##, 等 (and so on) | 原始字节字符串字面值;原始和字节字符串字面值的组合 (Raw byte string literal; combination of raw and byte string literal) |
'...' | 字符字面值 (Character literal) |
b'...' | ASCII 字节字面值 (ASCII byte literal) |
|…| expr | 闭包 (Closure) |
! | 用于发散函数的始终为空的底类型 (Always-empty bottom type for diverging functions) |
_ | “忽略”模式绑定;也用于提高整数字面值的可读性 (“Ignored” pattern binding; also used to make integer literals readable) |
表 B-3 显示了在通过模块层级结构指向某项的路径上下文中出现的符号。
Table B-3 shows symbols that appear in the context of a path through the module hierarchy to an item.
表 B-3:路径相关语法 (Table B-3: Path-Related Syntax)
Table B-3: Path-Related Syntax
| 符号 (Symbol) | 说明 (Explanation) |
|---|---|
ident::ident | 命名空间路径 (Namespace path) |
::path | 相对于 crate 根的路径(即显式的绝对路径) (Path relative to the crate root (that is, an explicitly absolute path)) |
self::path | 相对于当前模块的路径(即显式的相对路径) (Path relative to the current module (that is, an explicitly relative path)) |
super::path | 相对于当前模块父模块的路径 (Path relative to the parent of the current module) |
type::ident, <type as trait>::ident | 关联常量、函数和类型 (Associated constants, functions, and types) |
<type>::... | 无法直接命名的类型的关联项(例如 <&T>::...、<[T]>::... 等) (Associated item for a type that cannot be directly named (for example, <&T>::..., <[T]>::..., and so on)) |
trait::method(...) | 通过命名定义方法的 trait 来消除方法调用的歧义 (Disambiguating a method call by naming the trait that defines it) |
type::method(...) | 通过命名定义方法的类型来消除方法调用的歧义 (Disambiguating a method call by naming the type for which it’s defined) |
<type as trait>::method(...) | 通过命名 trait 和类型来消除方法调用的歧义 (Disambiguating a method call by naming the trait and type) |
表 B-4 显示了在利用泛型类型参数的上下文中出现的符号。
Table B-4 shows symbols that appear in the context of using generic type parameters.
表 B-4:泛型 (Table B-4: Generics)
Table B-4: Generics
| 符号 (Symbol) | 说明 (Explanation) |
|---|---|
path<...> | 在类型中指定泛型类型的参数(例如 Vec<u8>) (Specifies parameters to a generic type in a type (for example, Vec<u8>)) |
path::<...>, method::<...> | 在表达式中指定泛型类型、函数或方法的参数;通常被称为 turbofish(例如 "42".parse::<i32>()) (Specifies parameters to a generic type, function, or method in an expression; often referred to as turbofish (for example, "42".parse::<i32>())) |
fn ident<...> ... | 定义泛型函数 (Define generic function) |
struct ident<...> ... | 定义泛型结构体 (Define generic structure) |
enum ident<...> ... | 定义泛型枚举 (Define generic enumeration) |
impl<...> ... | 定义泛型实现 (Define generic implementation) |
for<...> type | 高阶生命周期约束 (Higher ranked lifetime bounds) |
type<ident=type> | 一个或多个关联类型具有特定赋值的泛型类型(例如 Iterator<Item=T>) (A generic type where one or more associated types have specific assignments (for example, Iterator<Item=T>)) |
表 B-5 显示了在用 trait 约束来限制泛型类型参数的上下文中出现的符号。
Table B-5 shows symbols that appear in the context of constraining generic type parameters with trait bounds.
表 B-5:Trait 约束 (Table B-5: Trait Bound Constraints)
Table B-5: Trait Bound Constraints
| 符号 (Symbol) | 说明 (Explanation) |
|---|---|
T: U | 泛型参数 T 被限制为实现了 U 的类型 (Generic parameter T constrained to types that implement U) |
T: 'a | 泛型类型 T 必须比生命周期 'a 存活得更久(意味着该类型不能传递地包含任何生命周期短于 'a 的引用) (Generic type T must outlive lifetime 'a (meaning the type cannot transitively contain any references with lifetimes shorter than 'a)) |
T: 'static | 泛型类型 T 除了 'static 引用外不包含任何借用引用 (Generic type T contains no borrowed references other than 'static ones) |
'b: 'a | 泛型生命周期 'b 必须比生命周期 'a 存活得更久 (Generic lifetime 'b must outlive lifetime 'a) |
T: ?Sized | 允许泛型类型参数是动态大小类型 (Allow generic type parameter to be a dynamically sized type) |
'a + trait, trait + trait | 复合类型约束 (Compound type constraint) |
表 B-6 显示了在调用或定义宏以及在项上指定属性的上下文中出现的符号。
Table B-6 shows symbols that appear in the context of calling or defining macros and specifying attributes on an item.
表 B-6:宏与属性 (Table B-6: Macros and Attributes)
Table B-6: Macros and Attributes
| 符号 (Symbol) | 说明 (Explanation) |
|---|---|
#[meta] | 外层属性 (Outer attribute) |
#![meta] | 内层属性 (Inner attribute) |
$ident | 宏替换 (Macro substitution) |
$ident:kind | 宏元变量 (Macro metavariable) |
$(...)... | 宏重复 (Macro repetition) |
ident!(...), ident!{...}, ident![...] | 宏调用 (Macro invocation) |
表 B-7 显示了创建注释的符号。
Table B-7 shows symbols that create comments.
表 B-7:注释 (Table B-7: Comments)
Table B-7: Comments
| 符号 (Symbol) | 说明 (Explanation) |
|---|---|
// | 行注释 (Line comment) |
//! | 内层行文档注释 (Inner line doc comment) |
/// | 外层行文档注释 (Outer line doc comment) |
/*...*/ | 块注释 (Block comment) |
/*!...*/ | 内层块文档注释 (Inner block doc comment) |
/**...*/ | 外层块文档注释 (Outer block doc comment) |
表 B-8 显示了使用圆括号的上下文。
Table B-8 shows the contexts in which parentheses are used.
表 B-8:圆括号 (Table B-8: Parentheses)
Table B-8: Parentheses
| 符号 (Symbol) | 说明 (Explanation) |
|---|---|
() | 空元组(又名 unit),既是字面值也是类型 (Empty tuple (aka unit), both literal and type) |
(expr) | 括号表达式 (Parenthesized expression) |
(expr,) | 单元素元组表达式 (Single-element tuple expression) |
(type,) | 单元素元组类型 (Single-element tuple type) |
(expr, ...) | 元组表达式 (Tuple expression) |
(type, ...) | 元组类型 (Tuple type) |
expr(expr, ...) | 函数调用表达式;也用于初始化元组结构体和元组枚举变体 (Function call expression; also used to initialize tuple structs and tuple enum variants) |
表 B-9 显示了使用花括号的上下文。
Table B-9 shows the contexts in which curly brackets are used.
表 B-9:花括号 (Table B-9: Curly Brackets)
Table B-9: Curly Brackets
| 上下文 (Context) | 说明 (Explanation) |
|---|---|
{...} | 块表达式 (Block expression) |
Type {...} | 结构体字面值 (Struct literal) |
表 B-10 显示了使用方括号的上下文。
Table B-10 shows the contexts in which square brackets are used.
表 B-10:方括号 (Table B-10: Square Brackets)
Table B-10: Square Brackets
| 上下文 (Context) | 说明 (Explanation) |
|---|---|
[...] | 数组字面值 (Array literal) |
[expr; len] | 包含 len 个 expr 副本的数组字面值 (Array literal containing len copies of expr) |
[type; len] | 包含 len 个 type 实例的数组类型 (Array type containing len instances of type) |
expr[expr] | 集合索引;可重载 (Index, IndexMut) (Collection indexing; overloadable (Index, IndexMut)) |
expr[..], expr[a..], expr[..b], expr[a..b] | 伪装成集合切片的集合索引,使用 Range、RangeFrom、RangeTo 或 RangeFull 作为“索引” (Collection indexing pretending to be collection slicing, using Range, RangeFrom, RangeTo, or RangeFull as the “index”) |