Understanding Cargo.toml
Cargo.toml is your project's ID card, listing its name, version, and everything else it depends on.The Package Section
The [package] section of Cargo.toml records your project's name, version, and the Rust edition it targets, forming the core identity of the crate.
Example: The Package Section
[package]
name = "my_project"
version = "0.1.0"
edition = "2021"
⚠️ Run this command in your terminal.
The Dependencies Section
The [dependencies] section lists external crates your project needs, along with version requirements that Cargo uses to resolve compatible versions.
Example: The Dependencies Section
[dependencies]
serde = "1.0"
rand = "0.8"
⚠️ Run this command in your terminal.
Cargo.toml vs Cargo.lock
While you edit Cargo.toml by hand to declare what you depend on, Cargo automatically generates and maintains Cargo.lock, recording the exact versions actually resolved and used for a reproducible build.
Example: Cargo.toml vs Cargo.lock
fn main() {
println!("Cargo.toml: what you declare");
println!("Cargo.lock: what was actually resolved and locked in");
}
Login to try C/C++/Java/PHP code in the editor
Rust Editions
The edition field lets Cargo compile your code against a specific set of language rules (like 2015, 2018, or 2021), allowing the language to evolve while keeping older code compiling unchanged.
Example: Rust Editions
fn main() {
let editions = ["2015", "2018", "2021"];
for e in editions.iter() {
println!("Rust edition: {}", e);
}
}
Login to try C/C++/Java/PHP code in the editor
- Editing the
[dependencies]section with an incorrect version format, which causescargo buildto fail to resolve dependencies. - Confusing
Cargo.toml(the manifest you edit by hand) withCargo.lock(the exact resolved versions, usually auto-managed). - Forgetting to bump the
versionfield before publishing an updated crate to crates.io.
Cargo.tomlis the manifest file describing a project's metadata, edition, and dependencies.- The
[package]section holds name, version, and edition;[dependencies]lists external crates. Cargo.lockrecords exact resolved dependency versions and is usually committed for binaries, auto-generated by Cargo.- Editions (like
2021) let Rust evolve syntax/semantics over time without breaking older projects.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: