@@ -134,39 +134,65 @@ For a Rust program to pass the privacy checking pass, all paths must be valid
134
134
accesses given the two rules above. This includes all use statements,
135
135
expressions, types, etc.
136
136
137
- ## ` pub(crate) ` and ` pub(in path ) `
137
+ ## ` pub(in path) ` , ` pub( crate)` , ` pub(super) ` , and ` pub(self ) `
138
138
139
139
In addition to public and private, Rust allows users to declare an item as
140
- ` pub(crate) ` or ` pub(in path) ` . These restrictions make it possible to specify
141
- the exact scope of an item's visibility. As their names would suggest,
142
- ` pub(crate) ` makes an item public within the current crate, and ` pub(in path) `
143
- makes an item public within the specified path.
140
+ visible within a given scope. The rules for ` pub ` restrictions are as follows:
141
+ - ` pub(in path) ` makes an item visible within the provided ` path ` . ` path ` must
142
+ be a parent module of the item whose visibility is being declared.
143
+ - ` pub(crate) ` makes an item visible within the current crate.
144
+ - ` pub(super) ` makes an item visible to the parent module. This equivalent to
145
+ ` pub(in super) ` .
146
+ - ` pub(self) ` makes an item visible to the current module. This is equivalent
147
+ to ` pub(in self) ` .
144
148
145
- Here's an example using ` pub(crate) ` and ` pub(in path) ` :
149
+ Here's an example:
146
150
147
151
``` rust
148
152
pub mod outer_mod {
149
153
pub mod inner_mod {
150
- // This function is public to the entire crate
154
+ // This function is visible within `outer_mod`
155
+ pub (in outer_mod ) fn outer_mod_visible_fn () {}
156
+
157
+ // This function is visible to the entire crate
151
158
pub (crate ) fn crate_visible_fn () {}
152
159
153
- // This function is public within `outer_mod`
154
- pub (in outer_mod ) fn outer_mod_visible_fn () {}
160
+ // This function is visible within `outer_mod`
161
+ pub (super ) fn super_mod_visible_fn () {
162
+ // This function is visible since we're in the same `mod`
163
+ inner_mod_visible_fn ();
164
+ }
165
+
166
+ // This function is visible
167
+ pub (self ) fn inner_mod_visible_fn () {}
155
168
}
156
- fn foo () {
157
- inner_mod :: crate_visible_fn ();
169
+ pub fn foo () {
158
170
inner_mod :: outer_mod_visible_fn ();
171
+ inner_mod :: crate_visible_fn ();
172
+ inner_mod :: super_mod_visible_fn ();
173
+
174
+ // This function is no longer visible since we're outside of `inner_mod`
175
+ // Error! `inner_mod_visible_fn` is private
176
+ // inner_mod::inner_mod_visible_fn();
159
177
}
160
178
}
161
179
162
180
fn bar () {
163
181
// This function is still visible since we're in the same crate
164
182
outer_mod :: inner_mod :: crate_visible_fn ();
165
183
184
+ // This function is no longer visible since we're outside of `outer_mod`
185
+ // Error! `super_mod_visible_fn` is private
186
+ // outer_mod::inner_mod::super_mod_visible_fn();
187
+
166
188
// This function is no longer visible since we're outside of `outer_mod`
167
189
// Error! `outer_mod_visible_fn` is private
168
190
// outer_mod::inner_mod::outer_mod_visible_fn();
191
+
192
+ outer_mod :: foo ();
169
193
}
194
+
195
+ fn main () { bar () }
170
196
```
171
197
172
198
## Re-exporting and Visibility
0 commit comments