Is there a way to import the root module as a single namespace?

Hi all,

I seem to to be able to find the correct syntax for doing the following: in a submodule I want to import the root module as a single namespace. So I want to do something like this (non-working code):

mod submodule {
    use super as root_module;
}

Now, I know that paths in use statements are relative to the root module so using super doesn't make sense and compiler understandably says no super in the root. But I can't find what should be used there to reference to the root module as a whole. I've tried:

  • use super as root_module
  • use self as root_module
  • use :: as root_module
  • use ::self as root_module
  • use ::{self} as root_module

So the only two options I see is either use * which is not exactly what I want or always prepend references to modules inside root module with ::.

Does anyone have an idea how to import root module as a single named namespace in the submodule?

I remember there being talk about changing the module system so that you refer to everything relative to the root (unless explicitly using super or self).

In that system you refer to your crate root with the crate keyword, so if I had some errors module in my crate I might use use crate::errors::Error to import my error types.

That said, I'm not sure whether it's possible to do what you are talking about currently. I currently just do use {foo, Bar}, skipping that leading :: seems to be valid (playground).

How about this:

mod submodule {
    mod root_module {
        pub use ::*;
    }
}

Playground

2 Likes

Nice idea, that works! Thanks!