[newbie] How to structure a repo with lib and multiple binaries?

Hello all,

I'm working on my first Rust project. The bulk of the project is a list of utility modules that I want to publish as a library. In addition, I have a main script and multiple helper scripts for development, mainly to calculate features for my ML model.

So far, I've managed everything with a single Cargo.toml at repo level along with src/lib.rs, src/main.rs, helper scripts in src/bin, and all library modules just in src/.

As the project has grown, however, I've started needing extra modules and dependencies for the scripts in src/bin, and as a result, quite a few of the packages in Cargo.toml are unrelated to the main library.

I'm fine with this for now, but I hope to publish my library on crates.io in the future, and I'm concerned that my library will claim to need a whole list of packages that in reality are only useful for some internal scripts.

Is there a better way to organize my repo here?

Thanks for your help!

Use a workspace. I'm not going to get into all the details, because the book explains it better, but it lets multiple crates in the same directory depend on each other and share some settings (including a Cargo.lock file) without needing to share dependencies. It's most useful when you have multiple related library crates, but it also works great for the situation you describe, too. In your case, split the binaries out to their own crate or crates, and then remove the no longer needed dependencies from the library. Crates in the workspace are still published individually, so you can still keep helper scripts unpublished if you would like.

2 Likes