Skip to content

Commit 91d2505

Browse files
committed
Add pub(self) and pub(super)
1 parent ce0989a commit 91d2505

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

examples/mod/visibility/visibility.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,30 @@ mod my_mod {
2929
}
3030

3131
// Functions declared using `pub(in path)` syntax are only visible
32-
// within the given path
32+
// within the given path. `path` must be a parent or ancestor module
3333
pub(in my_mod) fn public_function_in_my_mod() {
34-
println!("called `my_mod::nested::public_function_in_my_mod()`")
35-
}
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+
}
3649
}
3750

3851
pub fn call_public_function_in_my_mod() {
3952
print!("called `my_mod::call_public_funcion_in_my_mod()`, that\n> ");
4053
nested::public_function_in_my_mod();
54+
print!("> ");
55+
nested::public_function_in_super_mod();
4156
}
4257

4358
// pub(crate) makes functions visible only within the current crate

0 commit comments

Comments
 (0)