@@ -5,10 +5,6 @@ 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- Every item has a _ canonical path_ within its crate, but the path naming an item
9- is only meaningful within a given crate. There is no global namespace across
10- crates; an item's canonical path merely identifies it within the crate.
11-
128Two examples of simple paths consisting of only identifier components:
139
1410``` rust,ignore
@@ -97,7 +93,78 @@ mod a {
9793# fn main () {}
9894```
9995
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+ ```
100164[ item ] : items.html
101165[ variable ] : variables.html
102166[ identifiers ] : identifiers.html
103- [ expression ] : expressions.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