Finlay Weber
Finlay Weber

Reputation: 4163

How do I keep a mod private and use it in another module within the same project in Rust?

So I want to have some modules defined in lib.rs but not make them public but only available for use within the project.

in my lib.rs, if I have the definition as this:

pub mod args;

in my main.rs, I can use the args modules this way:

use my_lib::args::Cli;

where my_lib is defined in Cargo.tml as

[lib]
name = "my_lib"
path = "src/lib.rs"

but I don't want pub mod args;. I tried changing to pub(crate) mod args; but this leads to compilation error that the args module cannot be found.

How do I make a module like args defined in lib.rs available without have to give it the most permissive visibility?

Upvotes: 1

Views: 714

Answers (1)

cafce25
cafce25

Reputation: 27576

Since Rust separates the library lib.rs and binary main.rs into separate crates there is no simple way to include things that are in lib.rs and not pub from main.rs.

I suggest you follow the way of big crates (serde comes to mind) and add a pub mod __private; which conveys the meaning. You can additionaly annotate it with #[doc(hidden)] to hide it from the documentation making it even more obvious.

Upvotes: 5

Related Questions