Sub topic 8
Reading and editing Cargo.toml
This chapter assumes you can run cargo new and cargo run, and that you know what a compiler and a package manager do. Here you will read the manifest that Cargo generates and make the edits you will need most often.
What the manifest is
Cargo.toml is the manifest of a package. It is written in TOML, a plain-text format made of key = value pairs grouped under [table] headers. Cargo reads it every time you build, so a mistake in this file stops the build before the compiler runs.
Here is the file that cargo new hello_cargo writes:
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
[dependencies]The edition value that cargo new writes depends on your Cargo version. Newer releases may write 2024.
The [package] table
The [package] table describes the package itself. Three keys appear in every generated manifest.
| Key | Meaning |
|---|---|
| `name` | The package name. It also names the binary that Cargo builds, and the library crate if there is one. |
| `version` | The package version, in `major.minor.patch` form following semantic versioning. |
| `edition` | The Rust edition used to compile the code. |
An edition is a named set of language rules. Code written for one edition keeps compiling under that edition, even when newer editions change the rules. Packages on different editions can be used together in one build. Change edition only when you intend to move your code to the newer rules.
You can add optional keys to describe the package. These are the ones you will see most often:
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
description = "A small example project"
license = "MIT"These keys matter mainly if you publish to crates.io. For a local project you can leave them out.
The [dependencies] table
Each line under [dependencies] names a crate and the versions you accept. A crate is a unit of Rust code that Cargo compiles. The next chapter covers adding dependencies in detail. For now, you only need to be able to read the syntax:
[dependencies]
rand = "0.8"
serde = { version = "1", features = ["derive"] }The short form rand = "0.8" is a version requirement. Cargo reads it as "any version compatible with 0.8", meaning at least 0.8.0 and below 0.9.0. The table form lets you set more options for one dependency. Here features turns on optional parts of the serde crate.
Cargo picks one exact version that satisfies each requirement and records it in Cargo.lock. You edit Cargo.toml. Cargo writes Cargo.lock.
How edits take effect
Cargo checks the manifest on every command, so there is no separate "reload" step. The flow looks like this:
If the manifest has a syntax error, Cargo stops at the parsing step and prints the line and column of the problem.
Try it: edit the manifest
Start in the hello_cargo project from the earlier chapter. Add two description keys under [package], then check that Cargo still accepts the file.
[package]
name = "hello_cargo"
version = "0.2.0"
edition = "2021"
description = "My first Cargo project"
[dependencies]Now run a check:
cargo checkA successful run ends with a line like Finished and no errors. The version bump to 0.2.0 shows up in the Compiling hello_cargo v0.2.0 line.
Common errors
- A missing quote. Writing
name = hello_cargofails to parse because TOML strings need quotes. Cargo reports an "invalid string" or "expected" error with a line number. - A key in the wrong table. Putting
editionunder[dependencies]makes Cargo treat it as a crate name, and the build fails. Keep each key under its intended header. - A renamed package that breaks paths. Changing
namechanges the binary name undertarget/debug. Scripts that call the old path then fail. - An unknown edition. An edition value your Cargo does not support, such as
"2030", stops the build. Runrustup updateif you need a newer edition.
Exercises
Exercise
You change rand = "0.8" to rand = "0.9" in Cargo.toml and run cargo build. Which file does Cargo update to record the exact version it picked, and should you edit that file by hand?
Show answer
Cargo updates Cargo.lock. You should not edit it by hand, because Cargo writes it for you.
Exercise
After running cargo build, you see the error "expected an equals, found a newline" pointing to the line name "demo". What is wrong, and how do you fix it?
Show answer
The line is missing the = sign between the key and the value. Write it as name = "demo".