11# Visibility and Privacy
22
3+ r[ vis]
4+
5+ r[ vis.syntax]
36> ** <sup >Syntax<sup >** \
47> _ Visibility_ :\
58>   ;  ;   ;  ; ` pub ` \
811>   ;  ; | ` pub ` ` ( ` ` super ` ` ) ` \
912>   ;  ; | ` pub ` ` ( ` ` in ` [ _ SimplePath_ ] ` ) `
1013
14+ r[ vis.intro]
1115These two terms are often used interchangeably, and what they are attempting to
1216convey is the answer to the question "Can this item be used at this location?"
1317
18+ r[ vis.name-hierarchy]
1419Rust's name resolution operates on a global hierarchy of namespaces. Each level
1520in the hierarchy can be thought of as some item. The items are one of those
1621mentioned above, but also include external crates. Declaring or defining a new
1722module can be thought of as inserting a new tree into the hierarchy at the
1823location of the definition.
1924
25+ r[ vis.privacy]
2026To control whether interfaces can be used across modules, Rust checks each use
2127of an item to see whether it should be allowed or not. This is where privacy
2228warnings are generated, or otherwise "you used a private item of another module
2329and weren't allowed to."
2430
31+ r[ vis.default]
2532By default, everything is * private* , with two exceptions: Associated
2633items in a ` pub ` Trait are public by default; Enum variants
2734in a ` pub ` enum are also public by default. When an item is declared as ` pub ` ,
@@ -44,6 +51,7 @@ pub enum State {
4451}
4552```
4653
54+ r[ vis.access]
4755With the notion of an item being either public or private, Rust allows item
4856accesses in two cases:
4957
@@ -78,8 +86,10 @@ explain, here's a few use cases and what they would entail:
7886
7987In the second case, it mentions that a private item "can be accessed" by the
8088current 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
8393function would mean that it is invoked. Additionally, path expressions and
8494import statements are considered to access an item in the sense that the
8595import/expression is only valid if the destination is in the current visibility
@@ -144,18 +154,30 @@ expressions, types, etc.
144154
145155## ` pub(in path) ` , ` pub(crate) ` , ` pub(super) ` , and ` pub(self) `
146156
157+ r[ vis.scoped]
158+
159+ r[ vis.scoped.intro]
147160In addition to public and private, Rust allows users to declare an item as
148161visible only within a given scope. The rules for ` pub ` restrictions are as
149162follows:
163+
164+ r[ vis.scoped.in]
150165- ` pub(in path) ` makes an item visible within the provided ` path ` .
151166 ` path ` must be a simple path which resolves to an ancestor module of the item whose visibility is being declared.
152167 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]
153170- ` pub(crate) ` makes an item visible within the current crate.
171+
172+ r[ vis.scoped.super]
154173- ` pub(super) ` makes an item visible to the parent module. This is equivalent
155174 to ` pub(in super) ` .
175+
176+ r[ vis.scoped.self]
156177- ` pub(self) ` makes an item visible to the current module. This is equivalent
157178to ` pub(in self) ` or not using ` pub ` at all.
158179
180+ r[ vis.scoped.edition2018]
159181> ** Edition differences** : Starting with the 2018 edition, paths for
160182> ` pub(in path) ` must start with ` crate ` , ` self ` , or ` super ` . The 2015 edition
161183> may also use paths starting with ` :: ` or modules from the crate root.
@@ -219,6 +241,9 @@ fn main() { bar() }
219241
220242## Re-exporting and Visibility
221243
244+ r[ vis.reexports]
245+
246+ r[ vis.reexports.intro]
222247Rust allows publicly re-exporting items through a ` pub use ` directive. Because
223248this is a public directive, this allows the item to be used in the current
224249module through the rules above. It essentially allows public access into the
@@ -239,6 +264,7 @@ mod implementation {
239264This means that any external crate referencing ` implementation::api::f ` would
240265receive a privacy violation, while the path ` api::f ` would be allowed.
241266
267+ r[ vis.reexports.private-item]
242268When re-exporting a private item, it can be thought of as allowing the "privacy
243269chain" being short-circuited through the reexport instead of passing through
244270the namespace hierarchy as it normally would.
0 commit comments