0PricingLogin
Learn Rust Coding · Lesson

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

  1. Modules and mod
  2. pub and Visibility
  3. use and Paths
  4. Crates and the Crate Root
← Back to Learn Rust Coding