Sub topic 8
Adding and managing dependencies
This chapter assumes you know what a package and a crate are, can run cargo build, and can read basic TOML. It covers how Cargo decides which versions of other crates your package uses, and what to check when that goes wrong.
Declaring a dependency
Dependencies live in the [dependencies] table of Cargo.toml. Each entry maps a crate name to a version requirement or a table of options.
[package]
name = "date_check"
version = "0.1.0"
edition = "2021"
[dependencies]
regex = "1"
my_geometry = { path = "../my_geometry" }You can also run cargo add regex, which edits the manifest for you. The next build downloads the crate from crates.io, compiles it, and links it into your crate.
use regex::Regex;
fn main() {
let date = Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
println!("{}", date.is_match("2026-12-02"));
}Version requirements
A bare version such as "1.2.3" is a caret requirement. Cargo accepts any version that is semver-compatible with it. For versions below 1.0, the leftmost non-zero number is the compatibility boundary.
| Written | Accepts |
|---|---|
| `"1"` | `>=1.0.0, <2.0.0` |
| `"1.2.3"` | `>=1.2.3, <2.0.0` |
| `"0.8.5"` | `>=0.8.5, <0.9.0` |
| `"~1.2.3"` | `>=1.2.3, <1.3.0` |
| `"=1.2.3"` | exactly `1.2.3` |
Prefer the default caret form. Exact pins block your users from sharing one copy of a crate with other dependencies.
Cargo.lock
The manifest states what is allowed. Cargo.lock records what was chosen. The first build resolves every requirement to an exact version and writes the result to the lock file. Later builds reuse those versions until you run cargo update, which picks newer versions that still satisfy the manifest.
Commit Cargo.lock for binaries so every build uses the same versions. For libraries it is optional, because downstream users resolve their own versions.
Where dependencies come from
- crates.io: the default, selected by a version string.
- path: a local directory, as with
my_geometryabove. This is common in workspaces. - git:
{ git = "https://github.com/user/repo", rev = "abc123" }. Pin arev,tag, orbranch, because a bare git dependency follows the default branch.
A path dependency that you also publish needs a version as well, for example { path = "../core", version = "0.1" }. crates.io rejects packages that have only a path.
Other dependency tables
[dev-dependencies] holds crates for tests and examples, and [build-dependencies] holds crates for a build.rs script. Neither is linked into the code your users depend on.
Common errors
- No matching package named
xfound: the name is misspelled, or the registry is unreachable. - Failed to select a version: the requirement matches no published version, or two dependencies need incompatible versions of a third.
- Mismatched types between "the same" type: two semver-incompatible versions of one crate are both in the build, so their types are distinct. Run
cargo tree -dto list duplicates, then align the requirements. - Unresolved import after adding a crate: the crate name uses a hyphen in
Cargo.tomlbut an underscore in code.serde-jsonis writtenserde_json.
cargo add regex
cargo tree
cargo tree -d
cargo update -p regexcargo tree shows the full resolved graph, -d shows only duplicated crates, and cargo update -p regex refreshes a single crate in the lock file.
Exercise
Your manifest says rand = "0.8.5" and Cargo.lock pins 0.8.5. Which versions can cargo update move you to, and why can it not move you to 0.9.0?
Show answer
It can choose any version from 0.8.5 up to but not including 0.9.0. For versions below 1.0, a caret requirement treats the minor number as the compatibility boundary, so 0.9.0 counts as a breaking change. Allowing it would require editing the manifest, for example to "0.9".