Sub topic 6
Connecting crates with path dependencies
This chapter assumes you can write a single-package Cargo.toml and know the difference between library and binary crates. It also assumes you have the root manifest from the earlier chapters, with members = ["crates/*"].
What a path dependency is
A path dependency points at a crate on disk instead of a registry such as crates.io. Cargo reads that crate's Cargo.toml, compiles it, and links it into the dependent crate. In a workspace, this is how one member uses another.
The direct form
The most explicit form gives the relative path from the dependent crate's manifest to the other crate's directory.
[dependencies]
textkit = { path = "../textkit" }The key on the left (textkit) must match the name in the target's [package] table. It does not have to match the directory name. If the package name contains hyphens, such as text-kit, you write text_kit:: in Rust code.
The workspace form
With many members, repeating paths becomes error-prone. Declare the path once in the root manifest:
[workspace.dependencies]
textkit = { path = "crates/textkit" }The path here is relative to the root Cargo.toml, not to the member. Each member then opts in:
[package]
name = "app"
version.workspace = true
edition.workspace = true
license.workspace = true
[dependencies]
textkit.workspace = true
[lints]
workspace = trueWriting textkit.workspace = true means "use the definition from [workspace.dependencies]". Declaring a dependency there does not add it to any member. Each member must still list it.
Using the sibling crate
The library exposes a public function. Items are private unless marked pub.
pub fn shout(input: &str) -> String {
format!("{}!", input.to_uppercase())
}The binary calls it by crate name:
fn main() {
println!("{}", textkit::shout("hello"));
}Run it from anywhere inside the workspace:
cargo run -p appThe output is HELLO!. Cargo builds textkit first because app needs it. If you edit textkit, the next cargo run -p app rebuilds both.
Rules and common errors
- Dependency cycles are rejected. If
appdepends ontextkit, thentextkitcannot depend onapp. Move shared code into a third crate. - A path dependency inside the workspace directory becomes a workspace member automatically, even if
membersdoes not list it. Useexcludeif you do not want that. - A wrong path gives
failed to read .../Cargo.toml. Check whether you wrote the path relative to the member or to the root. - A mismatched name gives
no matching package named .... Compare the dependency key with[package] name. - Using
textkit.workspace = truewithout a matching[workspace.dependencies]entry fails with an error that the dependency was not found in the workspace.
Adding extras per member
A member can add to an inherited dependency. For features, write textkit = { workspace = true, features = ["serde"] }. The member's features are added to those in the root entry. You can also mark it optional = true in the member. You cannot override the path or version in the member.
Exercise
Your root manifest has textkit = { path = "crates/textkit" } under [workspace.dependencies]. A new member at crates/cli needs it. What exact line goes in crates/cli/Cargo.toml, and which section holds it? Why is path = "crates/textkit" correct in the root but would be wrong in the member?
Show answer
Put textkit.workspace = true under [dependencies] in crates/cli/Cargo.toml. In [workspace.dependencies] the path is relative to the root manifest, so crates/textkit is correct there. From crates/cli, the same directory is ../textkit, so the root-relative path would not resolve if written in the member.