pub and Visibility
Control access.
Private by Default
In Rust, everything inside a module is private by default. Other modules cannot see it unless you opt in with pub.
This safe default keeps implementation details hidden until you deliberately expose them.
pub Functions
Marking a function pub makes it callable from outside its module. Without it, the call fails to compile.
mod math {
pub fn square(x: i32) -> i32 {
x * x
}
}
fn main() {
println!("{}", math::square(5));
}All lessons in this course
- Modules and mod
- pub and Visibility
- use and Paths
- Crates and the Crate Root