Cargo
Cargo is Rust's official build tool and package manager. It creates projects, resolves and downloads dependencies, compiles them in the right order, and runs your tests and binaries.
Detailed Explanation
In plain terms, Cargo is the one command you type to do anything with a Rust project. You almost never invoke the compiler, rustc, by hand.
Rust shipped a first-party build tool on purpose. In C and C++, the compiler is standard but the build system is not, so every project picks Make, CMake, Bazel, or a hand-rolled script, and every dependency arrives in a different shape. That fragmentation is a real cost: reading an unfamiliar C++ repo often starts with reverse-engineering its build. Rust decided that the build system, the dependency format, the test runner, and the documentation generator would all be one tool that behaves identically in every project. The payoff is that cargo build and cargo test work in any Rust repository you clone.
The commands you use daily are small in number. cargo new my-app creates a project skeleton. cargo build compiles it into target/. cargo run builds and then executes the binary. cargo test builds a test harness and runs every #[test] function. cargo check type-checks without producing a binary, which is much faster and is what you want in an edit-compile loop.
A project is described by two files, and confusing them is a common beginner mistake.
| File | Written by | Purpose |
|---|---|---|
Cargo.toml | You | Declares the package name, edition, and dependency requirements like serde = "1" |
Cargo.lock | Cargo | Records the exact resolved version of every dependency, including transitive ones |
The rule for Cargo.lock: commit it for binaries, so that everyone and every CI run builds byte-identical code. For libraries it matters less, because the final application's lockfile wins. Modern guidance is to commit it either way.
Cargo builds under a profile, which is a named set of compiler flags. The dev profile is the default: no optimisation, debug symbols on, and integer overflow checks enabled. The release profile, selected with --release, turns optimisation on and those overflow checks off. The difference is not small. Release builds are routinely ten to fifty times faster at runtime, and considerably slower to compile. Benchmark in release, debug in dev.
When one repository holds several related packages, you use a workspace: a top-level Cargo.toml listing member directories. Members share one target/ directory, one lockfile, and one dependency resolution, so a shared dependency is compiled once rather than once per member.
The cost of all this convenience is compile time and disk. Cargo compiles your entire dependency tree from source the first time, target/ directories grow to hundreds of megabytes, and a large tree can take minutes to build from clean. Incremental rebuilds are much cheaper, and cargo check cheaper still.
Code Examples
Cargo.toml declares what you depend on (shown as a comment; it is TOML, not Rust):
// Cargo.toml declares what you depend on (shown as a comment; it is TOML, not Rust):
//
// [package]
// name = "config-check"
// version = "0.1.0"
// edition = "2021"
//
// [dependencies]
// serde_json = "1" // means ">=1.0.0, <2.0.0", not exactly 1.0.0
// src/main.rs — `cargo run` builds and executes this.
fn main() {
let raw = r#"{"port": 8080}"#; // r#"..."# is a raw string: no escaping of quotes
let parsed: serde_json::Value =
serde_json::from_str(raw).expect("invalid JSON");
println!("port = {}", parsed["port"]); // prints: port = 8080
}fn main() {
// `cfg!` is resolved at compile time, so this branch is decided by the profile.
if cfg!(debug_assertions) {
println!("dev build: overflow checks on, little optimisation");
} else {
println!("release build: optimised, overflow checks off");
}
}
// cargo run -> prints: dev build: overflow checks on, little optimisation
// cargo run --release -> prints: release build: optimised, overflow checks offCommon Gotchas
Benchmarking a dev build tells you nothing
an unoptimised Rust binary can be an order of magnitude slower than the release build. Any performance number you report must come from `cargo run --release` or `cargo bench`.
A caret requirement is not a pinned version
`serde = "1"` accepts any 1.x release. `Cargo.lock` is what pins the exact version. Deleting the lockfile or running `cargo update` can silently move you onto a new minor release.
`cargo check` is the fast feedback loop
it runs the front end and the borrow checker but skips code generation, so it often finishes in a fraction of the time of `cargo build`. Use it while you are fixing type and borrow errors.
`target/` is disposable and should not be committed
`cargo new` writes a `.gitignore` that excludes it. If a build behaves strangely, `cargo clean` removes it and forces a full rebuild.