]>
Commit | Line | Data |
---|---|---|
8bb4bdeb XL |
1 | # Paths |
2 | ||
3 | A _path_ is a sequence of one or more path components _logically_ separated by | |
4 | a namespace qualifier (`::`). If a path consists of only one component, it may | |
5 | refer to either an [item] or a [variable] in a local control | |
6 | scope. If a path has multiple components, it refers to an item. | |
7 | ||
8bb4bdeb XL |
8 | Two examples of simple paths consisting of only identifier components: |
9 | ||
cc61c64b | 10 | ```rust,ignore |
8bb4bdeb XL |
11 | x; |
12 | x::y::z; | |
13 | ``` | |
14 | ||
abe05a73 XL |
15 | Path components are usually [identifiers], but they may also include |
16 | angle-bracket-enclosed lists of type arguments. In [expression] context, the | |
17 | type argument list is given after a `::` namespace qualifier in order to | |
18 | disambiguate it from a relational expression involving the less-than symbol | |
19 | (`<`). In type expression context, the final namespace qualifier is omitted. | |
8bb4bdeb XL |
20 | |
21 | Two examples of paths with type arguments: | |
22 | ||
23 | ```rust | |
24 | # struct HashMap<K, V>(K,V); | |
25 | # fn f() { | |
26 | # fn id<T>(t: T) -> T { t } | |
27 | type T = HashMap<i32,String>; // Type arguments used in a type expression | |
28 | let x = id::<i32>(10); // Type arguments used in a call expression | |
29 | # } | |
30 | ``` | |
31 | ||
32 | Paths can be denoted with various leading qualifiers to change the meaning of | |
33 | how it is resolved: | |
34 | ||
35 | * Paths starting with `::` are considered to be global paths where the | |
36 | components of the path start being resolved from the crate root. Each | |
37 | identifier in the path must resolve to an item. | |
38 | ||
39 | ```rust | |
40 | mod a { | |
41 | pub fn foo() {} | |
42 | } | |
43 | mod b { | |
44 | pub fn foo() { | |
45 | ::a::foo(); // call a's foo function | |
46 | } | |
47 | } | |
48 | # fn main() {} | |
49 | ``` | |
50 | ||
51 | * Paths starting with the keyword `super` begin resolution relative to the | |
52 | parent module. Each further identifier must resolve to an item. | |
53 | ||
54 | ```rust | |
55 | mod a { | |
56 | pub fn foo() {} | |
57 | } | |
58 | mod b { | |
59 | pub fn foo() { | |
60 | super::a::foo(); // call a's foo function | |
61 | } | |
62 | } | |
63 | # fn main() {} | |
64 | ``` | |
65 | ||
66 | * Paths starting with the keyword `self` begin resolution relative to the | |
67 | current module. Each further identifier must resolve to an item. | |
68 | ||
69 | ```rust | |
70 | fn foo() {} | |
71 | fn bar() { | |
72 | self::foo(); | |
73 | } | |
74 | # fn main() {} | |
75 | ``` | |
76 | ||
77 | Additionally keyword `super` may be repeated several times after the first | |
78 | `super` or `self` to refer to ancestor modules. | |
79 | ||
80 | ```rust | |
81 | mod a { | |
82 | fn foo() {} | |
83 | ||
84 | mod b { | |
85 | mod c { | |
86 | fn foo() { | |
87 | super::super::foo(); // call a's foo function | |
88 | self::super::super::foo(); // call a's foo function | |
89 | } | |
90 | } | |
91 | } | |
92 | } | |
93 | # fn main() {} | |
94 | ``` | |
abe05a73 XL |
95 | |
96 | ## Canonical paths | |
97 | ||
98 | Items defined in a module or implementation have a *canonical path* that | |
99 | corresponds to where within its crate it is defined. All other paths to these | |
100 | items are aliases. The canonical path is defined as a *path prefix* appended by | |
101 | the path component the item itself defines. | |
102 | ||
103 | [Implementations] and [use declarations] do not have canonical paths, although | |
104 | the items that implementations define do have them. Items defined in | |
105 | block expressions do not have canonical paths. Items defined in a module that | |
106 | does not have a canonical path do not have a canonical path. Associated items | |
107 | defined in an implementation that refers to an item without a canonical path, | |
108 | e.g. as the implementing type, the trait being implemented, a type parameter or | |
109 | bound on a type parameter, do not have canonical paths. | |
110 | ||
111 | The path prefix for modules is the canonical path to that module. For bare | |
112 | implementations, it is the canonical path of the item being implemented | |
113 | surrounded by angle (`<>`) brackets. For trait implementations, it is the | |
114 | canonical path of the item being implemented followed by `as` followed by the | |
115 | canonical path to the trait all surrounded in angle (`<>`) brackets. | |
116 | ||
117 | The canonical path is only meaningful within a given crate. There is no global | |
118 | namespace across crates; an item's canonical path merely identifies it within | |
119 | the crate. | |
120 | ||
121 | ```rust | |
122 | // Comments show the canonical path of the item. | |
123 | ||
124 | mod a { // ::a | |
125 | pub struct Struct; // ::a::Struct | |
126 | ||
127 | pub trait Trait { // ::a::Trait | |
128 | fn f(&self); // a::Trait::f | |
129 | } | |
130 | ||
131 | impl Trait for Struct { | |
132 | fn f(&self) {} // <::a::Struct as ::a::Trait>::f | |
133 | } | |
134 | ||
135 | impl Struct { | |
136 | fn g(&self) {} // <::a::Struct>::g | |
137 | } | |
138 | } | |
139 | ||
140 | mod without { // ::without | |
141 | fn canonicals() { // ::without::canonicals | |
142 | struct OtherStruct; // None | |
143 | ||
144 | trait OtherTrait { // None | |
145 | fn g(&self); // None | |
146 | } | |
147 | ||
148 | impl OtherTrait for OtherStruct { | |
149 | fn g(&self) {} // None | |
150 | } | |
151 | ||
152 | impl OtherTrait for ::a::Struct { | |
153 | fn g(&self) {} // None | |
154 | } | |
155 | ||
156 | impl ::a::Trait for OtherStruct { | |
157 | fn f(&self) {} // None | |
158 | } | |
159 | } | |
160 | } | |
161 | ||
162 | # fn main() {} | |
163 | ``` | |
164 | [item]: items.html | |
165 | [variable]: variables.html | |
166 | [identifiers]: identifiers.html | |
167 | [expression]: expressions.html | |
168 | [implementations]: items/implementations.html | |
169 | [modules]: items/modules.html | |
170 | [use declarations]: items/use-declarations.html |