skip to content

In a Rust library, when is a pub item not part of the crate's public API, and what does the unreachable_pub lint flag?

level: seniorimportance: nice to knowfreq 20%

answer

  1. pub is not the same as exported
  2. reachable from the crate root
  3. effective visibility
  4. off unless you turn it on
  5. its help suggests pub(crate)

basics

~20 s

A pub item in a private module that is neither re-exported nor reachable through a public signature is effectively pub(crate). The allow-by-default unreachable_pub lint flags such items and suggests pub(crate), so the source states the API's real reach.

solid answer

~40 s

Other crates reach an item through public modules all the way down, a `pub use` re-export, or a public signature that hands them a value of its type. So in `mod convert { pub fn celsius_to_kelvin(..) }`, with `convert` private and nothing re-exporting the function, the `pub` is a promise nobody outside can use: the function is effectively `pub(crate)`. The `unreachable_pub` lint, allow by default, reports each such item as an "unreachable `pub` item" and suggests `pub(crate)` or exporting it. A `pub` type that a public function returns counts as reachable and is not flagged. Spelling it `pub(crate)` lets a reviewer see the real API, and makes a later `pub use` of the item fail with E0364 instead of silently growing the API. Turn the lint on for libraries.

code

rust · 31 lines
rust
// src/lib.rs of a library crate
#![warn(unreachable_pub)]

mod convert {
    // warning: unreachable `pub` item (help: `pub(crate)`)
    pub fn celsius_to_kelvin(c: f64) -> f64 {
        c + 273.15
    }

    // no warning: the restriction says what the reach really is
    pub(crate) fn kelvin_to_celsius(k: f64) -> f64 {
        k - 273.15
    }

    // no warning: reachable through the signature of `pub fn scale`
    pub struct Scale {
        pub name: &'static str,
    }
}

pub fn to_kelvin(c: f64) -> f64 {
    convert::celsius_to_kelvin(c)
}

pub fn to_celsius(k: f64) -> f64 {
    convert::kelvin_to_celsius(k)
}

pub fn scale() -> convert::Scale {
    convert::Scale { name: "kelvin" }
}

go deeper

for a junior

Know that pub alone does not export an item from a library: every module on the path must be public, or the item must be re-exported.

for a middle

Explain effective visibility: rustc treats a pub item behind a private module as crate-visible, and the unreachable_pub lint names it and suggests pub(crate).

for a senior

Turn the lint on for libraries and spell internal reach as pub(crate), so a reviewer sees the true API and an accidental pub use fails with E0364.

for a principal

Weigh the churn: enabling unreachable_pub on a large crate rewrites many declarations once, but afterwards every bare pub in the source is a public promise the release review can trust.

## Declared visibility versus effective visibility The `pub` keyword is a **declared visibility**. What other crates can actually use is the **effective visibility**, and rustc computes it from the module tree: an item is reachable from outside when every module on its path is public from the crate root, when a `pub use` re-exports it, or when it appears in the signature of something that is itself reachable; a `pub` item with none of these is not. So `pub` alone does not export anything. In the library below, `convert` is a private module at the root. Its `pub fn celsius_to_kelvin` can be called anywhere inside the crate, because the whole crate descends from the root where `convert` is declared, but no other crate can name it: `thermo::convert::celsius_to_kelvin` is E0603, "module `convert` is private". For every caller outside the crate it behaves exactly like `pub(crate)`. ## What unreachable_pub reports `unreachable_pub` is a rustc lint (not a Clippy one), listed in 1.99 as **allow** by default with the description "`pub` items not reachable from crate root". Once enabled, it warns on each item whose declared visibility is wider than its effective one: | Item in the private `convert` module | Reachable from other crates? | `unreachable_pub` | |---|---|---| | `pub fn celsius_to_kelvin` (never re-exported) | no | warns, suggests `pub(crate)` | | `pub(crate) fn kelvin_to_celsius` | no | silent: the declaration already says so | | `pub struct Scale`, returned by `pub fn scale()` | yes, through the signature | silent | | a `pub fn` re-exported with `pub use` at the root | yes | silent | The warning is "unreachable `pub` item" with a help line, "consider restricting its visibility: `pub(crate)`", and a second one, "or consider exporting it for use by other crates". Both are honest options: the lint asks you to decide, not to delete. Note the third row. Reachability is not only about paths: a value of type `Scale` reaches other crates whenever `scale()` is called, so `Scale` and its `pub` field must stay `pub` for downstream code to use what it receives. That is the same rule that makes a *private* type in a public signature a problem, seen from the other side. ## Why spell it pub(crate) The compiler treats both spellings identically for outside callers, so the gain is about people and future edits: - **Readers see the real API.** In a large library, a reviewer cannot tell from `pub fn` whether a change is breaking for other crates without walking the module tree. `pub(crate)` answers that at the declaration. - **Accidental export is blocked.** A later `pub use convert::celsius_to_kelvin;` compiles and quietly adds the function to the public API if it is `pub`. If it is `pub(crate)`, rustc rejects the re-export with E0364, "is only public within the crate, and cannot be re-exported outside", so widening the API takes a deliberate edit to the item itself. - **Dead code is reported either way.** An unreachable item that nothing uses gets a `dead_code` warning whether it says `pub` or `pub(crate)`, because rustc already knows its effective visibility. That warning is often the first hint that a `pub` is not exporting anything. ## Common misreadings - **"`pub` in a private module means visible to the parent only."** No: it means visible to every module that can reach the private module, which for a module declared at the crate root is the whole crate. - **"`unreachable_pub` is a Clippy lint."** It ships with rustc itself, so it works in an ordinary build with no extra tool; unused code remains `dead_code`'s job. - **"Fixing a warning means deleting the item."** The help offers two exits, restricting to `pub(crate)` or exporting the item, and both keep the code. - **"`pub(crate)` hides the item from the rest of the crate."** It does the opposite of hiding inside the crate: every module may use it, only other crates may not. ## Turning it on 1. Add `#![warn(unreachable_pub)]` at the top of `lib.rs`, or set the lint in the manifest's lint table so every crate of a workspace shares it (that table is Cargo's material). 2. Build, and for each warning choose: restrict to `pub(crate)`, or export the item on purpose with a public module or a `pub use`. 3. Keep it at warn or deny afterwards, so new unreachable `pub` items are caught in review rather than discovered when someone tries to re-export one. On a big crate the first pass touches many declarations. That churn is a one-off; after it, every bare `pub` left in the source is a real part of the crate's interface.

  • Why does unreachable_pub not flag `pub struct Scale` inside the private `convert` module?
    Because `pub fn scale() -> convert::Scale` puts it in a public signature, so other crates receive `Scale` values and use its public field even though they cannot write its path. Reachability follows public signatures as well as paths, and the type must stay `pub` for those values to be usable downstream.
  • What happens if someone later adds `pub use convert::kelvin_to_celsius;` at the crate root?
    If the function is declared `pub(crate)`, rustc rejects the re-export with E0364, 'is only public within the crate, and cannot be re-exported outside', so widening the API needs a deliberate edit to the item. If it is plain `pub`, the re-export compiles and the function silently becomes part of the public API.

  • Every pub item in a library is part of its public API.
  • unreachable_pub is on by default, so a build would already show these items.
  • A pub item in a private module and a pub(crate) item differ for callers in other crates.
  • A pub type returned by a public function should be narrowed to pub(crate).
  • A pub use can turn a pub(crate) item into public API without touching the item.