How to define modules outside of main/lib rs?

I have this file structure for my crate:

main.rs
fooA.rs
fooB.rs
fooShared.rs

and I'm using code from fooShared in both fooA and fooB, but not in main.

Now if I put mod fooShared; in main.rs, I can access fooShared from the other modules just fine.

However considering main is not a direct dependency of fooShared I would like to avoid having to define it main, but I haven't been able to figure out how to do this (or if its even possible), so any pointers would be appreciated.

Every module needs exactly one mod statement that can be found from main, but it doesn't need to appear directly there— it can be a submodule instead. You can, for example, structure your code like this:

// main.rs
mod foo;

fn main() {
    let a = foo::a::FooA::new();
    let b = foo::b::FooB::new();
}
// foo.rs
mod shared;
pub mod a;
pub mod b;
// foo/shared.rs
pub struct SharedFoo { ... }
// foo/a.rs
use super::shared::SharedFoo;
pub struct FooA { ... } 
// foo/b.rs
use super::shared::SharedFoo;
pub struct FooB { ... } 

Ah true, I guess I was a bit too focused on having everything as standalone modules, thanks for the quick reply :slight_smile:

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.