Sub topic 1
Why workspaces: one repository, several crates
Prerequisites
This chapter assumes you can:
- Write basic Rust and use
cargo new,cargo buildandcargo run. - Read the
Cargo.tomlof a single package, including[dependencies]and[features]. - Tell a library crate (
src/lib.rs) from a binary crate (src/main.rs).
The problem: a project that outgrows one crate
A single crate works well until it starts to hold unrelated parts. Say you are building a command-line tool. Text-processing code, a parser and the main function all sit in one package. Compile times grow, because a change anywhere recompiles the whole crate. Reuse gets harder, because another project cannot depend on only the parser. Boundaries blur, because every module can reach every other module.
The usual fix is to split the code into several crates. That creates a new problem. If each crate is its own project, each has its own Cargo.lock, its own target directory and its own copy of settings such as the edition. Dependency versions drift apart, and the same libraries get compiled again and again.
What a workspace is
A Cargo workspace is a set of packages, called members, that Cargo manages together. The workspace is defined by a [workspace] table in one root Cargo.toml. Members share:
- One
Cargo.lock, so the whole workspace resolves each dependency to a single version. - One
targetdirectory, so a dependency is compiled once and reused by every member. - Common settings that the root defines and the members inherit.
Members stay separate packages. Each has its own Cargo.toml, its own name and its own public API. The workspace does not merge them into one crate.
A minimal example
The smallest useful workspace is a root manifest with a members list. The crates/* glob picks up every directory under crates/ that contains a Cargo.toml.
[workspace]
resolver = "2"
members = ["crates/*"]With a library crates/textkit and a binary crates/app, you run the following from the root:
cargo build --workspace
cargo run -p appThe first command builds every member. The second runs only the app binary. Cargo finds the workspace by looking in the current directory and then in each parent directory for a Cargo.toml that has a [workspace] table. You can therefore run these commands from anywhere inside the repository.
When a workspace helps, and when it does not
A workspace fits when:
- Several crates change together and you want one commit, one review and one CI run to cover them.
- You want a library and its binary, or a core library with several front ends, in one repository.
- You want the same dependency versions and compiler settings everywhere.
A workspace does not fit when:
- The crates have unrelated release cycles and different owners. Separate repositories are simpler.
- You only want to organize code inside one crate. Modules do that without extra manifests.
What comes next
This chapter covered the idea. The next chapters build it up in this order:
- The
[workspace]table and virtual manifests. - Choosing members.
- Path dependencies between members.
- Inheritance for package metadata, dependencies and lints.
- Selection flags such as
-pand--exclude. - Feature resolution, profiles, publishing and CI.
Exercise
You have a library crate and a binary crate, each in its own directory, and each with its own Cargo.lock and target directory. Name two concrete benefits of moving them into one workspace.
Show answer
Any two of these: both crates resolve dependencies through one Cargo.lock, so versions cannot drift apart. Both build into one target directory, so shared dependencies are compiled once. Common settings can be defined once in the root manifest. One cargo build --workspace or cargo test --workspace covers both crates.
Exercise
Does putting two crates in a workspace turn them into a single crate? Explain.
Show answer
No. Each member remains a separate package with its own Cargo.toml, name and public API. The workspace only makes them share a lock file, a target directory and common settings.