Use neighbour module without calling parent's crate?

Hello everyone!

I'm dealing with module hierarchy. I have:

src/
  ecs/
    foo.rs
    bar.rs
  ecs.rs
  main.rs

With ecs.rs exposing module content:

// ecs.rs
mod foo;

pub use foo::do_something;

So I use ecs content in main.rs with:

// main.rs
mod ecs;
use crate::ecs;

ecs::do_something()

OK.

Now I want to use bar.rs content in foo.rs.

My only working solution is to add bar as mod:

// ecs.rs
mod foo;
mod bar; // declared here

pub use foo::do_something;

And use it in foo.rs like this:

// foo.rs
use crate::ecs::bar;

pub fn do_something() {
    bar::hello();
}

My problem is about giving the whole path:

use crate::ecs::bar;

I would like something that doesn't depends on full ecs path. A python-ish way would be:

// foo.rs
use .bar;
use crate::.::bar;

ecs module is a black box, internals shouldn't even known its name.

So, how foo.rs or bar.rs could use each other without having to specify the parent module name? What am I missing?

Thanks in advance! :slight_smile:

use super::bar;

https://doc.rust-lang.org/stable/book/ch07-03-paths-for-referring-to-an-item-in-the-module-tree.html#starting-relative-paths-with-super

2 Likes

Thanks, it's crazy I didn't found it depicts searching for this. :heart:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.