Originally published on php2rust.dev — part of the PHP to Rust course. View this lesson on the original site.

Crates.io is the official package registry for Rust. You can add dependencies in two ways: by editing Cargo.toml directly, or by using cargo add.

Example, adding the rand crate for random number generation:

cargo add rand

This updates your Cargo.toml, adds rand under [dependencies], and downloads the latest version from Crates.io.

What's next

Next: Cargo.lock: keeping dependencies consistent.

From PHP to Rust — Getting Started with Rust