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 offoo.
1. mod outer {
2. fn private() {
3. println!("outer::private");
4. }
6. pub fn public() {
7. println!("outer::public");
8. }
10. mod inner {
11. fn private() {
12. println!("outer::inner::private");
13. }
15. pub fn public() {
16. println!("outer::inner::public");
17. super::private();
18. }
19. }
20. }
22. fn main() {
23. outer::public();
24. }
- Use the
pubkeyword 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).
