liveBook · Manning

Lock file

As explained in Code Like a Pro in Rust

Cargo.lock is Cargo’s lock file that records exact package dependency versions and integrity checksums.

Cargo.lock records the resolved versions of direct and indirect package dependencies, plus checksums used to verify their integrity. Cargo.toml declares dependency requirements; the lock file records the packages selected under those requirements. section 2.2.1 section 2.2

Choose version-control treatment by project type:

  • Applications: commit Cargo.lock alongside Cargo.toml to help keep published releases consistent if third-party libraries change. section 2.2.1
  • Libraries: leave Cargo.lock out of version control so downstream packages can update indirect dependencies as needed; with Git, add it to .gitignore. section 2.2.1

Run cargo update to refresh Cargo.lock to the newest available crates permitted by the dependency specifications. This differs from pinning a version with an exact requirement in Cargo.toml: the requirement controls which versions are eligible. section 2.2

Listing 2.1 A minimal Cargo.toml
[package]
name = "simple-project"
version = "0.1.0"
authors = ["Brenden Matthews <brenden@brndn.io>"]
edition = "2018"

[dependencies]
rand = "0.8"

The rand = "0.8" entry is a Cargo.toml dependency requirement, not a record of every resolved package version. section 2.2

Workspace members share one top-level Cargo.lock and its resolved dependency tree. Keep member dependencies declared in Cargo.toml; workspace membership does not replace dependency declarations. section 2.10

How Code Like a Pro in Rust covers it

How deep it goes
Defines file contents and gives version-control and update guidance, without detailing the lock file format.
Examples
Uses a rand dependency requirement and a workspace with a path-dependent library to illustrate nearby configuration.
What you can do after
Operate/configure it by committing application lock files, excluding library lock files from Git, and running cargo update when appropriate.
How it connects
Links Cargo.toml dependency requirements, SemVer, cargo update, Git, and Cargo workspaces.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Code Like a Pro in Rust. The section links point to the parts of the book it draws on.