Sub topic 2
Declaring Dependencies in Cargo.toml
This chapter assumes you can create a project with cargo new, run commands in a terminal, and read basic Rust. It covers the manifest syntax that declares a dependency and the errors you are most likely to hit when you write it by hand. The cargo add command, version rules and features each get their own chapter.
Where dependencies live
Every Cargo project has a manifest file named Cargo.toml. It is written in TOML, a simple configuration format made of [tables] and key = value pairs. The [dependencies] table lists the crates your code needs at run time. Each key is a crate name from crates.io, and each value says which versions you accept.
[package]
name = "json_demo"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"Cargo reads this file each time you build. If a listed crate is missing locally, Cargo downloads it from crates.io and compiles it before compiling your code.
Three ways to write an entry
An entry can take one of three equivalent forms, depending on how much you need to say.
[dependencies]
# 1. Shorthand: a version string only
serde_json = "1"
# 2. Inline table: version plus options
serde = { version = "1", features = ["derive"] }
# 3. Expanded table: the same as form 2, easier to read when long
[dependencies.tokio]
version = "1"
default-features = false
features = ["rt", "macros"]Use the shorthand when you only need a version. Use a table when you need options such as features. The shorthand "1" is the same as { version = "1" }.
The version string is a requirement, not an exact pin. "1" means "any release compatible with 1.x.y". The SemVer chapter explains the exact rules.
What Cargo does with an entry
The diagram shows the path from a manifest line to compiled code.
Resolution picks the newest version that satisfies every requirement in your project, including the requirements your dependencies place on their own dependencies. The chosen versions are written to Cargo.lock, which a later chapter covers.
Names in the manifest and in code
The name on the left of an entry is the crate's package name. In Rust code you refer to the crate by that name, with hyphens turned into underscores. A package called serde-json would be written serde_json in a use line. The package serde_json already uses an underscore, so both match.
use serde_json::Value;
fn main() {
let v: Value = serde_json::from_str("[1, 2, 3]").unwrap();
println!("{v}");
}Common errors
Most manifest problems fall into a few patterns.
| Symptom | Likely cause | Fix |
|---|---|---|
| `no matching package named 'serde_jsn' found` | Typo in the crate name | Check the exact name on crates.io |
| `failed to select a version for the requirement` | No published release satisfies your version | Lower the requirement or check the available versions |
| `cannot find derive macro 'Serialize'` | The `derive` feature is not enabled | Add `features = ["derive"]` |
| `unused manifest key` warning | An entry sits under the wrong table, such as `[package]` | Move it under `[dependencies]` |
| `unresolved import` | Hyphen written in the `use` path | Replace hyphens with underscores |
Checking your work
After editing the manifest, run cargo check. It resolves and downloads the dependencies, then type-checks your code without producing a binary. It is faster than cargo build and reports manifest errors immediately.
Exercise
A teammate writes serde = "1" under [dependencies] and adds #[derive(Serialize)] to a struct. The build fails with "cannot find derive macro Serialize". What is missing, and how would you write the corrected entry?
Show answer
The derive feature of serde is not enabled. Change the entry to serde = { version = "1", features = ["derive"] }.