Search code examples
rustmodulevisibility

Rust visibility across files in same folder


I have divided my Rust project into multiple files in same folder for tidiness. Now each file is perceived as a mod of its own and I have to mark everything pub(crate). Is there a way to publicize all symbols in a file for same-crate usage, or mark files as not modules of their own but a part of my main.rs?


Solution

  • Rust has a builtin macro include!, that works similarly to #include in c but mind that that's not its intended use. The macro is usually used for including generated files like opengl bindings that are not intended for editing. Rust analyzer will not offer intelisense in included files. As @Chayim Friedman mentioned, using pub(crate) is the way, BUT:

    struct A; // private struct
    pub(crate) struct B; // public to whole crate
    pub struct C; // public to other crates, but only if this is library
    pub(super) struct D; // public to parent module
    

    If you have main I assume your project is application and not a library so you need just pub.