Cargo and Package Management
Cargo.toml
The Cargo.toml file is the manifest of a Rust package, written in the TOML (Tom's Obvious, Minimal Language) format. It declares package metadata, the Rust edition, minimum supported Rust version (rust-version), compilation targets, dependency graphs, feature flags, and build profiles.
Cargo.toml Sections:
[package] --> Name, version, edition = "2024", rust-version, license
[lib] / [[bin]] --> Explicit target path and crate-type overrides
[dependencies] --> Runtime library dependencies
[dev-dependencies] --> Test, example, and benchmark-only dependencies
[build-dependencies] --> Dependencies available to `build.rs`
[features] --> Conditional compilation feature flags
[profile.*] --> Optimization, debuginfo, LTO, and panic settings
[lints] --> Compiler (`[lints.rust]`) and Clippy (`[lints.clippy]`) rules
Complete Cargo.toml Manifest Reference
[package]
name = "telemetry_agent"
version = "0.3.1"
edition = "2024"
rust-version = "1.85"
authors = ["Platform Team <platform@example.com>"]
description = "Lightweight structured telemetry collector and batch shipper"
license = "MIT OR Apache-2.0"
repository = "https://github.com/example/telemetry_agent"
readme = "README.md"
keywords = ["telemetry", "metrics", "observability"]
categories = ["development-tools::profiling", "network-programming"]
[dependencies]
serde = { version = "1.0", features = ["derive"] }
bytes = "1.9"
[dev-dependencies]
proptest = "1.6"
[features]
default = ["compression"]
compression = []
[lints.rust]
unsafe_code = "forbid"
[lints.clippy]
all = { level = "warn", priority = -1 }
unwrap_used = "warn"
[profile.release]
lto = "thin"
codegen-units = 1
strip = "symbols"
Key [package] Fields
| Field | Required? | Purpose |
|---|---|---|
name |
Yes | Package identifier ([a-zA-Z0-9_-]). Hyphens - in package names are automatically converted to underscores _ in Rust code (use telemetry_agent::...). |
version |
Yes (default "0.1.0") |
Semantic version string (MAJOR.MINOR.PATCH). |
edition |
Recommended ("2024") |
Selects the Rust language and Cargo resolver edition ("2015", "2018", "2021", "2024"). Omitting it defaults to "2015". |
rust-version |
Optional | Minimum Supported Rust Version (MSRV), e.g., "1.85". In edition = "2024", Cargo's v3 resolver prefers dependency versions compatible with this rust-version. |
license / license-file |
Required for crates.io |
SPDX 2.1 license expression (such as "MIT OR Apache-2.0"). |
publish |
Optional (true) |
Setting publish = false prevents accidental cargo publish of internal proprietary packages. |
default-run |
Optional | Specifies which binary cargo run executes by default when a package has multiple binaries. |
Configuring Lints in Cargo.toml ([lints])
Instead of repeating #![warn(...)] attributes at the top of src/lib.rs, src/main.rs, and every binary target, declaring [lints.rust] and [lints.clippy] in Cargo.toml applies those lint levels uniformly across every target in the package.
pub fn parse_metric_id(raw: &str) -> Option<u64> {
raw.strip_prefix("m_")?.parse().ok()
}
fn main() {
println!("Metric ID: {:?}", parse_metric_id("m_4096"));
}
Metric ID: Some(4096)