intermediateModules
Module Visibility
Control visibility with pub, pub(crate), and pub(super).
Module Visibility
Control visibility with pub, pub(crate), and pub(super).
Difficulty
Intermediate
Code
rust
mod outer {
pub mod inner {
pub fn public_fn() { println!("public"); }
fn private_fn() { println!("private"); }
pub(crate) fn crate_visible() { println!("crate visible"); }
}
pub fn call_inner() {
inner::public_fn();
inner::crate_visible();
// inner::private_fn(); // error: function is private
}
}
fn main() {
outer::inner::public_fn();
outer::inner::crate_visible();
outer::call_inner();
}Explanation
pub, pub(crate), pub(super) control visibility.
Key Concepts
- Read the code carefully and understand the data flow
- Try modifying the example to see how it changes behavior
- Run this code in the Rust Playground
Related Topics
Browse more examples in the modules category to build a complete understanding of this topic.