liveBook · Manning

Cargo.toml

As explained in Learn Rust in a Month of Lunches

Cargo.toml is the manifest file that declares a Rust package's metadata and dependencies, including external crates and their versions.

Cargo.toml is the manifest for a Cargo project. It holds package details, such as the name, version, and Rust edition, and has a [dependencies] section for external crates—code your project uses from elsewhere. Running cargo new my_project creates this file alongside a src directory for your code. section 16.5.1 section 18.1.1

[package]
name = "rust_book"
version = "0.1.0"
authors = ["David MacLeod"]
edition = "2021"
 
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
[dependencies]
rand = "0.8.5"

This manifest adds rand = "0.8.5" under [dependencies]. The quoted value specifies the version used in this example. section 16.5.1

To add a crate, find it on crates.io and copy its suggested line into [dependencies]. You can also run cargo add rand. Then cargo run builds and runs the project: Cargo brings in rand and the other crates rand needs. In code, paths into this crate begin with rand rather than std. section 16.5.1 section 18.1.1

A release-mode setting

Cargo.toml can hold settings beyond dependencies. Adding [profile.release] followed by panic = 'abort' selects abort behavior for a release build. Use cargo run --release or cargo build --release to build with that setting; it can make the binary a little smaller, but is not usually the desired panic behavior. section 21.2

How Learn Rust in a Month of Lunches covers it

How deep it goes
Walks through adding a dependency and running the project, then gives a brief release-profile setting; does not detail manifest rules.
Examples
Shows a manifest with rand = "0.8.5", Cargo output compiling its dependencies, and a release profile using panic = 'abort'.
What it assumes
Assumes a Rust installation for local commands and shows how cargo new creates the project and its manifest.
What you can do after
Operate/configure it by adding a crate under [dependencies] and using cargo run to build and run with that crate.
How it connects
Connects Cargo.toml to Cargo, external crates, crates.io, cargo run, and release-mode panic behavior.

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 Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.