Search code examples
rustmodulenamespaces

Rust equivalent of C++ namespace?


With C++ namespaces, I can put anything anywhere from any file, and at compile time they will "merge" into a single namespace. Thus, this file structure:

/* src/main.cpp */
int main() {
    nsfoo::foo();
}
/******/

/* src/foo/function.cpp */
namespace foo {
    void foo(nsfoothing thing) {}
}
/******/

/* src/foo/structures.cpp */
namespace foo {
    struct nsfoothing {};

    void nsfooprocessthing(nsfoothing thing) {}
}
/******/

is equivalent to having a single file containing:

/* src/main.cpp */
namespace nsfoo {
    struct nsfoothing {};
    void foo(nsfoothing thing) {}
    void nsfooprocessthing(nsfoothing thing) {}
}

int main() {
    nsfoo::foo();
}
/******/

or this file structure:

/* src/main.cpp */
int main() {
    nsfoo:foo();
}
/******/

/* src/foo/foo.cpp */
namespace nsfoo {
    struct nsfoothing {};
    void foo(nsfoothing thing);
    void processnsfoothing(nsfoothing thing);
}
/******/

or this file:

/* tmp/quickndirtytesting.cpp */
namespace foo {
    struct nsfoothing {};
}

namespace foo {
    void foo(nsfoothing thing) {}
    void processnsfoothing(nsfoothing thing) {}
}

int main() {
    nsfoo::foo();
}
/******/

Point being, there are basically no restrictions as to how I layout my "modules", with what I put in what file - they will all "merge" when compiling.

With Rust, I tried translating a thing that should work in C++:

/* src/main.rs */
mod mymod {
    struct Foo {}
}

mod mymod { // <-- the name `mymod` is defined multiple times. `mymod` must be defined only once in the type namespace of this module.
    struct Bar {}
}

fn main() {
    // Would reference mymod here, but it produced an error before I even get here.
}
/******/

and quickly found that my experience "placing anything anywhere in whatever file structure" with C++ namespaces doesn't quite work the same in Rust. Ideally, I would like to have a structure similar to how I would have in C++:

.
└── src
    ├── main.rs
    └── mymod
        ├── agroupoffunctions.rs
        ├── structures.rs
        └── yetanothergroupoffunctions.rs

So I guess my question is, how do I make "partial namespaces" in Rust, similar to C++? If such a thing is not possible, how am I supposed to organize my data structures and functionalities?


Solution

  • Rust does not have a mechanism like C++'s namespaces.

    Rust's modules are the way to organize code, but they do not do any kind of automatic merging. If you want more nested files but access them in a flatter path, then you can re-export items from child modules into the parent module.


    Silly example:

    If you are writing code in main.rs and don't want to specify plants::trees::Pine, but rather want plants::Pine to work.

    src/
    - main.rs
    - plants/
      - mod.rs
      - trees/
        - ...
      - ferns/
        - ...
      - grasses/
        - ...
    

    Then you can re-export everything from plants::trees into plants in its mod.rs:

    mod trees;
    mod ferns;
    mod grasses;
    
    pub use trees::*; // <-- re-exports everything
    pub use ferns::*;
    pub use grasses::*;