1
1
# Visibility and Privacy
2
2
3
+ r[ vis]
4
+
5
+ r[ vis.syntax]
3
6
> ** <sup >Syntax<sup >** \
4
7
> _ Visibility_ :\
5
8
>   ;  ;   ;  ; ` pub ` \
8
11
>   ;  ; | ` pub ` ` ( ` ` super ` ` ) ` \
9
12
>   ;  ; | ` pub ` ` ( ` ` in ` [ _ SimplePath_ ] ` ) `
10
13
14
+ r[ vis.intro]
11
15
These two terms are often used interchangeably, and what they are attempting to
12
16
convey is the answer to the question "Can this item be used at this location?"
13
17
18
+ r[ vis.name-hierarchy]
14
19
Rust's name resolution operates on a global hierarchy of namespaces. Each level
15
20
in the hierarchy can be thought of as some item. The items are one of those
16
21
mentioned above, but also include external crates. Declaring or defining a new
17
22
module can be thought of as inserting a new tree into the hierarchy at the
18
23
location of the definition.
19
24
25
+ r[ vis.privacy]
20
26
To control whether interfaces can be used across modules, Rust checks each use
21
27
of an item to see whether it should be allowed or not. This is where privacy
22
28
warnings are generated, or otherwise "you used a private item of another module
23
29
and weren't allowed to."
24
30
31
+ r[ vis.default]
25
32
By default, everything is * private* , with two exceptions: Associated
26
33
items in a ` pub ` Trait are public by default; Enum variants
27
34
in a ` pub ` enum are also public by default. When an item is declared as ` pub ` ,
@@ -44,6 +51,7 @@ pub enum State {
44
51
}
45
52
```
46
53
54
+ r[ vis.access]
47
55
With the notion of an item being either public or private, Rust allows item
48
56
accesses in two cases:
49
57
@@ -78,8 +86,10 @@ explain, here's a few use cases and what they would entail:
78
86
79
87
In the second case, it mentions that a private item "can be accessed" by the
80
88
current module and its descendants, but the exact meaning of accessing an item
81
- depends on what the item is. Accessing a module, for example, would mean
82
- looking inside of it (to import more items). On the other hand, accessing a
89
+ depends on what the item is.
90
+
91
+ r[ vis.usage]
92
+ Accessing a module, for example, would mean looking inside of it (to import more items). On the other hand, accessing a
83
93
function would mean that it is invoked. Additionally, path expressions and
84
94
import statements are considered to access an item in the sense that the
85
95
import/expression is only valid if the destination is in the current visibility
@@ -144,18 +154,30 @@ expressions, types, etc.
144
154
145
155
## ` pub(in path) ` , ` pub(crate) ` , ` pub(super) ` , and ` pub(self) `
146
156
157
+ r[ vis.scoped]
158
+
159
+ r[ vis.scoped.intro]
147
160
In addition to public and private, Rust allows users to declare an item as
148
161
visible only within a given scope. The rules for ` pub ` restrictions are as
149
162
follows:
163
+
164
+ r[ vis.scoped.in]
150
165
- ` pub(in path) ` makes an item visible within the provided ` path ` .
151
166
` path ` must be a simple path which resolves to an ancestor module of the item whose visibility is being declared.
152
167
Each identifier in ` path ` must refer directly to a module (not to a name introduced by a ` use ` statement).
168
+
169
+ r[ vis.scoped.crate]
153
170
- ` pub(crate) ` makes an item visible within the current crate.
171
+
172
+ r[ vis.scoped.super]
154
173
- ` pub(super) ` makes an item visible to the parent module. This is equivalent
155
174
to ` pub(in super) ` .
175
+
176
+ r[ vis.scoped.self]
156
177
- ` pub(self) ` makes an item visible to the current module. This is equivalent
157
178
to ` pub(in self) ` or not using ` pub ` at all.
158
179
180
+ r[ vis.scoped.edition2018]
159
181
> ** Edition differences** : Starting with the 2018 edition, paths for
160
182
> ` pub(in path) ` must start with ` crate ` , ` self ` , or ` super ` . The 2015 edition
161
183
> may also use paths starting with ` :: ` or modules from the crate root.
@@ -219,6 +241,9 @@ fn main() { bar() }
219
241
220
242
## Re-exporting and Visibility
221
243
244
+ r[ vis.reexports]
245
+
246
+ r[ vis.reexports.intro]
222
247
Rust allows publicly re-exporting items through a ` pub use ` directive. Because
223
248
this is a public directive, this allows the item to be used in the current
224
249
module through the rules above. It essentially allows public access into the
@@ -239,6 +264,7 @@ mod implementation {
239
264
This means that any external crate referencing ` implementation::api::f ` would
240
265
receive a privacy violation, while the path ` api::f ` would be allowed.
241
266
267
+ r[ vis.reexports.private-item]
242
268
When re-exporting a private item, it can be thought of as allowing the "privacy
243
269
chain" being short-circuited through the reexport instead of passing through
244
270
the namespace hierarchy as it normally would.
0 commit comments