Sub topic 3
Building and Testing Selected Members
Assume you know basic Rust syntax, single-package Cargo.toml files, and ordinary cargo build, cargo test, and cargo run commands. A workspace contains several related packages, called members. Cargo can select one member or operate on all members.
Select Members with Cargo
Use -p or --package when you want one package. Use --workspace for every member. --exclude removes packages from a workspace-wide command.
| Goal | Command |
|---|---|
| Build one package | `cargo build -p app` |
| Test one package | `cargo test -p textkit` |
| Run one binary | `cargo run -p app` |
| Build all except one | `cargo build --workspace --exclude app` |
A virtual workspace usually has no root [package] table. The root still controls shared settings.
[workspace]
resolver = "2"
members = ["crates/*"]
default-members = ["crates/app"]
[workspace.lints.rust]
unsafe_code = "forbid"Members inherit the lint policy explicitly:
[lints]
workspace = trueThis makes unsafe code a compile-time error in every inheriting member. Shared lints keep local development and CI consistent.
Resolver Features and Root-Only Settings
The resolver controls how Cargo selects dependency versions and features. Resolver 2, shown above, avoids some unwanted feature sharing between unrelated dependency uses. Features can still unify when multiple workspace members use the same dependency in one build graph. Test a feature combination that CI actually builds.
The workspace root owns profiles such as release optimization:
[profile.release]
lto = trueA profile placed inside a member manifest does not control the workspace build. Keep profiles and other workspace-wide build policy at the root.
Versioning, Publishing, and CI
Each publishable crate needs a valid version, package metadata, and a dependency plan. In a workspace, members can inherit a common version:
[package]
name = "textkit"
version.workspace = true
edition.workspace = true
license.workspace = true
[workspace]
version = "0.1.0"Publish crates only when their manifests can resolve published dependencies, not unpublished local paths. Keep repository layout predictable, such as crates/app and crates/textkit, and commit the single workspace Cargo.lock for applications.
Exercise
Which command tests every workspace member except the app package?
Show answer
cargo test --workspace --exclude app