Visibility

By default, the items in a module have private visibility, but this can be overridden with the pub modifier. Only the public items of a module can be accessed from outside the module scope.

1. // A module named `my_mod`
2. mod my_mod {
3. // Items in modules default to private visibility.
4. fn private_function() {
5. println!("called `my_mod::private_function()`");
6. }

8. // Use the `pub` modifier to override default visibility.
9. pub fn function() {
10. println!("called `my_mod::function()`");
11. }

13. // Items can access other items in the same module,
14. // even when private.
15. pub fn indirect_access() {
16. print!("called `my_mod::indirect_access()`, that\n> ");
17. private_function();
18. }

20. // Modules can also be nested
21. pub mod nested {
22. pub fn function() {
23. println!("called `my_mod::nested::function()`");
24. }

26. #[allow(dead_code)]
27. fn private_function() {
28. println!("called `my_mod::nested::private_function()`");
29. }

31. // Functions declared using `pub(in path)` syntax are only visible
32. // within the given path. `path` must be a parent or ancestor module
33. pub(in my_mod) fn public_function_in_my_mod() {
34. print!("called `my_mod::nested::public_function_in_my_mod()`, that\n > ");
35. public_function_in_nested()
36. }

38. // Functions declared using `pub(self)` syntax are only visible within
39. // the current module
40. pub(self) fn public_function_in_nested() {
41. println!("called `my_mod::nested::public_function_in_nested");
42. }

44. // Functions declared using `pub(super)` syntax are only visible within
45. // the parent module
46. pub(super) fn public_function_in_super_mod() {
47. println!("called my_mod::nested::public_function_in_super_mod");
48. }
49. }

51. pub fn call_public_function_in_my_mod() {
52. print!("called `my_mod::call_public_funcion_in_my_mod()`, that\n> ");
53. nested::public_function_in_my_mod();
54. print!("> ");
55. nested::public_function_in_super_mod();
56. }

58. // pub(crate) makes functions visible only within the current crate
59. pub(crate) fn public_function_in_crate() {
60. println!("called `my_mod::public_function_in_crate()");
61. }

63. // Nested modules follow the same rules for visibility
64. mod private_nested {
65. #[allow(dead_code)]
66. pub fn function() {
67. println!("called `my_mod::private_nested::function()`");
68. }
69. }
70. }

72. fn function() {
73. println!("called `function()`");
74. }

76. fn main() {
77. // Modules allow disambiguation between items that have the same name.
78. function();
79. my_mod::function();

81. // Public items, including those inside nested modules, can be
82. // accessed from outside the parent module.
83. my_mod::indirect_access();
84. my_mod::nested::function();
85. my_mod::call_public_function_in_my_mod();

87. // pub(crate) items can be called from anywhere in the same crate
88. my_mod::public_function_in_crate();

90. // pub(in path) items can only be called from within the mode specified
91. // Error! function `public_function_in_my_mod` is private
92. //my_mod::nested::public_function_in_my_mod();
93. // TODO ^ Try uncommenting this line

95. // Private items of a module cannot be directly accessed, even if
96. // nested in a public module:

98. // Error! `private_function` is private
99. //my_mod::private_function();
100. // TODO ^ Try uncommenting this line

102. // Error! `private_function` is private
103. //my_mod::nested::private_function();
104. // TODO ^ Try uncommenting this line

106. // Error! `private_nested` is a private module
107. //my_mod::private_nested::function();
108. // TODO ^ Try uncommenting this line
109. }