Search code examples
rustrust-cargo

Rust, How to configure a workspace of libraries?


I have a workspace lets call my_workspace. my_workspace has a series of small libraries related but each one with their own responsibility. Something like:

[workspace]
members = [
    "my_lib_1",
    "my_lib_2",
    "my_lib_3",
    ...
]

And my_workspace are structured like this:

|my_lib_1/
|my_lib_2/
|my_lib_2/
|Cargo.toml
...

This workspace doesn't export any binaries only libraries.

The thing how I can configure my Cargo.toml to treat each members as a feature? to use like this:

# package that use the lib

[dependencies]
my_workspace = { ... , features = ["my_lib_1"] }

// inside of the package that use `my_workspace`
use my_workspace::my_lib_1;

fn do_something() {
    my_lib_1::whatever_that_exposes_this_lib();
}

I'm sorry if this is a noob question, but I can't find any information in the cargo documentation that would shed light on how to accomplish this.


Solution

  • It seems like rust doesn't let me have a workspace as 'main' project, so instead I solved by adding a new crate that import all workspace members and reexport them with the features key in the Cargo.toml and the #[cfg(feature)] annotation in the lib.rs for each member.