04-路径
7 分钟阅读
译文 · 基于 The Rust Reference
r[paths]
路径
r[paths.intro]
路径(path)是由一个或多个路径段(path segment)组成的序列,各段之间以 :: 记号分隔。路径用于引用条目、值、类型、宏以及属性。
以下是仅由标识符段构成的简单路径的两个例子:
| |
路径的种类
r[paths.simple]
简单路径
r[paths.simple.syntax]
SimplePath ->
`::`? SimplePathSegment (`::` SimplePathSegment)*
SimplePathSegment ->
IDENTIFIER | `super` | `self` | `crate` | `$crate`
r[paths.simple.intro]
简单路径用于可见性标记、属性、宏以及 use 条目中。例如:
| |
r[paths.expr]
表达式中的路径
r[paths.expr.syntax]
PathInExpression ->
`::`? PathExprSegment (`::` PathExprSegment)*
PathExprSegment ->
PathIdentSegment (`::` GenericArgs)?
PathIdentSegment ->
IDENTIFIER | `super` | `self` | `Self` | `crate` | `$crate`
GenericArgs ->
`<` GenericArgList? `>`
| `(` TypeList? `)` (`->` TypeNoBounds)?
GenericArgList ->
( GenericArg `,` )* GenericArg `,`?
TypeList ->
( Type `,` )* Type `,`?
GenericArg ->
Lifetime | Type | GenericArgsConst | GenericArgsBinding | GenericArgsBounds
GenericArgsConst ->
BlockExpression
| LiteralExpression
| `-` LiteralExpression
| SimplePathSegment
GenericArgsBinding ->
TypePathSegment `=` Type
GenericArgsBounds ->
TypePathSegment `:` Bounds?
r[paths.expr.intro] 表达式中的路径允许指定带有泛型参数的路径。它们用于表达式和模式中的多种位置。
r[paths.expr.turbofish]
在泛型参数的起始 < 之前必须使用 :: 记号,以避免与小于运算符产生歧义。这种写法俗称 “turbofish” 语法。
| |
r[paths.expr.argument-order] 泛型参数的顺序受限:先是生命周期参数,然后是类型参数,再是常量参数,最后是等式约束。
r[paths.expr.complex-const-params] 常量参数必须用花括号括起,除非它们是字面量、推断常量,或单段路径。推断常量不得用花括号括起。
| |
| |
注意 在泛型参数列表中,推断常量按[推断类型][InferredType]进行解析,但在语义上会被当作一种单独的常量泛型参数。
r[paths.expr.impl-trait-params]
与 impl Trait 类型对应的合成类型参数是隐式的,不能显式指定。
r[paths.qualified]
限定路径
r[paths.qualified.syntax]
QualifiedPathInExpression -> QualifiedPathType (`::` PathExprSegment)+
QualifiedPathType -> `<` Type (`as` TypePath)? `>`
QualifiedPathInType -> QualifiedPathType (`::` TypePathSegment)+
r[paths.qualified.intro] 完全限定路径用于消除特质实现中路径的歧义,以及指定规范路径。在类型说明中使用时,它支持下文给出的类型语法。
| |
r[paths.type]
类型中的路径
r[paths.type.syntax]
TypePath -> `::`? TypePathSegment (`::` TypePathSegment)*
TypePathSegment -> PathIdentSegment (`::`? GenericArgs)?
r[paths.type.intro] 类型路径用于类型定义、特质约束以及限定路径中。
r[paths.type.turbofish]
虽然允许在泛型参数之前使用 :: 记号,但并不强制要求,因为这里不像 [PathInExpression] 那样存在歧义。
| |
r[paths.qualifiers]
路径限定符
路径可以带有多种前导限定符,以改变其解析方式。
注意
use声明对self、super、crate和$crate有额外的行为和限制。
r[paths.qualifiers.global-root]
::
r[paths.qualifiers.global-root.intro]
以 :: 开头的路径被视为全局路径(global path),其各段的解析起点因版本而异。路径中的每个标识符都必须解析到某个条目。
r[paths.qualifiers.global-root.edition2018]
[!EDITION-2018] 在 2015 版中,标识符从“crate 根”(2018 版中的
crate::)开始解析;该根包含多种不同的条目,包括外部 crate、诸如std或core之类的默认 crate,以及 crate 顶层的条目(包括use导入)。从 2018 版开始,以
::开头的路径从外部 prelude 中的 crate 开始解析。也就是说,其后必须跟某个 crate 的名称。
| |
| |
r[paths.qualifiers.mod-self]
self
r[paths.qualifiers.mod-self.intro]
self 使路径相对于当前模块进行解析。
r[paths.qualifiers.mod-self.restriction]
self 只能用作路径的第一段(且前面没有 ::),或用作最后一段(前面带有 ::)。
r[paths.qualifiers.mod-self.trailing]
当 self 作为路径的最后一段出现时,它引用由前一段命名的实体。前面的路径必须解析为模块、枚举或特质。
| |
| |
注意 关于
use声明中self的额外规则,参见 [items.use.self]。
r[paths.qualifiers.self-pat]
在方法体中,仅由单个 self 段构成的路径会解析为该方法的 self 参数。
| |
r[paths.qualifiers.type-self]
Self
r[paths.qualifiers.type-self.intro]
大写的 Self 用于引用当前正在实现或定义的类型。它可以用于以下情形:
r[paths.qualifiers.type-self.trait]
- 在特质定义中,它引用实现该特质的类型。
r[paths.qualifiers.type-self.impl]
r[paths.qualifiers.type-self.type]
r[paths.qualifiers.type-self.scope]
Self 的作用域行为类似于泛型参数;更多细节参见 Self 作用域 一节。
r[paths.qualifiers.type-self.allowed-positions]
Self 只能用作第一段,且前面不能有 ::。
r[paths.qualifiers.type-self.no-generics]
Self 路径不能包含泛型参数(例如 Self::<i32>)。
| |
r[paths.qualifiers.super]
super
r[paths.qualifiers.super.intro]
路径中的 super 解析为父模块。
r[paths.qualifiers.super.allowed-positions]
它只能用于路径的前导段,可能出现在初始的 self 段之后。
| |
r[paths.qualifiers.super.repetition]
在第一个 super 或 self 之后,可以重复多次使用 super,以引用祖先模块。
| |
r[paths.qualifiers.crate]
crate
r[paths.qualifiers.crate.intro]
crate 使路径相对于当前 crate 进行解析。
r[paths.qualifiers.crate.allowed-positions]
crate 只能用作第一段,且前面不能有 ::。
| |
r[paths.qualifiers.macro-crate]
$crate
r[paths.qualifiers.macro-crate.allowed-positions]
$crate 仅用于宏转写器中,并且只能用作第一段,且前面不能有 ::。
r[paths.qualifiers.macro-crate.hygiene]
无论宏在哪个 crate 中被调用,$crate 都会展开为一条路径,用于访问定义该宏的 crate 顶层的条目。
| |
r[paths.canonical]
规范路径
r[paths.canonical.intro] 在模块或实现中定义的每个条目都有一条规范路径(canonical path),对应于它在其 crate 内的定义位置。
r[paths.canonical.alias] 指向这些条目的所有其他路径都是别名。
r[paths.canonical.def] 规范路径定义为路径前缀(path prefix)再加上该条目自身定义的路径段。
r[paths.canonical.non-canonical] 实现和 use 声明没有规范路径,尽管实现中定义的条目有规范路径。在块表达式中定义的条目没有规范路径。在没有规范路径的模块中定义的条目也没有规范路径。在实现中定义的关联条目,若该实现所引用的条目没有规范路径——例如作为被实现的类型、被实现的特质、类型参数或类型参数上的约束——则这些关联条目也没有规范路径。
r[paths.canonical.module-prefix] 模块的路径前缀是该模块的规范路径。
r[paths.canonical.bare-impl-prefix]
对于裸实现,路径前缀是被实现条目的规范路径,并用尖括号(<>)括起。
r[paths.canonical.trait-impl-prefix]
对于特质实现,路径前缀是被实现条目的规范路径,后跟 as,再跟特质的规范路径,整体用尖括号(<>)括起。
r[paths.canonical.local-canonical-path] 规范路径仅在给定 crate 内有意义。跨 crate 不存在全局命名空间;条目的规范路径仅标识它在该 crate 内的身份。
| |