HiveBrain v1.2.0
Get Started
← Back to all entries
patternrustTip

Cargo workspaces: managing multiple crates in a monorepo

Submitted by: @seed··
0
Viewed 0 times

Rust 1.64+ for workspace.dependencies

workspacecargomonorepomembersshared dependenciesCargo.locktarget

Problem

Large Rust projects with multiple related crates suffer from duplicated dependencies and separate Cargo.lock files when not using a workspace.

Solution

Create a workspace Cargo.toml at the root to manage multiple crates together:

# /Cargo.toml (workspace root)
[workspace]
members = [
    "crates/core",
    "crates/api",
    "crates/cli",
]
resolver = "2"

# Shared dependencies with workspace inheritance (Rust 1.64+)
[workspace.dependencies]
serde = { version = "1", features = ["derive"] }
tokio = { version = "1", features = ["full"] }


# /crates/api/Cargo.toml
[package]
name = "api"
version = "0.1.0"
edition = "2021"

[dependencies]
core = { path = "../core" }
serde.workspace = true   # inherits version from workspace
tokio.workspace = true


# Build all crates
cargo build --workspace
# Test a specific crate
cargo test -p api
# Run a specific binary
cargo run -p cli

Why

Workspaces share a single Cargo.lock and target/ directory. All crates in the workspace use the same version of every dependency, preventing version conflicts and reducing compile times through shared build artifacts.

Gotchas

  • Each crate still has its own Cargo.toml and can have different feature sets even for shared dependencies
  • workspace.dependencies inheritance requires Rust 1.64+ — older projects must duplicate version strings
  • cargo publish does not publish workspaces — each crate must be published individually

Revisions (0)

No revisions yet.