Skip to content

Commit 576efaf

Browse files
authored
Merge pull request #856 from cramertj/pub-restricted
Add pub(restricted) example
2 parents a501cc3 + 91d2505 commit 576efaf

File tree

1 file changed

+59
-19
lines changed

1 file changed

+59
-19
lines changed

examples/mod/visibility/visibility.rs

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,70 @@
1-
// A module named `my`
2-
mod my {
1+
// A module named `my_mod`
2+
mod my_mod {
33
// Items in modules default to private visibility.
44
fn private_function() {
5-
println!("called `my::private_function()`");
5+
println!("called `my_mod::private_function()`");
66
}
7-
7+
88
// Use the `pub` modifier to override default visibility.
99
pub fn function() {
10-
println!("called `my::function()`");
10+
println!("called `my_mod::function()`");
1111
}
12-
12+
1313
// Items can access other items in the same module,
1414
// even when private.
1515
pub fn indirect_access() {
16-
print!("called `my::indirect_access()`, that\n> ");
16+
print!("called `my_mod::indirect_access()`, that\n> ");
1717
private_function();
1818
}
1919

2020
// Modules can also be nested
2121
pub mod nested {
2222
pub fn function() {
23-
println!("called `my::nested::function()`");
23+
println!("called `my_mod::nested::function()`");
2424
}
2525

2626
#[allow(dead_code)]
2727
fn private_function() {
28-
println!("called `my::nested::private_function()`");
28+
println!("called `my_mod::nested::private_function()`");
2929
}
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()");
3061
}
31-
62+
3263
// Nested modules follow the same rules for visibility
3364
mod private_nested {
3465
#[allow(dead_code)]
3566
pub fn function() {
36-
println!("called `my::private_nested::function()`");
67+
println!("called `my_mod::private_nested::function()`");
3768
}
3869
}
3970
}
@@ -45,25 +76,34 @@ fn function() {
4576
fn main() {
4677
// Modules allow disambiguation between items that have the same name.
4778
function();
48-
my::function();
49-
79+
my_mod::function();
80+
5081
// Public items, including those inside nested modules, can be
5182
// 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
5494

5595
// Private items of a module cannot be directly accessed, even if
5696
// nested in a public module:
57-
97+
5898
// Error! `private_function` is private
59-
//my::private_function();
99+
//my_mod::private_function();
60100
// TODO ^ Try uncommenting this line
61101

62102
// Error! `private_function` is private
63-
//my::nested::private_function();
103+
//my_mod::nested::private_function();
64104
// TODO ^ Try uncommenting this line
65105

66106
// Error! `private_nested` is a private module
67-
//my::private_nested::function();
107+
//my_mod::private_nested::function();
68108
// TODO ^ Try uncommenting this line
69109
}

0 commit comments

Comments
 (0)