Sub topic 2
The root Cargo.toml and the [workspace] table
This chapter assumes you can read a single-package Cargo.toml, know the difference between library and binary crates, and have run cargo build and cargo run. Here you will see how one manifest turns a directory of crates into a workspace.
What the root manifest does
A workspace is defined by a Cargo.toml that contains a [workspace] table. That file is the workspace root. Every package the workspace includes is a member. The members share one Cargo.lock and one target directory, both placed next to the root manifest.
Here is a minimal root manifest for a project with several crates under crates/:
[workspace]
resolver = "2"
members = ["crates/*"]
[workspace.package]
version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"
[workspace.dependencies]
textkit = { path = "crates/textkit" }
[workspace.lints.rust]
unsafe_code = "forbid"This file has no [package] table, so it is a virtual manifest. It defines the workspace but is not a crate itself. A later chapter compares this with a root that is also a package.
The keys in the table
The [workspace] table has two kinds of keys. Some choose which packages belong to the workspace. Others define values that members can inherit.
| Key | Purpose |
|---|---|
| `members` | Paths, or globs such as `crates/*`, of the member packages |
| `resolver` | Feature resolver version, `"2"` here |
| `[workspace.package]` | Metadata members can inherit, such as `version` and `edition` |
| `[workspace.dependencies]` | Dependency declarations members can reuse |
| `[workspace.lints]` | Lint settings members can opt into |
The members, exclude and default-members keys get their own chapter. The inheritance tables are covered in later chapters. For now, the point is that the root is the one place for settings shared by every member.
How Cargo finds the root
You can run Cargo from any member directory. Cargo starts in the current directory and searches each parent directory for a Cargo.toml that contains [workspace]. The first one it finds is the workspace root. Cargo then checks that the current package is listed as a member.
A member can also name its root explicitly with package.workspace = "../..". This is rarely needed, because the upward search handles normal layouts.
A member manifest
Each member declares itself as usual and opts into the shared values. The library crate looks like this:
[package]
name = "textkit"
version.workspace = true
edition.workspace = true
license.workspace = true
[lints]
workspace = trueFields written as field.workspace = true take their value from the matching [workspace.package] entry. Cargo does not inherit anything silently. A member that omits version.workspace = true must set its own version.
Settings that belong only at the root
Some settings are read only from the root manifest. Profiles such as [profile.release] and the [patch] and [replace] tables are examples. If you put [profile.release] in a member, Cargo ignores it and prints a warning. Move such settings to the root.
Checking your setup
From the root, run cargo metadata --no-deps --format-version 1. The output includes a workspace_root field and a workspace_members list. Use it to confirm that Cargo sees the members you expect.
Exercise
You add a [profile.release] section with lto = true to crates/app/Cargo.toml. Release builds do not change. Why, and where should the section go?
Show answer
Profiles are only read from the workspace root manifest. Cargo ignores the profile in a member manifest and warns about it. Move the [profile.release] section to the root Cargo.toml, where it applies to every member.