liveBook · Manning

Cargo.toml

As explained in Code Like a Pro in Rust

Cargo.toml is a project manifest and configuration file that specifies metadata and dependencies for a Cargo project.

Cargo.toml is the TOML manifest for a Cargo project. `cargo new` creates one alongside `src/main.rs` for an application or `src/lib.rs` for a library. Use it to declare package metadata, dependencies, and project-level build settings. section 2.1.2 section 2.2

Listing 2.1 A minimal Cargo.toml
[package]
name = "simple-project"
version = "0.1.0"
authors = ["Brenden Matthews <brenden@brndn.io>"]
edition = "2018"

[dependencies]
rand = "0.8"

A minimal manifest shows `[package]` metadata and a `[dependencies]` entry for `rand`. section 2.2

Choose the manifest entry that matches the job:

  • Put crates used by the project under `[dependencies]`. Version strings follow SemVer requirements; without an explicit operator, Cargo uses caret requirements. `cargo add rand` can add a dependency, while `cargo update` refreshes Cargo.lock within the declared requirements. Pin exact versions when necessary, but consider conflicts for downstream libraries. section 2.2
  • For a multi-crate workspace, add a member path under `[workspace]` and declare any needed path dependency separately. Workspace members share the top-level Cargo.lock, target directory, and top-level patch, replace, and profile settings. section 2.10
  • Put crates needed by a top-level `build.rs` under `[build-dependencies]`. The build script is compiled Rust, not interpreted code; it can compile C code or perform other build-time operations. section 2.11
  • Use a path or Git dependency to work with a modified direct dependency. Use `[patch.crates-io]` when an indirect registry dependency needs replacing, but do not expect consumers of your crate to inherit that patch. section 2.4 section 2.4.1

A workspace member is also a dependency

A top-level manifest can contain `[dependencies]` with `subproject = { path = "./subproject" }` and `[workspace]` with `members = ["subproject"]`. Membership groups the crates; the path entry declares the dependency the application uses. Run `cargo check` to verify the setup. section 2.10

For a published crate, `[package]` can also include `documentation = "https://docs.rs/dryoc"` so visitors can find its documentation. Keep the manifest’s declarations aligned with how the project is built and consumed. section 2.8 section 2.10

How Code Like a Pro in Rust covers it

How deep it goes
Works through concrete manifest entries and Cargo commands, without providing an exhaustive TOML or manifest reference.
Examples
Shows a minimal manifest with rand, a path-based workspace dependency, C build dependencies, and a registry patch.
What it assumes
Starts with Cargo-generated application and library layouts, then assumes familiarity with Rust source files and crates.
What you can do after
Operate/configure it to add crates, organize workspace members, supply build dependencies, and temporarily replace upstream dependencies.
How it connects
Connects dependency management and SemVer to Cargo.lock, workspaces, build scripts, and rustdoc.

Also explained in

Compare how 4 books explain cargo.toml →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Code Like a Pro in Rust. The section links point to the parts of the book it draws on.