Crates
What a crate is
A crate is a compilation unit—your binary or library—and also a package published to crates.io.
Add a dependency
Edit Cargo.toml or use cargo add.
cargo add rand
# Or in Cargo.toml:
# [dependencies]
# rand = "0.8"
Use the crate
Import items from the dependency in your code.
use rand::Rng;
fn main() {
let n = rand::thread_rng().gen_range(1..=6);
println!("rolled {n}");
}
Dependency tips
- Run
cargo doc --opento read docs locally - Pin versions thoughtfully as projects grow
- Prefer well-maintained crates with clear licenses
- Binary crates have
main.rs; library crates uselib.rs
Comments
One comment per signed-in account. Comments are saved with this page’s URL.