Upload nested crate to crates.io

70 views Asked by At

I have a Rust project that is composed of several utilities splited in 3 different crates with some dependencies between them. The directory tree would be as follows:

|_ CrateA
|   |_lib.rs
|   |_cargo.toml
|_ CrateB
|   |_lib.rs
|   |_cargo.toml
|_ CrateC
|   |_lib.rs
|   |_cargo.toml

I'd like to publish this whole project as a single crate (let's call it GlobalCrate), allowing anybody that uses it to access the public funcionality contained in any of those crates. For example, one application importing this "GlobalCrate" should be able to perform the following calls:

fn main()
{
   GlobalCrate::CrateA::functionA();
   GlobalCrate::CrateB::functionB();
   GlobalCrate::CrateC::functionC();
}

Is this possible or do I have to publish each crate separately and then import all of them on my application?

1

There are 1 answers

0
cafce25 On BEST ANSWER

Yes, you can create global_crate that just re-exports the other crates contents:

// global_crate/src/lib.rs
pub mod crate_a {
    pub use crate_a::*;
}
pub mod crate_b {
    pub use crate_b::*;
}

and in global_crate/Cargo.toml:


[dependencies]
crate_a = { path = "../crate_a", version = "0.1.0" }
crate_b = { path = "../crate_b", version = "0.1.0" }

Note: The version key in that table is not optional if you want to publish this crate.

You do have to publish all the crates global_crate depends on in addition to global_crate itself though.