Usage of pub mod or mod

Hello

I am not sure on when to use mod or pub mod. According to the rust book (Modules Cheat Sheet),
code within a module is private from its parent modules by default. But in my basic ray tracer project, I declared the module with just mod camera, mode ray, etc. And each of the module is defined with pub struct and their corresponding implementations contains pub method.

Where is the scenario pub mod makes a difference ? Small snippet to example code would help to understand the issue .

Thanks

Think about whether anything outside the parent needs to access that module.

mod parent {
    pub mod foo {
        pub fn f() {}
    }

    mod bar {
        pub fn g() {}
    }
}

Other code can access parent::foo::f(), but not parent::bar::g() because bar is private.

2 Likes

Now it makes sense, I guess the wording in the book is somewhat flawsome - "Code within a module is private from its parent modules by default."

Well, if I annotate that statement for my example, "Code (like mod bar) within a module (mod parent) is private from its parent modules (outside of mod parent) by default." It doesn't help that I called it parent in this case, but I hope you see what it means.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.