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
33
+ pub ( in my_mod) fn public_function_in_my_mod ( ) {
34
+ println ! ( "called `my_mod::nested::public_function_in_my_mod()`" )
35
+ }
36
+ }
37
+
38
+ pub fn call_public_function_in_my_mod ( ) {
39
+ print ! ( "called `my_mod::call_public_funcion_in_my_mod()`, that\n > " ) ;
40
+ nested:: public_function_in_my_mod ( ) ;
41
+ }
42
+
43
+ // pub(crate) makes functions visible only within the current crate
44
+ pub ( crate ) fn public_function_in_crate ( ) {
45
+ println ! ( "called `my_mod::public_function_in_crate()" ) ;
30
46
}
31
-
47
+
32
48
// Nested modules follow the same rules for visibility
33
49
mod private_nested {
34
50
#[ allow( dead_code) ]
35
51
pub fn function ( ) {
36
- println ! ( "called `my ::private_nested::function()`" ) ;
52
+ println ! ( "called `my_mod ::private_nested::function()`" ) ;
37
53
}
38
54
}
39
55
}
@@ -45,16 +61,25 @@ fn function() {
45
61
fn main ( ) {
46
62
// Modules allow disambiguation between items that have the same name.
47
63
function ( ) ;
48
- my :: function ( ) ;
49
-
64
+ my_mod :: function ( ) ;
65
+
50
66
// Public items, including those inside nested modules, can be
51
67
// accessed from outside the parent module.
52
- my:: indirect_access ( ) ;
53
- my:: nested:: function ( ) ;
68
+ my_mod:: indirect_access ( ) ;
69
+ my_mod:: nested:: function ( ) ;
70
+ my_mod:: call_public_function_in_my_mod ( ) ;
71
+
72
+ // pub(crate) items can be called from anywhere in the same crate
73
+ my_mod:: public_function_in_crate ( ) ;
74
+
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
54
79
55
80
// Private items of a module cannot be directly accessed, even if
56
81
// nested in a public module:
57
-
82
+
58
83
// Error! `private_function` is private
59
84
//my::private_function();
60
85
// TODO ^ Try uncommenting this line
0 commit comments