1
- // A module named `my `
2
- mod my {
1
+ // A module named `my_mod `
2
+ mod my_mod {
3
3
// Items in modules default to private visibility.
4
4
fn private_function ( ) {
5
- println ! ( "called `my ::private_function()`" ) ;
5
+ println ! ( "called `my_mod ::private_function()`" ) ;
6
6
}
7
-
7
+
8
8
// Use the `pub` modifier to override default visibility.
9
9
pub fn function ( ) {
10
- println ! ( "called `my ::function()`" ) ;
10
+ println ! ( "called `my_mod ::function()`" ) ;
11
11
}
12
-
12
+
13
13
// Items can access other items in the same module,
14
14
// even when private.
15
15
pub fn indirect_access ( ) {
16
- print ! ( "called `my ::indirect_access()`, that\n > " ) ;
16
+ print ! ( "called `my_mod ::indirect_access()`, that\n > " ) ;
17
17
private_function ( ) ;
18
18
}
19
19
20
20
// Modules can also be nested
21
21
pub mod nested {
22
22
pub fn function ( ) {
23
- println ! ( "called `my ::nested::function()`" ) ;
23
+ println ! ( "called `my_mod ::nested::function()`" ) ;
24
24
}
25
25
26
26
#[ allow( dead_code) ]
27
27
fn private_function ( ) {
28
- println ! ( "called `my ::nested::private_function()`" ) ;
28
+ println ! ( "called `my_mod ::nested::private_function()`" ) ;
29
29
}
30
+
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
+ }
37
+
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
+ }
43
+
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
+ }
50
+
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
+ }
57
+
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()" ) ;
30
61
}
31
-
62
+
32
63
// Nested modules follow the same rules for visibility
33
64
mod private_nested {
34
65
#[ allow( dead_code) ]
35
66
pub fn function ( ) {
36
- println ! ( "called `my ::private_nested::function()`" ) ;
67
+ println ! ( "called `my_mod ::private_nested::function()`" ) ;
37
68
}
38
69
}
39
70
}
@@ -45,25 +76,34 @@ fn function() {
45
76
fn main ( ) {
46
77
// Modules allow disambiguation between items that have the same name.
47
78
function ( ) ;
48
- my :: function ( ) ;
49
-
79
+ my_mod :: function ( ) ;
80
+
50
81
// Public items, including those inside nested modules, can be
51
82
// accessed from outside the parent module.
52
- my:: indirect_access ( ) ;
53
- my:: nested:: function ( ) ;
83
+ my_mod:: indirect_access ( ) ;
84
+ my_mod:: nested:: function ( ) ;
85
+ my_mod:: call_public_function_in_my_mod ( ) ;
86
+
87
+ // pub(crate) items can be called from anywhere in the same crate
88
+ my_mod:: public_function_in_crate ( ) ;
89
+
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
54
94
55
95
// Private items of a module cannot be directly accessed, even if
56
96
// nested in a public module:
57
-
97
+
58
98
// Error! `private_function` is private
59
- //my ::private_function();
99
+ //my_mod ::private_function();
60
100
// TODO ^ Try uncommenting this line
61
101
62
102
// Error! `private_function` is private
63
- //my ::nested::private_function();
103
+ //my_mod ::nested::private_function();
64
104
// TODO ^ Try uncommenting this line
65
105
66
106
// Error! `private_nested` is a private module
67
- //my ::private_nested::function();
107
+ //my_mod ::private_nested::function();
68
108
// TODO ^ Try uncommenting this line
69
109
}
0 commit comments