Sub topic 11
Evaluating a crate before you depend on it
Adding a dependency takes one command, but it is a long-term commitment. You take on the crate's bugs, its security issues, its compile time, its license, and every crate it pulls in. This chapter gives you a checklist for deciding whether a crate is worth that cost. It assumes you can already use cargo add, read a [dependencies] table, and understand SemVer requirements and features from the earlier chapters.
Why evaluation matters
Cargo does not judge quality. Anyone can publish a crate to crates.io, and a name that looks right may be abandoned, unsound, or a typo-squat of a popular crate. A published version also cannot be deleted. Authors can only yank it, which stops new projects from selecting it but does not break builds that already have it in Cargo.lock.
A dependency also adds more than its own code:
- Its transitive dependencies (the crates it depends on) join your build.
- Its build scripts and procedural macros run code on your machine at compile time.
- Its license terms apply to what you ship.
- Its release pace becomes part of your update work.
You can reverse the decision later, but removing a crate that is used throughout your code costs far more than checking it first.
A practical checklist
Work through these questions in order. Stop as soon as one answer rules the crate out.
- Do you need a crate at all?
- Is it maintained?
- Is it used and trusted by others?
- Is the API and documentation good?
- What does it cost in dependencies, features, and build time?
- Is the license compatible with your project?
- Is the code safe to run?
The following sections cover each question.
Do you need a crate at all?
If the standard library does the job, use it. A crate that wraps ten lines of code is rarely worth a new line in your supply chain. Ask whether you need the whole crate or only a small part of it. Often a feature flag can narrow it down, as the next section on cost explains.
The reverse also holds. Do not write your own cryptography, date and time handling, or JSON parser. Those areas have many edge cases, and well-reviewed crates already cover them.
Is it maintained?
Open the crate's page on crates.io and look at these signals:
- The date of the latest release.
- The release history. Regular small releases are a good sign.
- The repository link. A crate with no repository is a warning.
- Open issues and pull requests in the repository, and whether anyone answers them.
An old release date does not always mean abandonment. A small, finished crate, such as one that implements a fixed file format, may need no changes. Look at whether bug reports get a response. Also check the rust-version field and whether the crate builds on the toolchain you use.
A crate below version 1.0 is not necessarily immature, since many widely used crates stay at 0.x. It does mean the authors have not promised stability, and, as you saw in the SemVer chapter, the 0.x caret rule lets fewer upgrades through automatically.
Is it used and trusted?
The download count and the "reverse dependencies" tab on crates.io show how many other crates depend on this one. High numbers do not prove quality, but they do mean many people will notice and report problems. Compare candidates side by side. If two crates solve the same problem, the one with more dependents and recent activity is usually the safer pick.
Check the owners list too. A crate maintained by an organization or several people is less likely to vanish than one owned by a single account.
Be careful with names. Typo-squatting is the practice of publishing a malicious crate whose name is close to a popular one, such as serde_jsonn. Always copy the name from the project's official documentation or repository, and check that the crate page links to the expected repository.
Is the API and documentation good?
Open the crate's documentation on docs.rs. Check that:
- Every public item has an explanation, and there are examples.
- The docs build successfully. A failed docs.rs build is a warning sign.
- The API matches your needs without awkward workarounds.
Reading the examples tells you how you will write code against the crate. If the first example is unclear, the rest of your experience is likely to be unclear too.
What does it cost?
The cost of a crate is mostly its dependency tree. Use cargo tree before you commit to the crate. A quick way is to add it in a scratch project and inspect the result.
cargo new eval_scratch
cd eval_scratch
cargo add reqwest
cargo tree --depth 1
cargo tree | wc -lThe last command counts the lines in the tree, which approximates how many crates (counting repeats) the dependency pulls in. Compare that number between candidates.
Features often control the cost. A crate may enable many optional parts by default. You can turn them off and select only what you need:
[dependencies]
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }Then look at what each feature enables:
cargo tree -e features
cargo tree -i some_crateThe second command is the inverse tree. It shows which of your dependencies pull in some_crate, which helps when a crate you did not expect appears.
Also measure compile time. Procedural macros and large generic-heavy crates slow clean builds, and you can see the effect with a timing report:
cargo build --timingsThis writes an HTML report listing the time spent on each crate. Run it before and after adding the dependency.
Is the license compatible?
Each crate declares a license in its metadata, shown on crates.io. Many crates use MIT OR Apache-2.0, which lets you choose either one. Others use copyleft licenses such as GPL, which can place conditions on how you distribute your own program. Check the license of every crate in the tree, not only the direct one. If your project has legal requirements, the tool cargo deny can check licenses automatically. Install it with cargo install cargo-deny and configure it in a deny.toml file.
Is the code safe to run?
Check for known problems first. The RustSec advisory database lists reported vulnerabilities and unmaintained crates. You can query it with cargo audit:
cargo install cargo-audit
cargo auditThen look at how much unsafe code the crate uses. unsafe is Rust's marker for code the compiler cannot verify, so bugs there can cause memory errors. The tool cargo geiger counts it:
cargo install cargo-geiger
cargo geigerSome unsafe is normal and expected in crates that wrap C libraries or implement low-level data structures. The question is whether it is limited, documented, and tested.
Finally, check for a build.rs file and for procedural macros. Both run arbitrary code during cargo build, before any of your code runs. Skim the build.rs in the repository. Be suspicious of one that downloads files or runs shell commands without a clear reason. Cargo does not sandbox these.
Comparing candidates
When two crates compete, record your findings in a table. Here is an example with fictional values:
| Question | crate_a | crate_b |
|---|---|---|
| Latest release | 2 months ago | 3 years ago |
| Reverse dependencies | 4,200 | 310 |
| Total crates in tree | 12 | 48 |
| License | MIT OR Apache-2.0 | GPL-3.0 |
| Known advisories | none | 1 unfixed |
| Docs with examples | yes | partial |
In this example crate_a is the clear choice. Real comparisons are rarely this one-sided, so weigh the factors against what your project needs. A command-line tool used by one team can accept more risk than a library that thousands of people will build.
The decision process
After you add the crate
Evaluation continues after the first commit:
- Commit
Cargo.lockfor applications so everyone builds the same versions. - Run
cargo auditin your continuous integration (CI) pipeline so new advisories show up. - Review
Cargo.lockchanges in pull requests. A new unexpected crate in the diff deserves a question. - Keep the version requirement loose enough to take compatible fixes. Cargo's default caret requirement does this.
- Keep your use of the crate behind a small module or wrapper if you think you may replace it later. This limits how much code changes if you switch.
Common mistakes
- Choosing by download count alone. An old, heavily downloaded crate may be unmaintained.
- Ignoring default features, which pulls in code and crates you never use.
- Checking only direct dependencies, while the risk sits in a transitive one.
- Trusting a crate's name without checking the repository link.
- Adding a large crate for one small function.
Exercise
You are choosing between two crates that parse the same file format. Crate A has 40 reverse dependencies, a release 8 months ago, a build.rs that downloads a binary from a personal web server, and a tree of 6 crates. Crate B has 900 reverse dependencies, a release 1 month ago, a tree of 25 crates, and MIT OR Apache-2.0 as the license. Which one would you pick, and which command would you run to check whether B's tree can be made smaller?
Show answer
Pick crate B. Crate A's build script downloads and runs a binary from an unverified source during the build, which is a serious risk that its small tree does not offset. B is more widely used and recently maintained, and its license is permissive. To reduce B's tree, inspect it with cargo tree -e features, then add the crate with default-features = false and enable only the features you need, and compare the result with cargo tree | wc -l.
Exercise
You added a crate with default-features = false, but cargo tree -e features still shows a default feature enabled. What is the most likely cause, and how do you find it?
Show answer
Another crate in your dependency tree depends on the same crate and enables that feature. Cargo unifies features across the tree, so one request turns it on for all. Find the source with cargo tree -e features -i the_crate, which shows which dependents enable each feature.