]> git.proxmox.com Git - rustc.git/blame - src/doc/reference/src/paths.md
New upstream version 1.23.0+dfsg1
[rustc.git] / src / doc / reference / src / paths.md
CommitLineData
8bb4bdeb
XL
1# Paths
2
3A _path_ is a sequence of one or more path components _logically_ separated by
4a namespace qualifier (`::`). If a path consists of only one component, it may
5refer to either an [item] or a [variable] in a local control
6scope. If a path has multiple components, it refers to an item.
7
8bb4bdeb
XL
8Two examples of simple paths consisting of only identifier components:
9
cc61c64b 10```rust,ignore
8bb4bdeb
XL
11x;
12x::y::z;
13```
14
abe05a73
XL
15Path components are usually [identifiers], but they may also include
16angle-bracket-enclosed lists of type arguments. In [expression] context, the
17type argument list is given after a `::` namespace qualifier in order to
18disambiguate it from a relational expression involving the less-than symbol
19(`<`). In type expression context, the final namespace qualifier is omitted.
8bb4bdeb
XL
20
21Two 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 }
27type T = HashMap<i32,String>; // Type arguments used in a type expression
28let x = id::<i32>(10); // Type arguments used in a call expression
29# }
30```
31
32Paths can be denoted with various leading qualifiers to change the meaning of
33how 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
40mod a {
41 pub fn foo() {}
42}
43mod 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
55mod a {
56 pub fn foo() {}
57}
58mod 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
70fn foo() {}
71fn bar() {
72 self::foo();
73}
74# fn main() {}
75```
76
77Additionally keyword `super` may be repeated several times after the first
78`super` or `self` to refer to ancestor modules.
79
80```rust
81mod 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
98Items defined in a module or implementation have a *canonical path* that
99corresponds to where within its crate it is defined. All other paths to these
100items are aliases. The canonical path is defined as a *path prefix* appended by
101the path component the item itself defines.
102
103[Implementations] and [use declarations] do not have canonical paths, although
104the items that implementations define do have them. Items defined in
105block expressions do not have canonical paths. Items defined in a module that
106does not have a canonical path do not have a canonical path. Associated items
107defined in an implementation that refers to an item without a canonical path,
108e.g. as the implementing type, the trait being implemented, a type parameter or
109bound on a type parameter, do not have canonical paths.
110
111The path prefix for modules is the canonical path to that module. For bare
112implementations, it is the canonical path of the item being implemented
113surrounded by angle (`<>`) brackets. For trait implementations, it is the
114canonical path of the item being implemented followed by `as` followed by the
115canonical path to the trait all surrounded in angle (`<>`) brackets.
116
117The canonical path is only meaningful within a given crate. There is no global
118namespace across crates; an item's canonical path merely identifies it within
119the crate.
120
121```rust
122// Comments show the canonical path of the item.
123
124mod 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
140mod 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