Skip to main content

pub Keyword in Rust

The pub Keyword in Rust

In Rust, visibility and access control are essential for structuring code effectively. The pub keyword is used to make items such as functions, structs, enums, and modules publicly accessible. This article explores the significance of pub in Rust, its usage in different contexts, and how it influences encapsulation.


01. Understanding the pub Keyword

By default, items in Rust modules are private. The pub keyword allows an item to be accessed from outside its module.


mod library {
    pub fn greet() {
        println!("Hello from the library module!");
    }
}

fn main() {
    library::greet(); // ✅ Accessible due to `pub`
}
  • The greet function is marked as pub, making it accessible outside the library module.
  • Without pub, trying to call library::greet() would result in a compilation error.

02. pub with Structs

Structs in Rust follow visibility rules where fields remain private by default. The pub keyword must be used explicitly for each field that should be accessible.


mod vehicle {
    pub struct Car {
        pub brand: String, // Accessible outside
        year: u16,         // Private field
    }

    impl Car {
        pub fn new(brand: &str, year: u16) -> Car {
            Car {
                brand: brand.to_string(),
                year,
            }
        }
    }
}

fn main() {
    let my_car = vehicle::Car::new("Tesla", 2023);
    println!("Car brand: {}", my_car.brand); // ✅ Allowed
    // println!("Car year: {}", my_car.year); // ❌ Error: `year` is private
}
  • The brand field is marked as pub and can be accessed outside the module.
  • The year field is private and cannot be accessed directly.

03. pub with Enums

Unlike structs, making an enum pub automatically makes all its variants accessible.


mod status {
    pub enum Status {
        Active,
        Inactive,
    }
}

fn main() {
    let state = status::Status::Active; // ✅ Accessible
}
  • Marking Status as pub makes both Active and Inactive accessible.
  • Unlike structs, individual enum variants do not require additional pub declarations.

04. pub with Modules

By default, modules themselves are private unless marked with pub. To access a module from another file or module, declare it with pub mod.


pub mod utils {
    pub fn helper() {
        println!("Helper function is accessible!");
    }
}

fn main() {
    utils::helper(); // ✅ Accessible
}
  • Marking utils as pub makes it accessible.
  • The function helper is also pub and can be called outside the module.

05. pub(crate): Restricting Access to a Crate

Sometimes, you may want to make an item public but only within the same crate. Use pub(crate) for this purpose.


mod network {
    pub(crate) fn connect() {
        println!("Connecting to the network...");
    }
}

fn main() {
    network::connect(); // ✅ Accessible within the crate
}
  • pub(crate) allows access within the same crate but prevents external access.

06. pub(super): Restricting Access to a Parent Module

The pub(super) visibility modifier makes an item accessible only in the parent module.


mod outer {
    mod inner {
        pub(super) fn parent_only() {
            println!("Accessible only in the parent module");
        }
    }

    fn call_inner() {
        inner::parent_only(); // ✅ Allowed within `outer`
    }
}
  • pub(super) allows access only in the immediate parent module.

07. pub(in path): Restricting Access to a Specific Module

To restrict access to a specific module, use pub(in path).


mod a {
    pub mod b {
        pub(in crate::a) fn restricted_fn() {
            println!("Accessible only within `a`");
        }
    }
}
  • The function restricted_fn is only accessible inside module a.

08. Conclusion

The pub keyword in Rust is crucial for managing visibility and access control. By carefully structuring modules, structs, and enums with appropriate pub variations, developers can ensure encapsulation while maintaining modular and readable code.

Comments