Visibility rule issues

I have a program that has three source files:

  • main.rs
  • lib.rs
  • template.rs (a module declared publicly in lib.rs

Currently, all items needed in main.rs are declared as public in template.rs. I don't want these items to be visible to external crates, though, as they are no use to callers who aren't me. I tried marking the items with pub(crate) but that caused Cargo to complain that the items are private and cannot be called in main.rs. Surely, though, main.rs and template.rs are in the same crate? I don't want to faff around with creating new modules or anything, but is there a way to make items declared in mod template visible to main.rs, while not being visible to callers using my crate?

Thanks!

With your setup, you have one binary crate (main.rs) and one library crate (lib.rs). In your library crate, you declared mod template;, making template.rs part of your library crate. So indeed, main.rs and template.rs are not in the same crate if declared this way. If only your binary needs access to template you can also declare the module in your binary crate instead of in your library crate.

4 Likes