What is Cargo.toml
Cargo.toml is the manifest and configuration file for a Rust project. It acts as a project's control center, defining its name, version, and external libraries(dependencies). Example Cargo.toml:
[package]
name = "linked_list" // name of program
version = "0.1.0" // current version
edition = "2024" // Rust edition in your code
// Lists the external libraries (crates) needed by project
// Cargo automatically fetches these packages from crates.io and compiles them
[dependencies]
serde = { version = "1.0", features = ["derive"] }
reqwest = "0.12"
[dev-dependencies] // Libraries required only for running tests
[build-dependencies] // Crates required for your build scripts (e.g., build.rs)
[features] // compile-time flags to enable conditional compilation
// section to manage different crates from 1 location
[workspace]
Sections on Cargo.toml
workspace
[workspace]: header in root Cargo.toml, we define that directory
as the "root" of a multi-crate project.
[members]: which subdirectories contain the crates that belong to
this workspace. When you run a command like cargo build from the root,
Cargo looks at these members and builds them together
resolver = "2": This is Feature Resolver version. This came in
Rust 2021. It fixed feature bloat issues in Rust
{ workspace = true }: Workspace inheritance. Don't look for a
version number here. Go to the root Cargo.toml, find the tokio version
and features are defined there.
Project Structure
|- Cargo.toml //main
|- crates
|- module1
|- Cargo.toml
|- src/main.rs
|- module2
|- Cargo.toml
|- src
|- module21
|- main.rs
| Cargo.toml //Root | module1/Cargo.toml |
|---|---|
|
|