Cargo.toml Section by Section
Cargo.toml is the manifest: a TOML file that tells Cargo what the package is, what it builds, what it needs and how to compile it. cargo new writes three lines of it; real projects use a dozen tables. This lesson is the map; later lessons go deep into each part.
The tables at a glance
| Table | Purpose | Lesson |
|---|---|---|
[package] |
name, version, edition, authors, license, MSRV (rust-version), publishing metadata |
this one |
[dependencies] |
crates the code uses | Dependency Kinds |
[dev-dependencies] |
crates only tests, examples and benches use | Dependency Kinds |
[build-dependencies] |
crates the build.rs script uses |
build.rs |
[target.'cfg(...)'.dependencies] |
platform-specific dependencies | Dependency Kinds |
[features] |
optional functionality | Features |
[lib], [[bin]], [[example]], [[test]], [[bench]] |
target settings (TOML [[x]] means "an array of tables") |
Targets |
[profile.*] |
compiler settings per build profile | Profiles |
[lints] |
rustc and Clippy lint levels for the package | check, clippy & fmt |
[workspace] |
turns the folder into a workspace root | Workspaces |
[patch], [replace] |
override a dependency's source | Dependency Kinds |
[package] in practice
[package]
name = "units" # crate name on the registry; [a-zA-Z0-9_-]
version = "0.4.1" # SemVer: MAJOR.MINOR.PATCH
edition = "2024" # language edition: 2015, 2018, 2021 or 2024
rust-version = "1.85" # minimum supported Rust version (MSRV)
description = "Unit conversions with compile-time checked dimensions"
license = "MIT OR Apache-2.0" # an SPDX expression
repository = "https://github.com/example/units"
readme = "README.md"
keywords = ["units", "physics"]
categories = ["science"]
publish = false # never upload by accident (or a list of allowed registries)editionselects language rules per crate. Crates of different editions link together freely, so you can upgrade one crate at a time (cargo fix --editiondoes most of the work).rust-versionis a promise ("this builds with Rust 1.85 or newer"). Cargo refuses to build with an older compiler, and the resolver prefers dependency versions that keep the promise. See Field Notes: CI & MSRV.description,license,repositoryare required or recommended for publishing; cargo package warns when they are missing.
Cargo exposes manifest values to your code at compile time as environment variables, so you never repeat the version number in source:
cargo new units --vcs none -q
cd units
cat > Cargo.toml <<'T'
[package]
name = "units"
version = "0.4.1"
edition = "2024"
rust-version = "1.85"
description = "Unit conversions"
license = "MIT OR Apache-2.0"
authors = ["Ada Lovelace <ada@example.com>"]
[dependencies]
T
cat > src/main.rs <<'RS'
fn main() {
println!("{} v{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
println!("by {}", env!("CARGO_PKG_AUTHORS"));
println!("license: {}", env!("CARGO_PKG_LICENSE"));
println!("MSRV: {}", env!("CARGO_PKG_RUST_VERSION"));
println!("major version: {}", env!("CARGO_PKG_VERSION_MAJOR"));
}
RS
cargo run -qunits v0.4.1
by Ada Lovelace <ada@example.com>
license: MIT OR Apache-2.0
MSRV: 1.85
major version: 0
env! fails at compile time if the variable is missing, so these values can't drift from the manifest. --version output in a CLI should always come from CARGO_PKG_VERSION.
Typos are warnings, not errors
TOML is forgiving and so is Cargo: an unknown key is ignored with a warning. That is convenient for forward compatibility (newer keys don't break older Cargo versions), and dangerous when you misspell something important:
cargo new demo --vcs none -q
cd demo
cat >> Cargo.toml <<'T'
[profile.release]
opt-levle = 3
T
sed -i 's/^edition = "2024"/&\nlicence = "MIT"/' Cargo.toml
cargo build 2>&1warning: Cargo.toml: unused manifest key: package.licence
warning: Cargo.toml: unused manifest key: profile.release.opt-levle
warning: `demo` (manifest) generated 2 warnings
Compiling demo v0.1.0 (/home/learner/demo)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.21s
Neither the license nor the optimisation setting took effect. Read the warnings in CI logs, or make them fail the build (Cargo has no "deny unused manifest keys" flag, but cargo package and code review catch most of them).
Where else Cargo reads settings
The manifest describes the package. Settings that describe your machine or CI (registries, linkers, build directories, aliases, offline mode) belong in .cargo/config.toml, covered in config.toml & Vendoring. A rule of thumb: if every developer and every CI job must agree on it, it goes in Cargo.toml; if it depends on where you build, it goes in config.
Test yourself
What happens when Cargo.toml contains opt-levle = 3 under [profile.release]?
- The build fails
- Cargo warns about an unused manifest key and ignores it
- Cargo guesses
opt-level - It applies to all profiles
How should a CLI print its own version?
- Hard-code the string in main.rs
env!("CARGO_PKG_VERSION")- Read Cargo.toml at runtime
std::env::var("VERSION")
Which setting belongs in .cargo/config.toml rather than Cargo.toml?
- The crate's license
- The linker to use for a cross-compilation target on this machine
- The list of features
- The package version