Rust reference
Operational lookup companion to the Rust ecosystem architecture: toolchains, Cargo structure, dependency resolution, features, profiles, targets, build gates, publication, and graph inspection. Current to Rust 1.98.0, snapshot 2026-09-03.
Toolchains, channels, and components¶
| Command | Purpose | Architecture note |
|---|---|---|
rustup show | Show active/default toolchains, installed targets, and overrides | First diagnostic when Cargo and the editor disagree |
rustup update stable | Update the stable channel and installed components | Current snapshot: 1.98.0 |
rustup toolchain install 1.98.0 | Install an exact archived toolchain | Useful for reproducing a historical build |
rustup override set stable | Set a directory override | A committed rust-toolchain.toml is visible to every checkout |
rustup component add clippy rustfmt | Add optional toolchain components | Availability can differ by toolchain and host |
rustup target add <triple> | Install a target standard library | Does not install the target linker, SDK, or native libraries |
cargo +nightly ... | Run a Cargo command through the nightly proxy | Pin dated nightly when a build depends on unstable behavior |
rustup command surface. Toolchain selection precedes Cargo resolution: the selected Cargo and rustc versions can change accepted manifests, resolver behavior, diagnostics, and artifacts. Sources: The rustup book: Toolchains; The rustup book: Components; The rustup book: Cross-compilation.
# rust-toolchain.toml — pin what CI and local proxies select.
[toolchain]
channel = "1.98.0"
profile = "minimal"
components = ["clippy", "rustfmt"]
targets = ["wasm32-unknown-unknown"]
The stable channel is the deployment default for most projects. Beta tests the next release; nightly exposes unstable compiler and Cargo features and may occasionally lack optional components. Treat a nightly date as a dependency when it is required by a build.
Project anatomy¶
| Term | Identity | What it controls |
|---|---|---|
| Workspace | One or more packages managed together | Root Cargo.toml, one Cargo.lock, shared target/ |
| Package | Versioned and publishable unit | One Cargo.toml; contains one or more targets |
| Target | A buildable library, binary, example, test, or benchmark | Every target compiles as a crate |
| Crate | One compilation unit and trait-coherence boundary | Rooted at lib.rs, main.rs, or an explicit path |
| Module | Name and privacy hierarchy within a crate | Inline or loaded from another source file |
| Feature | Additive conditional capability of a package | Affects dependency and cfg activation |
| Profile | Compiler settings for a kind of build | dev, release, test, bench, or custom |
| Target triple | Architecture-vendor-OS-environment output contract | Selects cfg, ABI, libraries, and linker |
The package-model vocabulary. In particular, package and crate are not synonyms. Source: The Cargo Book: Glossary; The Cargo Book: Workspaces.
workspace/
├── Cargo.toml # [workspace], shared dependencies/lints/profiles
├── Cargo.lock # concrete resolution for the workspace
├── rust-toolchain.toml # compiler/tool selection (rustup)
├── crates/
│ ├── domain/ # library package: stable application concepts
│ ├── adapters/ # library package: DB/network integrations
│ └── service/ # binary package: composition root
└── .cargo/config.toml # target/linker/build environment policy
Keep dependency direction visible: domain packages should not need transport, database, or runtime adapters. The binary package is usually the composition root that selects implementations and operational policy. This is an architectural convention, not a rule Cargo infers.
Manifest and workspace keys¶
| Table | Carries | Boundary |
|---|---|---|
[package] | name, version, edition, rust-version, licence, repository, publish policy | Package identity and compatibility declaration |
[dependencies] | normal target dependencies | Enter library/binary artifacts |
[dev-dependencies] | tests, examples, benchmarks | Ignored when this package is a dependency |
[build-dependencies] | dependencies of build.rs | Compile and execute for the host |
[target.'cfg(...)'.dependencies] | target-conditional graph edges | Resolver considers target conditions; activation differs by resolver/build |
[features] | named additive cfg/dependency activation | Public API and build-matrix surface |
[lib], [[bin]] | target paths, names, crate types | Compilation and linkage units |
[workspace] | members, resolver, shared metadata/dependencies/lints | Policy at repository/package-graph scope |
[profile.*] | optimization, debug info, panic, LTO, codegen units | Artifact behavior and cost |
[lints] | rustc and Clippy levels/priorities | Versioned quality policy |
High-impact stable Cargo.toml tables. Source: The Cargo Book: The Manifest Format; The Cargo Book: Workspaces; The Cargo Book: Lints.
[workspace]
members = ["crates/domain", "crates/adapters", "crates/service"]
resolver = "3"
[workspace.package]
edition = "2024"
rust-version = "1.85"
license = "MIT OR Apache-2.0"
[workspace.dependencies]
serde = { version = "1", features = ["derive"] }
[workspace.lints.rust]
unsafe_code = "forbid"
[profile.release]
lto = "thin"
panic = "abort"
Workspace inheritance centralizes a constraint; it does not automatically make every dependency public to
every member. A member still opts in with dependency.workspace = true. Profile sections are read
from the workspace root. A virtual workspace has no root package edition from which to infer a resolver, so
declare it explicitly.
Dependencies, features, and resolution¶
| Mechanism | Meaning | Design consequence |
|---|---|---|
| Version requirement | Allowed releases, commonly caret compatibility | Intent in Cargo.toml |
| Resolved package ID | Name + exact version + source | Concrete node in Cargo.lock/metadata |
| Default features | Enabled unless default-features = false | Part of a crate's default architecture |
| Optional dependency | Activated by a feature | Still an additive graph capability |
| Feature unification | Union within the active resolver boundary | A dependency is not built once per caller |
| Resolver 2 | Separates some target/build/dev feature activation | Default for edition 2021 |
| Resolver 3 | Adds MSRV-aware incompatible-version fallback | Default for edition 2024; Rust 1.84+ |
| [patch] | Registry/source overlay selected at workspace root | Testing or replacing a dependency source |
Cargo dependency-resolution concepts. Sources: The Cargo Book: Dependency Resolution; The Cargo Book: Features; The Cargo Book: Workspaces.
[features]
default = ["tls-rustls"]
tls-rustls = ["dep:rustls"]
metrics = ["dep:metrics"]
[dependencies]
rustls = { version = "0.23", optional = true }
metrics = { version = "0.24", optional = true }
Features should be additive: enabling two callers’ feature sets must not silently select incompatible global modes. If two backends are mutually exclusive, prefer separate adapter crates, an explicit runtime constructor, or a build error that makes the illegal combination visible. Test default, no-default, all, and supported feature combinations rather than assuming one successful build covers the graph.
cargo tree --workspace --target all --all-features
cargo tree -e features
cargo tree -d # duplicate package versions
cargo metadata --format-version 1 # machine-readable resolved graph
cargo update -p package --precise 1.2.3
Build, test, lint, and documentation commands¶
| Command | What it establishes | Limit |
|---|---|---|
cargo check --workspace --all-targets | Type-check without final code generation | Fast structural gate; does not prove linking or runtime behavior |
cargo build --locked | Build without changing Cargo.lock | CI/release lock discipline |
cargo test --workspace | Unit, integration, and documentation tests | Builds test harnesses |
cargo fmt --all -- --check | Check rustfmt output | Textual consistency gate |
cargo clippy --all-targets --all-features -- -D warnings | Compiler-integrated lints | Feature-complete lint gate; may expose combinations not shipped |
cargo doc --no-deps --document-private-items | Generate API documentation | Useful for reviewing module and public/private structure |
cargo fix | Apply machine-applicable compiler suggestions | Review the resulting diff |
cargo bench | Run benchmark targets | Stable statistical harness usually comes from a crate |
cargo miri test | Interpret tests under Miri | Additional UB checking; not a general executor |
A layered local/CI gate. No one command establishes correctness, portability, soundness, or supply-chain trust. Sources: The Cargo Book: Commands; The Cargo Book: cargo test; The Cargo Book: cargo doc; Rust tools; Miri.
Integration tests under tests/ compile as separate crates and exercise only public API. Unit
tests inside modules can reach private items. Documentation tests keep examples executable. Cross-target tests
need a runner or deployment environment; compiling for another target is not the same as executing there.
Profiles, artifacts, and reproducibility¶
| Setting/profile | Role | Tradeoff |
|---|---|---|
| dev | opt-level 0, debug assertions on, overflow checks on | Fast edit/build loop |
| release | opt-level 3, debug assertions off, overflow checks off | Deployment baseline; tune explicitly |
| test | Inherits dev | Test harness and dependencies |
| bench | Inherits release | Benchmark targets |
| lto | off, thin, fat, or linker-plugin | Cross-unit optimization versus link time |
| codegen-units | Partitions code generation | Parallel compile speed versus optimization opportunity |
| panic | unwind or abort | Binary size and cleanup/unwind behavior |
| strip / debug | Symbol retention and debug-info level | Artifact size versus postmortem diagnosis |
Cargo profile controls with architecture impact. Defaults can evolve; inspect the linked reference for the selected toolchain. Source: The Cargo Book: Profiles; The Cargo Book: Build Cache.
--locked refuses lockfile changes. --offline refuses network access.
--frozen requests both. They constrain Cargo’s inputs but do not by themselves make every build
bit-for-bit reproducible: native toolchains, environment variables, paths, timestamps, build scripts, and
linker behavior may still vary. Preserve the toolchain file, lockfile, target, native dependencies, and build
environment as one release input.
The target directory contains final and intermediate artifacts, fingerprints, incremental caches, build-
script outputs, generated docs, and package archives. With --target, host build scripts and proc
macros are separated from target artifacts. Do not treat paths inside the internal build directory as a stable
API; consume declared outputs and Cargo metadata instead.
Targets, no_std, linking, and FFI¶
| Deployment shape | Library/runtime boundary | Typical artifact |
|---|---|---|
| Hosted native | std + target runtime/ABI | bin, rlib, dylib, cdylib, staticlib |
| no_std + alloc | core plus allocator-backed collections | Kernel, firmware, constrained runtime |
| no_std + core | No allocation or OS assumption | Bare-metal or highly constrained target |
| WebAssembly | Target std/core plus host imports | wasm module, often with generated host glue |
| C ABI | extern declarations and repr(C) data | staticlib/cdylib or native dependency |
| Python extension | PyO3 and Python runtime contract | cdylib packaged as a Python module |
| C++ bridge | CXX-generated bridge plus C++ toolchain | Linked native library/binary |
Deployment shapes and their explicit runtime boundary. Sources: rustc Platform Support; The rustup book: Cross-compilation; The Embedded Rust Book: no_std; The Rustonomicon: FFI; PyO3 user guide; CXX Rust/C++ interop; The wasm-bindgen Guide.
# Show target facts from the selected compiler.
rustc --print target-list
rustc --print cfg --target x86_64-unknown-linux-gnu
# Install Rust's target library, then provide the required linker/SDK separately.
rustup target add wasm32-unknown-unknown
cargo build --release --target wasm32-unknown-unknown
#[repr(C)] requests a C-compatible representation for supported types; it does not make Rust
strings, vectors, trait objects, enums with data, or panics into portable C APIs. Prefer opaque handles,
fixed-width scalars, explicit byte spans, paired allocation/free functions, and status/error values. State
which side owns every allocation and never unwind through a foreign ABI that does not permit it.
Publication and compatibility¶
| Command | Effect | Boundary |
|---|---|---|
cargo package --list | Inspect files selected for the crate archive | Run before publication |
cargo package | Create and verify a local .crate archive | Exercises packaged, not checkout, inputs |
cargo publish --dry-run | Run publication checks without upload | Does not reserve the version |
cargo publish | Upload an immutable version to the selected registry | Ownership/token required |
cargo yank --version X | Prevent new resolutions from selecting a version | Does not delete it or break locks |
Cargo publication lifecycle. crates.io is designed as a permanent archive: yanking changes future resolution, not existing locked builds. Source: The Cargo Book: Publishing on crates.io; The Cargo Book: Registry Index.
Cargo’s SemVer guidance treats more changes as breaking than signature comparison alone suggests:
removing public items, tightening generic bounds, adding trait requirements, changing feature behavior, or
exposing new trait implementations can affect downstream type inference and coherence. Run downstream tests
and inspect public API diffs, but keep the human compatibility contract explicit. The rust-version
field declares the minimum supported Rust version; testing it remains the publisher’s responsibility. Source:
The Cargo Book: SemVer Compatibility; The Cargo Book: The Manifest Format.
Inspection and assurance workflow¶
| Question | Mechanism | Evidence produced |
|---|---|---|
| Resolved topology | cargo metadata; cargo tree | Packages, sources, targets, features, duplicate versions |
| Build execution | build.rs/proc-macro inventory | Which dependencies run on the host and why |
| Unsafe boundary | lint plus code/invariant review | Where compiler obligations become human obligations |
| Advisories | cargo audit / RustSec | Known vulnerable or unmaintained package records |
| Policy | cargo deny | Allowed licences, sources, bans, duplicates, advisories |
| Review evidence | cargo vet | Audits and trusted imports for dependency criteria |
| Dynamic behavior | tests, sanitizers where applicable, Miri | Executed paths under distinct models |
| Performance | criterion plus workload/system measurements | Regression distributions and operational cost |
Review layers for a Rust dependency graph. Each answers a different question; passing one is not evidence for the others. Sources: The Cargo Book: cargo metadata; The Cargo Book: Build Scripts; The Rustonomicon: Working with Unsafe; RustSec Advisory Database; cargo-deny documentation; cargo-vet documentation; Miri; Criterion.rs documentation.
# A compact release gate; add target- and project-specific jobs around it.
cargo fmt --all -- --check
cargo check --workspace --all-targets --locked
cargo clippy --workspace --all-targets --all-features -- -D warnings
cargo test --workspace --locked
cargo doc --workspace --no-deps
cargo tree -d
A deep review records exceptions: why a duplicate version remains, which native toolchain is trusted, which unsafe invariant was checked, which features are intentionally excluded, and which targets were compiled versus executed. The command transcript is evidence only when paired with the exact toolchain, lock graph, target, and configuration that produced it.
Sources¶
Official Rust and Cargo documentation defines the toolchain. Project-owned documentation defines each optional ecosystem tool. Snapshot date: 2026-09-03.
- The rustup book: Toolchains, The Rust Project.
- The rustup book: Components, The Rust Project.
- The rustup book: Cross-compilation, The Rust Project.
- The Cargo Book: Glossary, The Rust Project.
- The Cargo Book: The Manifest Format, The Rust Project.
- The Cargo Book: Workspaces, The Rust Project.
- The Cargo Book: Dependency Resolution, The Rust Project.
- The Cargo Book: Features, The Rust Project.
- The Cargo Book: Profiles, The Rust Project.
- The Cargo Book: Build Cache, The Rust Project.
- The Cargo Book: Commands, The Rust Project.
- The Cargo Book: cargo test, The Rust Project.
- The Cargo Book: cargo doc, The Rust Project.
- The Cargo Book: Publishing on crates.io, The Rust Project.
- The Cargo Book: Registry Index, The Rust Project.
- The Cargo Book: SemVer Compatibility, The Rust Project.
- The Cargo Book: Lints, The Rust Project.
- The Cargo Book: cargo metadata, The Rust Project.
- Rust tools, The Rust Project.
- Miri, The Rust Project.
- rustc Platform Support, The Rust Project.
- The Embedded Rust Book: no_std, The Rust Project.
- The Rustonomicon: FFI, The Rust Project.
- The Rustonomicon: Working with Unsafe, The Rust Project.
- PyO3 user guide, PyO3 project.
- CXX Rust/C++ interop, CXX project.
- The wasm-bindgen Guide, wasm-bindgen project.
- RustSec Advisory Database, RustSec.
- cargo-deny documentation, Embark Studios.
- cargo-vet documentation, Mozilla.
- Criterion.rs documentation, Criterion project.