Sub topic 9
Adding a dependency from crates.io
This chapter assumes you can run cargo new and cargo run, and that you have read the Cargo.toml chapter. You should know that [dependencies] is a table in the manifest and that it starts out empty.
What a dependency is
A dependency is a crate (a Rust package) that your project uses. Most public crates live on crates.io, the default package registry. Cargo downloads them, compiles them, and links them into your build.
Two files describe your dependencies:
Cargo.tomlstates what you want, as a version requirement such asrand = "0.8.5".Cargo.lockrecords the exact versions Cargo actually resolved, including dependencies of dependencies.
Adding a crate with cargo add
Inside your project directory, run:
cargo add randCargo contacts the registry, picks the latest compatible version, and inserts a line into the [dependencies] table. The result looks like this. The version number on your machine will differ.
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
[dependencies]
rand = "0.8.5"The string "0.8.5" is a caret requirement. It means "any version at least 0.8.5 that is semver-compatible". For versions below 1.0, compatible means the same minor number, so 0.8.9 is allowed and 0.9.0 is not.
The next cargo build or cargo run downloads rand and its own dependencies, compiles them, and writes the exact versions to Cargo.lock.
Using the crate
Adding the crate to the manifest does not import it into your code. You still need a use declaration. This example is written for rand 0.8, so pin that version if cargo add selected a newer release:
use rand::Rng;
fn main() {
let mut rng = rand::thread_rng();
let roll: u32 = rng.gen_range(1..=6);
println!("You rolled a {roll}");
}Run it with cargo run. The first run is slower because Cargo compiles rand as well. Later runs reuse the compiled result.
To request a specific version, put it after the name:
cargo add [email protected]What happens during a build
The diagram shows how Cargo turns the manifest into a build.
Once Cargo.lock exists, Cargo keeps using the locked versions. A new release of rand does not change your build until you ask for it.
Updating dependencies
To move to newer compatible versions, run cargo update. It rewrites Cargo.lock within the limits set in Cargo.toml. To move across an incompatible version, such as 0.8 to 0.9, change the requirement in the manifest (for example with cargo add [email protected]).
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `unresolved import rand` | The crate is not in `[dependencies]`, or you misspelled its name | Run `cargo add rand` and check the spelling |
| `no method named gen_range` or similar API errors | The code targets a different major version than the one installed | Pin the version your example expects |
| `failed to get ... as a dependency` | No network access or a proxy is blocking the registry | Check connectivity, then retry |
| Different versions on two machines | `Cargo.lock` is not shared | Commit `Cargo.lock` for applications |
Exercises
Exercise
You run cargo add rand and then cargo build. Which file records the exact version of rand that was chosen, and which file records the version requirement you asked for?
Show answer
Cargo.lock records the exact resolved version, and Cargo.toml records the requirement, such as rand = "0.8.5".
Exercise
Your Cargo.toml says rand = "0.8.5" and Cargo.lock pins 0.8.5. Version 0.8.7 is released. Does cargo build use it? What do you run to pick it up?
Show answer
No. cargo build keeps using the locked 0.8.5. Run cargo update to move to 0.8.7, which is allowed by the requirement.