What Is Cargo.toml vs Cargo.lock

When to Use Which

Use Cargo.toml to define dependencies and Cargo.lock to lock exact versions for reproducible builds.

Use Cargo.toml to define your project's dependencies and settings, and let Cargo.lock automatically record the exact versions resolved for reproducible builds. You edit Cargo.toml to add or change dependencies, while Cargo.lock is generated by Cargo and should be committed to version control to ensure everyone builds with the same dependency tree.

[package]
name = "your-project"
version = "0.1.0"
edition = "2024"

[dependencies]
serde = "1.0"
cargo build

Cargo.lock is created automatically after the first build and contains the precise versions of all dependencies and their transitive dependencies.