← Back to Rust Course | Chapter 15: Cargo, Testing & Best Practices | Lesson 1 of 8

Understanding Cargo.toml

Cargo.toml is your project's ID card, listing its name, version, and everything else it depends on.
Syntax
rust
[package]
name = "name"
version = "0.1.0"
edition = "2021"

[dependencies]
crate_name = "1.0"

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

bash
[package]
name = "my_project"
version = "0.1.0"
edition = "2021"

⚠️ Run this in your own terminal or Node.js environment.

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

bash
[dependencies]
serde = "1.0"
rand = "0.8"

⚠️ Run this in your own terminal or Node.js environment.

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

markup
// Entry point: execution of the program starts here
fn main() {
    // Print "Cargo.toml: what you declare" to the console (with a trailing newline)
    println!("Cargo.toml: what you declare");
    // Print "Cargo.lock: what was actually resolved and locked in" to the console (with a trailing newline)
    println!("Cargo.lock: what was actually resolved and locked in");
}

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

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `editions`, set to `["2015", "2018", "2021"]`
    let editions = ["2015", "2018", "2021"];
    // Loop over `editions.iter()`, binding each item to `e`
    for e in editions.iter() {
        // Print "Rust edition: {}" to the console, substituting in `e` (with a trailing newline)
        println!("Rust edition: {}", e);
    }
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Editing the [dependencies] section with an incorrect version format, which causes cargo build to fail to resolve dependencies.
  2. Confusing Cargo.toml (the manifest you edit by hand) with Cargo.lock (the exact resolved versions, usually auto-managed).
  3. Forgetting to bump the version field before publishing an updated crate to crates.io.
Chapter Summary
  • Cargo.toml is the manifest file describing a project's metadata, edition, and dependencies.
  • The [package] section holds name, version, and edition; [dependencies] lists external crates.
  • Cargo.lock records 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.