@@ -5,29 +5,18 @@ a namespace qualifier (`::`). If a path consists of only one component, it may
55refer to either an [ item] or a [ variable] in a local control
66scope. If a path has multiple components, it refers to an item.
77
8- [ item ] : items.html
9- [ variable ] : variables.html
10-
11- Every item has a _ canonical path_ within its crate, but the path naming an item
12- is only meaningful within a given crate. There is no global namespace across
13- crates; an item's canonical path merely identifies it within the crate.
14-
158Two examples of simple paths consisting of only identifier components:
169
1710``` rust,ignore
1811x;
1912x::y::z;
2013```
2114
22- Path components are usually [ identifiers] , but they may
23- also include angle-bracket-enclosed lists of type arguments. In
24- [ expression] context, the type argument list is given
25- after a ` :: ` namespace qualifier in order to disambiguate it from a
26- relational expression involving the less-than symbol (` < ` ). In type
27- expression context, the final namespace qualifier is omitted.
28-
29- [ identifiers ] : identifiers.html
30- [ expression ] : expressions.html
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.
3120
3221Two examples of paths with type arguments:
3322
@@ -103,3 +92,79 @@ mod a {
10392}
10493# fn main () {}
10594```
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
0 commit comments