In Rust, who may use an item marked pub(crate), pub(super) or pub(in path), and what can these restrictions not override?
answer
- restrictions, not grants
- parent, crate, or a named ancestor
- the path must be an ancestor
- every module on the way
- E0742 and E0364
basics
~20 spub(crate) opens an item to the whole crate, pub(super) to the parent module, and pub(in path) to a named ancestor module. They only narrow pub: the path must be an ancestor (E0742), and every module on the way must still be visible.
solid answer
~40 sEach form names the module whose subtree may use the item. `pub(super)` is the parent module (the same as `pub(in super)`), `pub(in crate::station)` is any ancestor written as a path starting with `crate`, `self` or `super`, `pub(crate)` is the crate root and so the whole crate, and `pub(self)` is the same as private. They are ceilings, not grants: naming a module that is not an ancestor is E0742, and a private module on the path still blocks access, so `station::sensor::raw_kelvin()` is E0603 when `sensor` is private even though `raw_kelvin` is `pub(crate)`. Code outside the scope gets E0603, and `pub use` cannot export a `pub(crate)` item past the crate (E0364). `pub(crate)` is the everyday choice for a library's internals; `pub(super)` fits a helper only the parent module calls.
code
rust · 23 linesmod station {
pub mod sensor {
pub(super) fn calibrate() -> f64 { 0.25 } // visible in `station` and below
pub(in crate::station) fn offset() -> f64 { 0.1 } // same scope, spelled out
pub(crate) fn raw_kelvin() -> f64 { 293.0 } // visible anywhere in this crate
pub(self) fn scratch() -> f64 { 0.0 } // same as no `pub` at all
pub fn read() -> f64 {
raw_kelvin() + calibrate() + offset() + scratch()
}
}
pub fn self_test() -> f64 {
sensor::calibrate() + sensor::offset()
}
}
fn main() {
println!("{}", station::sensor::raw_kelvin()); // ok: pub(crate)
println!("{}", station::sensor::read());
println!("{}", station::self_test());
// station::sensor::calibrate(); // error[E0603]: function `calibrate` is private
}go deeper
Recall the three spellings and their reach: pub(super) the parent, pub(crate) the whole crate, pub(in path) a named ancestor, and that pub(self) is just private.
Explain that each is a ceiling checked with the normal path rule: the path must be an ancestor (E0742), and a private module in between still blocks access with E0603.
Use pub(crate) as the default for library internals so rustc blocks accidental export with E0364, and reach for pub(super) when a helper should stay inside one subsystem.
Treat these modifiers as documentation the compiler checks: a reviewer who sees pub(crate) knows the change cannot break another crate, which keeps refactoring inside a crate cheap.
## Five spellings of visibility Between private and fully public, Rust lets you name **which module's subtree** may use an item. The Reference lists the grammar as `pub`, `pub(crate)`, `pub(self)`, `pub(super)` and `pub(in SimplePath)`: | Modifier on an item in `crate::station::sensor` | Usable in | Typical use | |---|---|---| | none, or `pub(self)` | `sensor` and its descendants | the default for helpers | | `pub(super)` (same as `pub(in super)`) | `station` and everything under it | a helper the parent module calls | | `pub(in crate::station)` | the same subtree, spelled as a path | naming a grandparent or higher ancestor | | `pub(crate)` | the whole crate | library internals shared across modules | | `pub` | anywhere the path is reachable, including other crates | the crate's interface | `pub(self)` exists for completeness; the Reference calls it equivalent to `pub(in self)` or to writing no `pub` at all, so it is rarely written by hand. ## Restrictions narrow, they never widen The Reference is explicit that this syntax *only adds another restriction*. In practice that comes down to a short list of rules rustc enforces: 1. **The path must be an ancestor.** `pub(in path)` must resolve to an ancestor module of the item. `pub(in crate::sea) struct Shark;` written at the crate root is rejected with E0742, "visibilities can only be restricted to ancestor modules", because `sea` does not contain `Shark`. 2. **Modules only, named directly.** Each identifier in the path must refer to a module itself, not to a name brought in by `use`. 3. **Edition rule.** Since the 2018 edition the path must start with `crate`, `self` or `super`; the 2015 edition also accepted paths starting with `::` or a root module name. 4. **The parent chain still applies.** A restriction does not guarantee the item is visible everywhere in the named scope: every module between the user and the item must be visible as well. With `mod station { mod sensor { pub(crate) fn raw_kelvin() {} } }`, calling `station::sensor::raw_kelvin()` from `main` fails with E0603 "module `sensor` is private". 5. **Re-exports cannot widen it.** `pub use` of a `pub(crate)` item is E0364, "is only public within the crate, and cannot be re-exported outside". A `pub(crate) use` is fine. ## The station and its sensor The example below puts four helpers in `station::sensor`. `calibrate` is `pub(super)`, so `station::self_test` can call it, but `main`, one level further up, gets E0603 "function `calibrate` is private". `offset` is `pub(in crate::station)`, which grants exactly the same scope as `pub(super)` here, written as an explicit path. `raw_kelvin` is `pub(crate)` and `main` calls it directly, because both `station` and `sensor` are reachable from the crate root in this example (the `pub mod` matters). `scratch` is `pub(self)` and so is private. Note what the error says: code outside a restricted scope is told the item is **private**, the same E0603 a fully private item produces. From the outside, "visible elsewhere" and "private" look identical. ## Choosing between them - **`pub(crate)`** is the workhorse for libraries: shared internals that several modules call but other crates must not see. The compiler then guarantees that changing them cannot break a downstream crate, and blocks an accidental `pub use`. - **`pub(super)`** suits a submodule that exists to serve its parent, such as a parser split into `lexer` and `tokens` where only the parser itself calls the lexer's helpers. - **`pub(in crate::path)`** is for a deeper tree where a helper should stay inside one subsystem, more than one level up but short of the whole crate. It is uncommon; reaching for it often means the module tree wants reshaping. - **Plain `pub`** inside a private module, with nothing re-exporting it, behaves for other crates exactly like `pub(crate)`, but says less to a reader. Spelling the real reach is clearer. ## Errors you will meet - **E0603** a restricted item used from outside its scope, or any private module on the path; - **E0742** a `pub(in path)` naming a module that is not an ancestor; - **E0364** a `pub use` trying to re-export a restricted item more widely; - **E0449** a visibility written where none is allowed, such as on an enum variant.
- Can a library re-export a pub(crate) function from its root with pub use?No. A re-export cannot be wider than the item, so `pub use convert::fahrenheit_to_kelvin;` on a `pub(crate)` function is E0364, 'is only public within the crate, and cannot be re-exported outside'. `pub(crate) use` works; if the function really belongs to the API, change the item itself to `pub`.
- Why is `pub(in crate::sea) struct Shark;` written at the crate root rejected?A restriction may only name the item's own ancestors, and `sea` does not contain the crate root, where `Shark` is declared. rustc reports E0742, 'visibilities can only be restricted to ancestor modules'. Move `Shark` inside `sea`, or name a module that actually contains it, such as `crate`.
- Does pub(super) on an item in `station::sensor` let the crate root use it?No. `pub(super)` means the parent module, `station`, and that module's descendants. The crate root is the grandparent, so it gets E0603 as if the item were private. Widen to `pub(crate)`, or have `station` expose a public wrapper.
- pub(crate) makes an item callable anywhere in the crate even behind a private module.
- pub(super) exposes the item to every ancestor module up to the crate root.
- pub(in path) can name any module, such as a sibling, to share an item with it.
- A pub(crate) item can be made part of the public API with a pub use at the root.
- pub(self) is a separate level between private and pub(super).