Hi -- I've been trying to understand the visibility of structs in rust and what the "pub" keyword does. By experimenting, I have reversed-engineered some rules but I couldn't verify them. Is there a pointer to a definitive guide so I can check if my findings are correct?
My current version of the rules:
- A non-public struct is visible to the module where it's defined and all its descendants
- A public struct is only visible to the immediate parent of a module, but not higher.
Is this correct?
Say we have module tree structure being
Crate -> (Mod1, Mod2), and Mod1 -> (SubMod1, SubMod2).
-
If we have a
struct Foo
in Mod1, then it is visible from SubMod1, but not visible in any other places. -
If we have a
pub struct Bar
in the lowest submodule SubMod1, then it is visible from Mod1 (its immediate parent), but not visible anywhere else.
Please advise. Thanks!