How to define modules outside of main/lib rs?

I have this file structure for my crate:

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

Now if I put mod fooShared; in, 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:

mod foo;

fn main() {
    let a = foo::a::FooA::new();
    let b = foo::b::FooB::new();
mod shared;
pub mod a;
pub mod b;
// foo/
pub struct SharedFoo { ... }
// foo/
use super::shared::SharedFoo;
pub struct FooA { ... } 
// foo/
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.