Sub topic 11
Evaluating a crate before you depend on it
This chapter assumes you can add a dependency with cargo add, read a version requirement, and run cargo tree. Here you will learn how to judge a crate before it enters your project. Every dependency is code you ship, build and maintain. A short check up front is cheaper than replacing a crate later.
Why evaluation matters
A dependency brings its own code and its own dependencies. If the crate is abandoned, buggy or unsafe, your project inherits the problem. You also trust its authors, because the code runs with the same permissions as your program. Build scripts and procedural macros run on your machine at compile time.
Signals to check
No single signal decides the question. Look at several and weigh them against how important the dependency is to your project.
| Signal | Where to look | What it tells you |
|---|---|---|
| Downloads | crates.io crate page | How widely the crate is used. High numbers mean more eyes on it, but not that it is good. |
| Recent releases | Versions tab | Whether anyone maintains it. Old releases are fine for small, finished crates. |
| Documentation | docs.rs page | Whether you can learn the API without reading its source. |
| Repository | Repository link | Open issues, response times, tests and CI status. |
| Version | Version number | A `0.x` crate may break compatibility on any minor release. |
| Licence | License field | Whether it fits your project's terms. |
| Dependents | "Reverse dependencies" on crates.io | Which well-known projects rely on it. |
A missing repository link, an empty documentation page, or a crate name that closely imitates a popular one is a warning sign. Typosquatting, where an attacker publishes a similarly named crate, is a real risk. Check the exact spelling and the publisher before you run cargo add.
Measure the cost of a dependency
A crate that looks small can pull in many others. Add it in a scratch project first, then inspect what you get.
cargo new eval_demo
cd eval_demo
cargo add serde_json
cargo tree
cargo tree -e featuresThe first cargo tree shows every crate that will be compiled. The second shows which features are enabled. Count the crates, and look for ones you did not expect. If the tree is large for a small job, check whether turning off default features shrinks it. You can also time a clean build with cargo build to see the compile-time cost.
[dependencies]
serde_json = "1"Check the code you will run
For important dependencies, spend a few minutes reading the source. Look for these points:
- Does the crate use
unsafe? Some use is normal, but a lot of it needs a closer look. - Does it have a
build.rsfile? Build scripts run arbitrary code during compilation. - Is it a procedural macro crate? These also run at compile time.
- Does it have tests, and do they pass?
The cargo doc --open command builds the documentation for your whole dependency tree locally, which helps you compare the API of candidate crates.
cargo doc --openDecide using alternatives
Compare at least two candidates when the dependency matters. The standard library or a few lines of your own code may be the better choice for a trivial task. Writing your own small function avoids a dependency, but do not do this for security-sensitive code such as cryptography or parsing untrusted input. A well-reviewed crate is safer there.
Common mistakes
- Choosing the crate with the most downloads without reading its documentation.
- Adding a large crate for one small function.
- Ignoring licence terms until release time.
- Never revisiting a dependency after it stops receiving updates.
Exercise
You find two crates that do the same job. Crate A has many downloads but its last release was four years ago and it has a build script. Crate B has fewer downloads, was updated last month, and has no build script. Which checks would you run before choosing, and what would make you pick A anyway?
Show answer
Check issues and CI on both repositories, read the documentation, run cargo tree in a scratch project to compare their dependency counts, and check the licence. Also look at whether A's age reflects a finished, stable crate with few open issues, since small complete crates may not need updates. You would pick A if it is clearly stable, its issues are answered, the build script is harmless, and its dependency tree is smaller or its API fits better.