Cargo.toml for a Rust Project
Cargo.toml is the manifest file for Rust projects, defining the package metadata, dependencies with version constraints, feature flags, and build profiles. This example shows a binary crate with common dependencies, a library crate with feature flags, and separate profile settings for development and release builds. The TOML validator catches syntax errors and malformed version strings before you run cargo build. Enabling link-time optimization and codegen-units = 1 in the release profile maximizes performance at the cost of slower compile times.
[package] name = "my-app" version = "0.1.0" edition = "2021" authors = ["Your Name <[email protected]>"] description = "A fast CLI tool" license = "MIT" [dependencies] tokio = { version = "1", features = ["full"] } serde = { version = "1", features = ["derive"] } serde_json = "1" clap = { version = "4", features = ["derive"] } anyhow = "1" reqwest = { version = "0.11", features = ["json"] } [dev-dependencies] tokio-test = "0.4" [profile.release] opt-level = 3 lto = true codegen-units = 1 strip = true
FAQ
- What is the difference between dependencies and dev-dependencies?
- dependencies are compiled into your final binary. dev-dependencies are only compiled for tests and benchmarks, reducing production binary size and build time.
- How do Cargo features work?
- Features are opt-in functionality flags. Crates can gate code behind features, and consumers enable them in Cargo.toml. This avoids compiling unused code and reduces binary size.
- What does strip = true do in the release profile?
- strip = true removes debug symbols from the compiled binary, reducing its file size significantly. Stripped binaries cannot be debugged with a source-level debugger but are smaller for distribution.
Related Examples
Enabling strict mode in TypeScript activates a collection of checks that catch t...
EditorConfig for a Multi-Language ProjectEditorConfig enforces consistent code style rules across different editors and I...
Maven pom.xml for a Java ProjectThe Maven Project Object Model (pom.xml) is the build configuration file for Jav...