Crates are Rust’s way of sharing reusable packages. You can use external crates to add functionality like random number generation, regex matching, or HTTP requests. Cargo makes it easy to include them in your project by adding them to your Cargo.toml file.
When building something in Rust, you do not have to write everything from scratch. The Rust ecosystem has a rich set of community-maintained libraries (called crates) that solve real problems. From working with JSON to making network calls, there is probably a crate for what you need.
In this post, I will show you how to find, install, and use external crates in your project. If you have already read Cargo Basics, this will feel like a natural next step.
What Is a Crate?
A crate is a package of Rust code. It can be a library (used by other code) or a binary (an app that runs). Rust’s standard library is made up of crates. You can also create or use third-party crates.
Crates are published on crates.io, Rust’s official package registry.
Finding Crates
Go to https://crates.io and search for a keyword like: rand (random numbers), serde (serialization), regex (regular expressions), reqwest (HTTP client) or chrono (date and time).
Each crate page shows:
- Latest version
- Documentation link
- Download stats
- How to add it to Cargo.toml
You can also check https://lib.rs for better search filters.
Adding a Crate to Your Project
In your Cargo.toml, add the crate under [dependencies]:
[dependencies]
rand = "0.8"
Then run:
cargo build
Cargo downloads and compiles the crate.
If you have cargo-edit installed, you can do this with one command:
cargo add rand
Using the Crate in Code
Once added, use the use keyword to bring functions into scope:
use rand::Rng;
fn main() {
let mut rng = rand::thread_rng();
let num = rng.gen_range(1..=10);
println!("Random number: {}", num);
}
You can now use that crate’s functionality in your app.
Managing Versions
Each crate version follows semantic versioning (major.minor.patch). Use version ranges to stay up to date:
- “1.2.3” → exactly version 1.2.3
- “^1.2.3” → compatible updates (like 1.3, but not 2.0)
Cargo.lock locks the exact versions so your builds stay consistent.
Working with Features
Some crates offer optional features. Enable them like this:
serde = { version = "1.0", features = ["derive"] }
This allows you to customize what parts of a crate you use.
Checking Documentation
Every crate links to its docs on https://docs.rs. For example:
Always read the documentation for examples and usage tips.
Removing a Crate
Just delete the line from Cargo.toml and run:
cargo build
Or use cargo rm (if cargo-edit is installed):
cargo rm rand
Common External Crates
Crate | Purpose |
---|---|
rand | Random number generation |
serde | Serialization (JSON, etc.) |
regex | Regular expressions |
chrono | Date and time utilities |
reqwest | HTTP client for API calls |
tokio | Asynchronous runtime |
Summary
External crates are one of Rust’s biggest strengths. They allow you to add powerful features to your project without writing everything from scratch. With Cargo, adding and managing dependencies is simple, safe, and well-documented.