Skip to content

Commit dd86fed

Browse files
authored
Merge pull request #481 from Speedy-Consoles/master
Clarify pub(restricted) a bit
2 parents e8f420d + 495efdb commit dd86fed

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/visibility-and-privacy.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,15 @@ expressions, types, etc.
145145
## `pub(in path)`, `pub(crate)`, `pub(super)`, and `pub(self)`
146146

147147
In addition to public and private, Rust allows users to declare an item as
148-
visible within a given scope. The rules for `pub` restrictions are as follows:
148+
visible only within a given scope. The rules for `pub` restrictions are as
149+
follows:
149150
- `pub(in path)` makes an item visible within the provided `path`. `path` must
150151
be a parent module of the item whose visibility is being declared.
151152
- `pub(crate)` makes an item visible within the current crate.
152153
- `pub(super)` makes an item visible to the parent module. This is equivalent
153154
to `pub(in super)`.
154155
- `pub(self)` makes an item visible to the current module. This is equivalent
155-
to `pub(in self)`.
156+
to `pub(in self)` or not using `pub` at all.
156157

157158
> **Edition Differences**: Starting with the 2018 edition, paths for
158159
> `pub(in path)` must start with `crate`, `self`, or `super`. The 2015 edition
@@ -177,7 +178,8 @@ pub mod outer_mod {
177178
inner_mod_visible_fn();
178179
}
179180

180-
// This function is visible
181+
// This function is visible only within `inner_mod`,
182+
// which is the same as leaving it private.
181183
pub(self) fn inner_mod_visible_fn() {}
182184
}
183185
pub fn foo() {
@@ -209,6 +211,11 @@ fn bar() {
209211
fn main() { bar() }
210212
```
211213

214+
> **Note:** This syntax only adds another restriction to the visibility of an
215+
> item. It does not guarantee that the item is visible within all parts of the
216+
> specified scope. To access an item, all of its parent items up to the
217+
> current scope must still be visible as well.
218+
212219
## Re-exporting and Visibility
213220

214221
Rust allows publicly re-exporting items through a `pub use` directive. Because

0 commit comments

Comments
 (0)