Visibility

Modules are a privacy boundary:

  • Module items are private by default (hides implementation details).
  • Parent and sibling items are always visible.
  • In other words, if an item is visible in module foo, it’s visible in all the descendants of foo.
  1. mod outer {
  2. fn private() {
  3. println!("outer::private");
  4. }
  5. pub fn public() {
  6. println!("outer::public");
  7. }
  8. mod inner {
  9. fn private() {
  10. println!("outer::inner::private");
  11. }
  12. pub fn public() {
  13. println!("outer::inner::public");
  14. super::private();
  15. }
  16. }
  17. }
  18. fn main() {
  19. outer::public();
  20. }
  • Use the pub keyword to make modules public.

Additionally, there are advanced pub(...) specifiers to restrict the scope of public visibility.

  • See the Rust Reference).
  • Configuring pub(crate) visibility is a common pattern.
  • Less commonly, you can give visibility to a specific path.
  • In any case, visibility must be granted to an ancestor module (and all of its descendants).