liveBook · Manning

Dependency management

As explained in Code Like a Pro in Rust

Package and dependency management is the practice of declaring, resolving, retrieving, and maintaining software packages and their dependencies.

Dependency management supplies Rust projects with libraries and applications through Cargo and the crates.io registry. Rust keeps many features outside the core language, so crates provide capabilities such as random-number generation. section 1.2.2 section 2.2

Use the dependency workflow to:

  • Declare crates in Cargo.toml under [dependencies]. section 2.2
  • Specify compatible versions with semantic versioning, including caret, tilde, wildcard, and comparison requirements. section 2.2
  • Add a dependency from the command line with cargo add. section 2.2

Adding rand

A minimal Cargo.toml declares rand = "0.8". This selects the latest 0.8 release under Cargo's default caret requirement behavior; use explicit SemVer operators when a narrower or exact range is needed. 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"

Inspect the complete Cargo.toml listing, including package metadata, the 2018 edition, and the rand dependency. section 2.2

Table 2.2 Summary of SemVer dependency specification (view table figure)

Operator

Example

Min version

Max version

Updates?

Caret

^2.3.4

>=2.3.4

<3.0.0

Allowed

Caret

^2.3

>=2.3.0

<3.0.0

Allowed

Caret

^0.2.3

>=0.2.3

<0.3.0

Allowed

Caret

^2

>=2.0.0

<3.0.0

Allowed

Tilde

~2.3.4

>=2.3.4

<2.4.0

Allowed

Tilde

~2.3

>=2.3.0

<2.4.0

Allowed

Tilde

~0.2

>=0.2.0

<0.3

Allowed

Wildcard

2.3.*

>=2.3.0

<2.4.0

Allowed

Wildcard

2.*

>=2.0.0

<3.0.0

Allowed

Wildcard

*

None

None

Allowed

Comparison

=2.3.4

=2.3.4

=2.3.4

No

Comparison

>=2.3.4

>=2.3.4

None

Allowed

Comparison

>=2.3.4,<3.0.0

>=2.3.4

<3.0.0

Allowed

Compare SemVer operators by minimum version, maximum version, and whether updates remain allowed. section 2.2

How Code Like a Pro in Rust covers it

How deep it goes
Explains registry rationale, Cargo.toml declarations, cargo add, and SemVer ranges, but does not detail lockfiles or resolution internals.
Examples
Demonstrates rand through Cargo.toml, cargo add, and a SemVer operator comparison table.
What you can do after
Operate/configure Cargo by declaring crates and selecting compatible dependency version ranges.
How it connects
Connects Cargo with Cargo.toml, crates.io, crates, random-number generation, and semantic versioning.

Also explained in

Compare how 5 books explain dependency management →

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.