Skip to content

Commit 2c1c42f

Browse files
committed
Add pub(super) and pub(self) to pub(restricted) docs
1 parent adf1673 commit 2c1c42f

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

src/visibility-and-privacy.md

+37-11
Original file line numberDiff line numberDiff line change
@@ -134,39 +134,65 @@ For a Rust program to pass the privacy checking pass, all paths must be valid
134134
accesses given the two rules above. This includes all use statements,
135135
expressions, types, etc.
136136

137-
## `pub(crate)` and `pub(in path)`
137+
## `pub(in path)`, `pub(crate)`, `pub(super)`, and `pub(self)`
138138

139139
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)`.
144148

145-
Here's an example using `pub(crate)` and `pub(in path)`:
149+
Here's an example:
146150

147151
```rust
148152
pub mod outer_mod {
149153
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
151158
pub(crate) fn crate_visible_fn() {}
152159

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() {}
155168
}
156-
fn foo() {
157-
inner_mod::crate_visible_fn();
169+
pub fn foo() {
158170
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();
159177
}
160178
}
161179

162180
fn bar() {
163181
// This function is still visible since we're in the same crate
164182
outer_mod::inner_mod::crate_visible_fn();
165183

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+
166188
// This function is no longer visible since we're outside of `outer_mod`
167189
// Error! `outer_mod_visible_fn` is private
168190
//outer_mod::inner_mod::outer_mod_visible_fn();
191+
192+
outer_mod::foo();
169193
}
194+
195+
fn main() { bar() }
170196
```
171197

172198
## Re-exporting and Visibility

0 commit comments

Comments
 (0)