Skip to content

Commit ce0989a

Browse files
committed
Add pub(restricted) example
1 parent 7cb86da commit ce0989a

File tree

1 file changed

+44
-19
lines changed

1 file changed

+44
-19
lines changed

examples/mod/visibility/visibility.rs

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,55 @@
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
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()");
3046
}
31-
47+
3248
// Nested modules follow the same rules for visibility
3349
mod private_nested {
3450
#[allow(dead_code)]
3551
pub fn function() {
36-
println!("called `my::private_nested::function()`");
52+
println!("called `my_mod::private_nested::function()`");
3753
}
3854
}
3955
}
@@ -45,25 +61,34 @@ fn function() {
4561
fn main() {
4662
// Modules allow disambiguation between items that have the same name.
4763
function();
48-
my::function();
49-
64+
my_mod::function();
65+
5066
// Public items, including those inside nested modules, can be
5167
// 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
5479

5580
// Private items of a module cannot be directly accessed, even if
5681
// nested in a public module:
57-
82+
5883
// Error! `private_function` is private
59-
//my::private_function();
84+
//my_mod::private_function();
6085
// TODO ^ Try uncommenting this line
6186

6287
// Error! `private_function` is private
63-
//my::nested::private_function();
88+
//my_mod::nested::private_function();
6489
// TODO ^ Try uncommenting this line
6590

6691
// Error! `private_nested` is a private module
67-
//my::private_nested::function();
92+
//my_mod::private_nested::function();
6893
// TODO ^ Try uncommenting this line
6994
}

0 commit comments

Comments
 (0)