Sblog

Rust workspaces

Date: 2023-01-15

I've recently come to try out workspaces in Rust and I love them. Rust workspaces are groups of crates that work together. Here are some selling points:

Easy to set up

Setting up couldn't be simpler - all you need is a single Cargo.toml file with a single section that defines what crates you have in the workspace. Example:

[workspace]
members = ["api", "core", "cli"]

Common lock file

All of your crates within the workspace have a common Cargo.lock that lives in the main directory. This means that however many crates depend on the same 3rd party crate - they will all have the same locked version. No need to wrangle different versions of the same crate - Yay!

Multiple binaries

Workspaces make it easy to have multiple binaries in the same project. Example - you want to make a CLI utility that:

  • helps you with maintenance and reporting;
  • uses the same data representation as in your API;
  • doesn't make sense to ship on its own.

With a workspace you can have the CLI and web API executables share the same core library all in one neat little project.

Run all tests at once

Tests - good; individual crate tests - good; running them all at once - good! Why use big words when small words do trick?