Controlling module names in crate?

I want to write a couple of crates that exist in a shared module namespace.

What I'll do now is this:

Crate 1 is named library_core so its definitions reside in library_core.

Crate 2 is named library_extras so its definitions reside in library_extras.

What I'd like to do is

Crate 1 is named library_core and its definitions reside in library::core.

Crate 2 is named library_extras and its definitions reside in library::extras.

I want these to be separate crates with separate Cargo.toml so dependencies are more isolated. Is that possible?

Will this do the trick?

extern crate library_core;
extern crate library_extras;

pub mod library {
    pub use library_core as core;
    pub use library_extras as extras;
}
1 Like

However, given "I want these to be separate crates with separate Cargo.toml so dependencies are more isolated." and core and extra names, I would suggest looking into Cargo features: Page Moved

1 Like

I expect that example would work when if I included them a single package.

I haven't seen anything in the Cargo documentation that looks to me like the ability to specify the "root module" a given package resides within.

I haven't seen anything in the Cargo documentation that looks to me like the ability to specify the "root module" a given package resides within.

I mean, maybe you don't want two crates here? Maybe you want one crate with optional dependencies?

But if you do want separate crates, and do want to access them via a single interface, then you need to create a third crate, which will play the role of facade.

1 Like

I haven't seen anything in the Cargo documentation that looks to me like the ability to specify the "root module" a given package resides within.

IIRC, you can place extern crate anywhere:

mod library {
  extern crate foo;
  extern crate bar;
}

1 Like

By itself each crate has its own root, only when you import it into another crate it becomes a submodule. So you can't specify such a "root module" in the crate itself.

1 Like