Sub topic 6
Re-exports and shaping a public API
This chapter assumes you know how to declare modules, write use declarations, and mark items pub. It covers how pub use lets you separate where code lives from the path users see.
Why internal layout should not leak
The module tree you choose for maintenance is often not the one users want to type. A user of a geometry library would rather write my_geometry::Square than my_geometry::geometry::shapes::Square. If the long path is the public one, moving a file later becomes a breaking change for every user.
A re-export fixes this. pub use brings an item into scope and also makes that new name part of the public API of the module that contains the declaration.
A private module with a public path
// src/lib.rs
mod geometry;
pub use geometry::shapes::Square;
pub fn describe(s: &Square) -> String {
use std::fmt::Write;
let mut out = String::new();
write!(out, "square with area {}", s.area()).unwrap();
out
}Here mod geometry; has no pub, so outside crates cannot name geometry at all. The line pub use geometry::shapes::Square; publishes only Square, at the crate root. Users write my_geometry::Square.
Two details are worth noticing:
Squareitself must be reachable and public where it is defined. Apub usecannot make a private item public. Theshapesmodule ingeometry.rsis declaredpub mod shapes;, andSquareispub struct, so the chain of visibility holds from the crate root.- The
use std::fmt::Write;insidedescribeis a normalusewith nopub. It brings the trait into scope for that function only, which is what makeswrite!work on aString. It is not re-exported.
How the paths relate
The solid arrows show where the code is defined. The dotted arrow is the public shortcut. The user crate only sees the crate root and what it exports.
Common patterns
| Pattern | Example | Use it when |
|---|---|---|
| Flatten a path | `pub use geometry::shapes::Square;` | The nested path is an implementation detail. |
| Rename on export | `pub use geometry::shapes::Square as Sq;` | Two items would otherwise clash. |
| Glob re-export | `pub use geometry::shapes::*;` | A module exists to collect many items. Use it sparingly, because it hides what is public. |
| Prelude module | `pub mod prelude { pub use super::Square; }` | Users commonly import the same set of items together. |
Common errors
- Private item error. Writing
pub useon an item that is not itself visible gives error E0364 or E0365. Fix the visibility at the definition, not at the re-export. - Re-exporting too much. A glob
pub usecan add names to your API every time you add an item to the source module. Each added name is a commitment to keep it. - Changing the defining path. If you re-export, you can move
Squareto another file and update onepub useline. Users do not notice. Without the re-export, they do.
Doc tests follow the public path
Documentation examples compile as if they were an outside crate, so they use the re-exported path. This keeps them honest about the API users actually see.
/// Adds two numbers.
///
/// ```
/// assert_eq!(my_geometry::add(2, 3), 5);
/// ```
pub fn add(a: i32, b: i32) -> i32 {
a + b
}Exercise
A crate declares mod geometry; (private) in lib.rs, and geometry.rs contains pub struct Square(pub u32);. What single line in lib.rs lets a user write my_geometry::Square, and why does the user still not see my_geometry::geometry?
Show answer
Add pub use geometry::Square; to lib.rs. The re-export publishes only the name Square at the crate root. The geometry module is declared without pub, so it stays private to the crate and its path cannot be named from outside.
Exercise
You write pub use inner::helper; where helper is declared as fn helper() {} in inner. The compiler rejects it. What is the fix?
Show answer
Make the function public where it is defined: pub fn helper() {}. A pub use can only re-export items that are already visible to the place doing the re-export at the level of publicity you ask for. It cannot widen visibility.