Pub type exported or not
⚓ Rust 📅 2025-06-22 👤 surdeus 👁️ 14While working on a Rust library, I encountered this thing I didn't realize before:
A pub type Foo can be accessed by external code even when Foo was not exported in lib.rs.
For example, lib.rs has exported a public function like:
pub fn my_fn() -> Foo
The external user can call my_fn() to get a Foo and even call Foo.foo_func as long as foo_func is pub.
It means the external code can use Foo for quite a bit use cases by Rust's type inference. But this creates an interesting scenario: in cargo doc output, the user can see this my_fn returns a Foo, but they cannot get the doc of Foo as it's not exported.
A random example: ArgMatches in clap - Rust
There is no doc for the public type Occurrences.
I guess basically Rust does not force an external public type to be exported in a lib. (Or, should I call such type half-pub type?) But I'm curious if that is a design choice or by accident?
7 posts - 4 participants
🏷️ rust_feed