Dependencies and Cargo.lock
Quick answer
Dependencies supply code from other packages. Cargo reads version requirements from Cargo.toml and records the chosen dependency graph in Cargo.lock. This separates requested compatibility from the versions selected for a build.
Requirements and resolved versions
For example, 1.2 normally permits compatible versions from 1.2.0 up to but excluding 2.0.0; =1.2.3 selects exactly that version. A lockfile records transitive dependencies too. Commit application lockfiles; a library's lockfile does not control its downstream users' resolution. Use --locked to reject a build requiring lockfile changes.
Manual terminal step; not executed. Expected behavior (environment dependent):
Existing lockfile and cached dependencies required.Use a disposable reviewed project with a generated lockfile; offline does not obtain missing crates.
cargo new lesson_lockcd lesson_lockcargo generate-lockfile --offlinecargo check --locked --offlineManual terminal step; not executed. Expected behavior (environment dependent):
Cargo.lock is created and check should succeed.The project starts without third-party dependencies.
Common mistake
A direct version pin alone does not pin every transitive dependency.
Try it yourself
Compare the manifest and lockfile of the new package and identify which one lists resolved versions.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Dependencies and Cargo.lock — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A CI build must not change its resolved dependency graph. Which Cargo flag enforces this?
Dependencies and Cargo.lock — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```bash cargo tree cargo check --locked --offline ``` Which outcome or interpretation best matches this example?
Dependencies and Cargo.lock — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```bash cargo new lesson_lock cd lesson_lock cargo generate-lockfile --offline cargo check --locked --offline ``` Which outcome or interpretation best matches this example?