liveBook · Manning

Cargo.toml

As explained in Embedded Software with Rust

Cargo.toml is the TOML-formatted manifest file that declares a Rust package's metadata, dependencies, and build configuration.

Cargo.toml is the TOML-formatted manifest for a Rust package. It declares package metadata and dependencies and can configure build profiles and features. For embedded firmware, these choices help match the build to the hardware and its memory limits. section 3.1.2

[package]
name = "cortex-m-blinky"
edition = "2024"
version = "0.1.0"

[dependencies]
cortex-m = "0.7"
cortex-m-rt = "0.7"
cortex-m-semihosting = "0.5"
panic-halt = "1.0.0"
stm32f1xx-hal = { version = "0.11.0", features = ["stm32f103", "medium"] }

[profile.release]
opt-level = 'z'
codegen-units = 1
lto = true

This manifest declares an STM32 HAL dependency with hardware features and a release profile optimized for a small binary. The opt-level setting favors size, while link-time optimization further reduces the code footprint. section 3.1.2

Cargo features are named compile-time switches. A dependency can expose features for a microcontroller variant or runtime integration, allowing the build to include what the application needs rather than every available code path. section 3.1.2

[package]
name = "feature-demo"
version = "0.1.0"
edition = "2024"

[dependencies]
cortex-m = "0.7.7"
cortex-m-rt = "0.7.5"

panic-halt = { version = "1.0.0", optional = true }
panic-semihosting = { version = "0.6", optional = true }
cortex-m-semihosting = { version = "0.5", optional = true }

[features]
default = ["panic-halt"]
semihosting = ["dep:panic-semihosting", "dep:cortex-m-semihosting"]

Optional dependencies and named features select a panic handler. The default enables panic-halt; the semihosting feature enables dependencies for diagnostic output. section 3.3.2

Use conditional compilation in Rust code to select the corresponding behavior. For the preceding manifest, cargo run uses the default feature; cargo run --no-default-features --features semihosting selects the alternative. Running on hardware also requires an appropriately configured runner. section 3.3.2

Check dependency features before using a crate in a bare-metal build: desktop defaults can include std. For example, arrayvec = { version = "0.7", default-features = false } disables its default features. Use cargo tree -e features to inspect which features are enabled and how they propagate. section 3.3.4

Cargo.toml is distinct from .cargo/config.toml, which can define targets, linker arguments and runner commands. Cargo.lock records dependency versions, while memory.x specifies the firmware memory layout. section 3.6

How Embedded Software with Rust covers it

How deep it goes
Explains manifest fields, release optimization and feature selection through focused configurations and build commands.
Examples
Uses an STM32 firmware manifest and optional semihosting dependencies to demonstrate size optimization and selectable panic behavior.
What you can do after
Operate and configure a Cargo manifest to select embedded dependencies, features and release-profile settings.
How it connects
Connects Cargo features to conditional compilation, no_std, hardware abstraction layers and panic handling.

Also explained in

Compare how 5 books explain cargo.toml →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Embedded Software with Rust. The section links point to the parts of the book it draws on.