Sub topic 8
Reading and Editing Cargo.toml
Cargo.toml is the manifest of a Cargo package. It tells Cargo what the package is called, which edition of Rust it uses, and which other crates it needs. This chapter assumes you can run shell commands, have installed Rust with rustup, and have created a project with cargo new. You should also know roughly what a package manager does.
What the manifest is
The manifest is written in TOML (Tom's Obvious, Minimal Language). TOML is a configuration format built from key = value pairs, grouped under headers in square brackets. A header such as [package] starts a table. Every key below it belongs to that table until the next header.
Cargo reads the manifest every time you run cargo build, cargo run, cargo check, or cargo test. You can edit it by hand, and some commands, such as cargo add, edit it for you.
Here is the manifest that cargo new hello_cargo generates:
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
[dependencies]The edition value that cargo new writes depends on your Cargo version. Newer versions may write a newer edition.
The `[package]` table
The [package] table describes the package itself. Three keys are present in a new project.
| Key | Meaning | Example |
|---|---|---|
| `name` | The package name. It also names the compiled binary. | `"hello_cargo"` |
| `version` | The package version, in semantic versioning form. | `"0.1.0"` |
| `edition` | The Rust edition used to compile the package's code. | `"2021"` |
name
The name is a string. Cargo uses it for the binary file in target/debug, so hello_cargo produces target/debug/hello_cargo (with .exe appended on Windows). Package names may contain letters, digits, -, and _. If you publish to crates.io, the name must also be unique there.
version
Versions follow semantic versioning, written MAJOR.MINOR.PATCH. In general, a major bump signals breaking changes, a minor bump adds features, and a patch bump fixes bugs. New projects start at 0.1.0. Versions below 1.0.0 are treated specially by Cargo's compatibility rules, which the dependency section below covers.
edition
An edition is an opt-in set of language changes that can break older code. Editions let Rust evolve without breaking existing projects. Each crate chooses its edition, and crates with different editions can be linked together in one build. If you copy old code into a new project and see errors about keywords or syntax, a mismatched edition is a common cause.
Changing the edition is a deliberate act. Do not change it just to silence an error without reading what changed. The command cargo fix --edition can help migrate code.
Optional package keys
You can add more keys to [package]. None is required to build locally. Most matter when you publish.
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
description = "A small example project"
license = "MIT"
rust-version = "1.70"descriptionis a short summary shown on crates.io.licenseis an SPDX license identifier, such asMITorApache-2.0.rust-versionis the oldest Rust compiler version the package supports. Cargo refuses to build the package with an older compiler and reports the mismatch.
The `[dependencies]` table
The [dependencies] table lists the crates your package uses. A crate is a unit of compiled Rust code, and a package can depend on crates published on crates.io, in a Git repository, or at a local path. A new project has an empty table.
Each entry maps a crate name to a version requirement. For example, after running cargo add rand, the table contains an entry like this:
[dependencies]
rand = "0.8"The exact number depends on the latest release when you run the command.
Version requirements
A bare version string such as "0.8" is shorthand for a caret requirement, ^0.8. It allows any version that is semver-compatible with the one you wrote. The rules are:
| Requirement | Allowed versions |
|---|---|
| `"1.2.3"` (same as `^1.2.3`) | `>=1.2.3, <2.0.0` |
| `"0.8"` (same as `^0.8`) | `>=0.8.0, <0.9.0` |
| `"0.0.3"` (same as `^0.0.3`) | `>=0.0.3, <0.0.4` |
| `"=1.2.3"` | exactly `1.2.3` |
| `">=1.2, <1.5"` | any version in that range |
| `"1.*"` | any `1.x.y` version |
For 0.x versions, the leftmost non-zero number is treated as the compatibility boundary. That is why "0.8" does not allow 0.9.0.
The requirement is a range, not a pin. Cargo picks a version inside it. The exact version it picks is recorded in Cargo.lock, so later builds use the same one until you ask Cargo to update it. That is the division of labor: Cargo.toml states what you will accept, and Cargo.lock records what you got. To move to newer compatible versions, run cargo update. To move to an incompatible version, change the requirement in Cargo.toml.
Table form for dependencies
When a dependency needs more than a version, you can write it as an inline table:
[dependencies]
serde = { version = "1.0", features = ["derive"] }A feature is an optional piece of a crate that you can switch on. Here features = ["derive"] enables the derive feature of serde. The same syntax selects other sources:
[dependencies]
my_helper = { path = "../my_helper" }
some_tool = { git = "https://github.com/example/some_tool" }A path dependency points at a crate on your disk. A git dependency fetches from a repository. Packages with a path or git source cannot be published to crates.io unless they also carry a version.
Other common tables
Beyond [package] and [dependencies], a few tables show up often.
[dev-dependencies]lists crates used only for tests, examples, and benchmarks. They are not part of the dependency set for people who use your crate.[build-dependencies]lists crates used by a build script.[profile.release]and[profile.dev]change compiler settings for release and debug builds.
A short example that combines two of these:
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
[dependencies]
rand = "0.8"
[dev-dependencies]
pretty_assertions = "1"
[profile.release]
opt-level = 3
lto = trueHere opt-level = 3 is the highest standard optimization level, and lto = true turns on link-time optimization, which can shrink and speed up the binary at the cost of longer builds.
Editing the manifest
You have two ways to change dependencies.
- Run
cargo add. It inserts the entry in the right table with the latest compatible version. - Edit the file by hand, then run any Cargo build command. Cargo re-reads the file and updates
Cargo.lockif needed.
cargo add rand
cargo add pretty_assertions --dev
cargo add serde --features deriveThe --dev flag puts the crate in [dev-dependencies]. The --features flag enables features. After each command, open Cargo.toml and read what changed. Seeing the result builds your understanding of the format.
Common errors
Manifest errors are reported before compilation starts. These are the ones you are most likely to meet.
| Symptom | Likely cause | Fix |
|---|---|---|
| `could not parse input as TOML` | A missing quote, bracket, or `=` | Check the line number in the message |
| `no matching package named ... found` | A misspelled crate name | Compare with the name on crates.io |
| `failed to select a version for the requirement` | The requirement matches no published version | Loosen or correct the version string |
| `duplicate key` | The same key appears twice in one table | Remove one of the entries |
| `unknown feature` | The crate has no feature with that name | Check the crate's documentation |
String values must be in double quotes. Writing edition = 2021 without quotes is parsed as a number and Cargo rejects it.
How the pieces connect
The diagram shows how Cargo uses the manifest and lock file during a build.
Exercises
Exercise
A manifest contains rand = "0.8". Cargo has recorded 0.8.5 in Cargo.lock, and 0.9.0 has since been released. Will cargo build use 0.9.0? What would you do to use it?
Show answer
No. The requirement "0.8" means >=0.8.0, <0.9.0, and Cargo.lock pins 0.8.5, so the build keeps using 0.8.5. To use 0.9.0, change the requirement in Cargo.toml to "0.9" (or run cargo add [email protected]), then build so Cargo updates Cargo.lock.
Exercise
You want a test-only crate named pretty_assertions that does not become a dependency for users of your library. Which command adds it, and under which table does the entry appear?
Show answer
Run cargo add pretty_assertions --dev. The entry appears under [dev-dependencies].
Exercise
Cargo reports a TOML parse error for the line edition = 2021... Actually, the line parses as valid TOML, but Cargo still rejects it. Why, and what is the correct line?
Show answer
TOML parses 2021 as an integer, but Cargo expects the edition as a string. The correct line is edition = "2021".