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. }
  7. // Use the `pub` modifier to override default visibility.
  8. pub fn function() {
  9. println!("called `my_mod::function()`");
  10. }
  11. // Items can access other items in the same module,
  12. // even when private.
  13. pub fn indirect_access() {
  14. print!("called `my_mod::indirect_access()`, that\n> ");
  15. private_function();
  16. }
  17. // Modules can also be nested
  18. pub mod nested {
  19. pub fn function() {
  20. println!("called `my_mod::nested::function()`");
  21. }
  22. #[allow(dead_code)]
  23. fn private_function() {
  24. println!("called `my_mod::nested::private_function()`");
  25. }
  26. // Functions declared using `pub(in path)` syntax are only visible
  27. // within the given path. `path` must be a parent or ancestor module
  28. pub(in my_mod) fn public_function_in_my_mod() {
  29. print!("called `my_mod::nested::public_function_in_my_mod()`, that\n > ");
  30. public_function_in_nested()
  31. }
  32. // Functions declared using `pub(self)` syntax are only visible within
  33. // the current module
  34. pub(self) fn public_function_in_nested() {
  35. println!("called `my_mod::nested::public_function_in_nested");
  36. }
  37. // Functions declared using `pub(super)` syntax are only visible within
  38. // the parent module
  39. pub(super) fn public_function_in_super_mod() {
  40. println!("called my_mod::nested::public_function_in_super_mod");
  41. }
  42. }
  43. pub fn call_public_function_in_my_mod() {
  44. print!("called `my_mod::call_public_funcion_in_my_mod()`, that\n> ");
  45. nested::public_function_in_my_mod();
  46. print!("> ");
  47. nested::public_function_in_super_mod();
  48. }
  49. // pub(crate) makes functions visible only within the current crate
  50. pub(crate) fn public_function_in_crate() {
  51. println!("called `my_mod::public_function_in_crate()");
  52. }
  53. // Nested modules follow the same rules for visibility
  54. mod private_nested {
  55. #[allow(dead_code)]
  56. pub fn function() {
  57. println!("called `my_mod::private_nested::function()`");
  58. }
  59. }
  60. }
  61. fn function() {
  62. println!("called `function()`");
  63. }
  64. fn main() {
  65. // Modules allow disambiguation between items that have the same name.
  66. function();
  67. my_mod::function();
  68. // Public items, including those inside nested modules, can be
  69. // accessed from outside the parent module.
  70. my_mod::indirect_access();
  71. my_mod::nested::function();
  72. my_mod::call_public_function_in_my_mod();
  73. // pub(crate) items can be called from anywhere in the same crate
  74. my_mod::public_function_in_crate();
  75. // pub(in path) items can only be called from within the mode specified
  76. // Error! function `public_function_in_my_mod` is private
  77. //my_mod::nested::public_function_in_my_mod();
  78. // TODO ^ Try uncommenting this line
  79. // Private items of a module cannot be directly accessed, even if
  80. // nested in a public module:
  81. // Error! `private_function` is private
  82. //my_mod::private_function();
  83. // TODO ^ Try uncommenting this line
  84. // Error! `private_function` is private
  85. //my_mod::nested::private_function();
  86. // TODO ^ Try uncommenting this line
  87. // Error! `private_nested` is a private module
  88. //my_mod::private_nested::function();
  89. // TODO ^ Try uncommenting this line
  90. }