Rust Learning Path — Chapter 17. Cargo is more than a command that runs rustc. It is Rust's package manager, dependency resolver, build orchestrator, test runner and release tool. Understanding Cargo.toml, Cargo.lock, features and profiles is essential once you build real applications.
1. Cargo.toml is the package manifest
[package] name = "service" version = "0.1.0" edition = "2024" [dependencies] serde = { version = "1", features = ["derive"] }The manifest describes your package and tells Cargo which dependencies and build settings it needs.
2. Add dependencies
cargo add serde --features derive cargo add tokio --features full cargo add anyhowCargo writes the corresponding dependency entries into the manifest.
3. Version requirements
A requirement such as serde = "1.0" normally allows compatible releases in the same major version. For application teams, commit Cargo.lock so everyone and CI use the same resolved versions.
4. Cargo.lock
Cargo.toml expresses allowed dependency ranges. Cargo.lock records the exact versions Cargo selected. Applications should normally commit it.
cargo update cargo treecargo tree is extremely useful when investigating duplicate versions or unexpected transitive dependencies.
5. Optional dependencies and feature flags
[features] default = [] postgres = ["dep:sqlx"] metrics = ["dep:prometheus"] [dependencies] sqlx = { version = "0.8", optional = true } prometheus = { version = "0.14", optional = true }Compile with selected features:
cargo build --features postgres,metricsFeatures should represent additive capabilities. Avoid using mutually exclusive feature combinations when a normal configuration value would be clearer.
6. Feature-gated code
#[cfg(feature = "metrics")] fn register_metrics() { println!("metrics enabled"); }This code is compiled only when the feature is enabled.
7. Default features
[features] default = ["json"] json = [] xml = []Disable defaults when needed:
cargo build --no-default-features8. Development dependencies
Dependencies used only by tests and benchmarks belong under [dev-dependencies]:
[dev-dependencies] tempfile = "3"9. Build dependencies
Build scripts use [build-dependencies]. Keep build scripts small because they execute during compilation.
10. Git and path dependencies
[dependencies] shared = { path = "../shared" } # or a Git source when necessaryPath dependencies are common in workspaces. Prefer released crates for reusable public libraries and pin external Git dependencies carefully.
11. Build profiles
Cargo provides dev and release profiles by default:
[profile.release] lto = "thin" codegen-units = 1 panic = "abort"Do not blindly enable every optimization. Measure build time, binary size and runtime performance first.
12. Workspace dependencies
[workspace] members = ["api", "core"] [workspace.dependencies] serde = { version = "1", features = ["derive"] }Members can inherit the dependency:
[dependencies] serde.workspace = true13. Useful Cargo commands
| Command | Purpose |
|---|---|
| cargo check | Fast type checking |
| cargo tree | Dependency graph |
| cargo update | Refresh compatible locked versions |
| cargo metadata | Machine-readable workspace information |
| cargo build --release | Optimized build |
14. Security and dependency hygiene
- Prefer well-maintained crates with clear ownership.
- Review transitive dependencies with cargo tree .
- Keep lockfiles in application repositories.
- Update intentionally and run tests after dependency changes.
- Use minimal feature sets to reduce unnecessary code and attack surface.
15. Mini lab
Create a CLI with an optional json feature. Build once with defaults, then with --no-default-features, and observe which code is compiled.
Key takeaways
Cargo's manifest defines what your package needs; the lockfile records the exact resolved graph; features make capabilities optional; profiles control build characteristics. Treat dependency configuration as production engineering, not boilerplate.
Join the discussion
0 CommentsAsk questions, add context, or share what worked.
More from Melochip Editorial
Learn what Playwright testing is, why modern QA teams use it, how testers can get started, and the major types...
7 min read · Sep 19, 2026
Learn how Kubernetes Deployments and ReplicaSets work with practical YAML, scaling, self-healing, rolling upda...
7 min read · Sep 17, 2026
Learn Kubernetes security best practices with RBAC, Secrets, ServiceAccounts, Pod securityContext, NetworkPoli...
6 min read · Sep 17, 2026Related articles
Learn what Playwright testing is, why modern QA teams use it, how testers can get started, and the major types...
7 min read · Sep 19, 2026
Learn how Kubernetes Deployments and ReplicaSets work with practical YAML, scaling, self-healing, rolling upda...
7 min read · Sep 17, 2026
Learn Kubernetes security best practices with RBAC, Secrets, ServiceAccounts, Pod securityContext, NetworkPoli...
6 min read · Sep 17, 2026
Learn Kubernetes monitoring and logs with kubectl logs, events, metrics-server, top commands, Prometheus, Graf...
5 min read · Sep 17, 2026Stay in the loop. Get new Melochip articles
Receive Melochip article updates when new articles are available. Explicit opt-in only.
No comments yet. Start the discussion.