Answer#
A dependency is code written by someone else that runs inside your process, often with access to everything your program can reach. Adding one is a trust decision. A short checklist makes that decision visible.
| Question | How to look |
|---|---|
| What does it add? | cargo tree -p <crate> after adding it, or cargo tree for the whole graph. Count new crates, not just one |
| Who is behind it? | The repository, the owners on crates.io, how many people contribute, whether releases are regular |
| Is it used by others? | Download counts and "used by" on crates.io are a signal of use, not of safety |
| Does it run code at build time? | Look for a build.rs file and for proc-macro = true in its Cargo.toml (see the thread on build scripts) |
Does it use unsafe? | Search the source for unsafe; cargo geiger can count it. Some use is normal in low-level crates |
| Known problems? | Search rustsec.org, then run cargo audit or cargo deny |
| Features | Disable default features you do not need: default-features = false and enable only what you use |
| Licence | Check it fits your policy (cargo deny check licenses) |
| Do you need it? | A dozen lines of your own code can be better than a dependency with fifty others |
The tree of the test project in the thread on Cargo.lock has nine crates, though only two were named. That multiplication is typical, and it is why the number of new crates matters.
How to do it#
- Add the crate on a branch.
- Run
cargo treeand look at what else arrived. - Open the crate's repository and read its
Cargo.toml,build.rs(if any) and the code that touches files, the network or processes. - Run
cargo auditorcargo deny check. - Pin the result in
Cargo.lock, and note in the pull request why the crate was chosen. - Consider supply-chain tools that record review:
cargo vetandcargo crevexist for sharing audits; check their documentation to see if they suit your team.
Common mistakes#
- Adding a crate from a name seen in a tutorial without checking that it is the right one: look-alike names (typosquatting) exist on every package registry.
- Taking the first search result of crates.io without reading what is behind it.
- Never removing dependencies that are no longer used (
cargo-udepsorcargo-machetecan find them).
Related questions#
Is a popular crate always safe?#
No. Popularity lowers some risks and raises others (it is a more attractive target). Treat it as one signal among several.
Should I copy code instead of adding a dependency?#
Only for small, well-understood pieces, and check the licence first. Copied code no longer receives fixes, so you take on that maintenance.
Sources#
- The Cargo Book, "Specifying Dependencies": https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html
- RustSec: https://rustsec.org/
- The Cargo Book, "Features": https://doc.rust-lang.org/cargo/reference/features.html