Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Rust Engineering Practices — Beyond cargo build
Rust 工程实践:超越 cargo build

Speaker Intro
讲者简介

  • Principal Firmware Architect in Microsoft SCHIE (Silicon and Cloud Hardware Infrastructure Engineering) team
    微软 SCHIE 团队首席固件架构师。
  • Industry veteran with expertise in security, systems programming (firmware, operating systems, hypervisors), CPU and platform architecture, and C++ systems
    长期从事安全、系统编程、固件、操作系统、虚拟机监控器、CPU 与平台架构,以及 C++ 系统开发。
  • Started programming in Rust in 2017 (@AWS EC2), and have been in love with the language ever since
    自 2017 年在 AWS EC2 开始使用 Rust,此后持续深耕这门语言。

A practical guide to the Rust toolchain features that most teams discover too late: build scripts, cross-compilation, benchmarking, code coverage, and safety verification with Miri and Valgrind. Each chapter uses concrete examples drawn from a real hardware-diagnostics codebase — a large multi-crate workspace — so every technique maps directly to production code.
这是一本偏工程实践的指南,专门讲那些很多团队往往接触得太晚的 Rust 工具链能力:构建脚本、交叉编译、基准测试、代码覆盖率,以及借助 Miri 和 Valgrind 做安全验证。每一章都围绕一个真实的硬件诊断代码库展开,这个代码库是一个大型多 crate 工作区,因此里面的每个技巧都能直接映射到生产代码。

How to Use This Book
如何使用本书

This book is designed for self-paced study or team workshops. Each chapter is largely independent — read them in order or jump to the topic you need.
这本书既适合个人自学,也适合团队工作坊。各章节之间大体独立,可以按顺序阅读,也可以直接跳到当前最需要的主题。

Difficulty Legend
难度说明

SymbolLevelMeaning
🟢Starter
入门
Straightforward tools with clear patterns — useful on day one
模式清晰、上手直接,第一天就能用起来。
🟡Intermediate
中级
Requires understanding of toolchain internals or platform concepts
需要理解工具链内部机制或平台概念。
🔴Advanced
高级
Deep toolchain knowledge, nightly features, or multi-tool orchestration
涉及深层工具链知识、nightly 特性或多工具协同。

Pacing Guide
学习节奏建议

PartChaptersEst. TimeKey Outcome
I — Build & Ship
第一部分:构建与交付
ch01–02
第 1–2 章
3–4 h
3–4 小时
Build metadata, cross-compilation, static binaries
掌握构建元数据、交叉编译与静态二进制。
II — Measure & Verify
第二部分:度量与验证
ch03–05
第 3–5 章
4–5 h
4–5 小时
Statistical benchmarking, coverage gates, Miri/sanitizers
掌握统计型基准测试、覆盖率门禁和 Miri / sanitizer 验证。
III — Harden & Optimize
第三部分:加固与优化
ch06–10
第 6–10 章
6–8 h
6–8 小时
Supply chain security, release profiles, compile-time tools, no_std, Windows
掌握供应链安全、发布配置、编译期工具、no_std 和 Windows 相关工程问题。
IV — Integrate
第四部分:集成
ch11–13
第 11–13 章
3–4 h
3–4 小时
Production CI/CD pipeline, tricks, capstone exercise
掌握生产级 CI/CD 流水线、实战技巧和综合练习。
总计16–21 h
16–21 小时
Full production engineering pipeline
建立完整的生产工程能力视角。

Working Through Exercises
练习建议

Each chapter contains 🏋️ exercises with difficulty indicators. Solutions are provided in expandable <details> blocks — try the exercise first, then check your work.
每一章都带有按难度标记的 🏋️ 练习。答案放在可展开的 <details> 块里,建议先自己做,再对答案。

  • 🟢 exercises can often be done in 10–15 minutes
    🟢 难度的练习通常 10–15 分钟就能完成。
  • 🟡 exercises require 20–40 minutes and may involve running tools locally
    🟡 难度的练习一般需要 20–40 分钟,并且可能要在本地真正跑工具。
  • 🔴 exercises require significant setup and experimentation (1+ hour)
    🔴 难度的练习往往需要较多前置环境和实验时间,可能超过 1 小时。

Prerequisites
前置知识

ConceptWhere to learn it
Cargo workspace layout
Cargo 工作区结构
Rust Book ch14.3
Feature flags
特性开关
Cargo Reference — Features
#[cfg(test)] and basic testing
#[cfg(test)] 与基础测试
Rust Patterns ch12
可参考 Rust Patterns 第 12 章。
unsafe blocks and FFI basics
unsafe 代码块与 FFI 基础
Rust Patterns ch10
可参考 Rust Patterns 第 10 章。

Chapter Dependency Map
章节依赖图

                 ┌──────────┐
                 │ ch00     │
                 │  Intro   │
                 └────┬─────┘
        ┌─────┬───┬──┴──┬──────┬──────┐
        ▼     ▼   ▼     ▼      ▼      ▼
      ch01  ch03 ch04  ch05   ch06   ch09
      Build Bench Cov  Miri   Deps   no_std
        │     │    │    │      │      │
        │     └────┴────┘      │      ▼
        │          │           │    ch10
        ▼          ▼           ▼   Windows
       ch02      ch07        ch07    │
       Cross    RelProf     RelProf  │
        │          │           │     │
        │          ▼           │     │
        │        ch08          │     │
        │      CompTime        │     │
        └──────────┴───────────┴─────┘
                   │
                   ▼
                 ch11
               CI/CD Pipeline
                   │
                   ▼
                ch12 ─── ch13
              Tricks    Quick Ref

Read in any order: ch01, ch03, ch04, ch05, ch06, ch09 are independent.
可以按任意顺序阅读的章节:ch01、ch03、ch04、ch05、ch06、ch09,这几章相对独立。 Read after prerequisites: ch02 (needs ch01), ch07–ch08 (benefit from ch03–ch06), ch10 (benefits from ch09).
建议有前置再读的章节:ch02 依赖 ch01;ch07–ch08 读过 ch03–ch06 会更顺;ch10 最好建立在 ch09 基础上。 Read last: ch11 (ties everything together), ch12 (tricks), ch13 (reference).
适合放到最后读的章节:ch11 负责把前面全部串起来,ch12 是经验技巧,ch13 是查阅手册。

Annotated Table of Contents
带说明的目录总览

Part I — Build & Ship
第一部分:构建与交付

#ChapterDifficultyDescription
1Build Scripts — build.rs in Depth
构建脚本:深入理解 build.rs
🟢Compile-time constants, compiling C code, protobuf generation, system library linking, anti-patterns
涵盖编译期常量、C 代码编译、protobuf 生成、系统库链接,以及常见反模式。
2Cross-Compilation — One Source, Many Targets
交叉编译:一套源码,多种目标
🟡Target triples, musl static binaries, ARM cross-compile, cross tool, cargo-zigbuild, GitHub Actions
涵盖 target triple、musl 静态二进制、ARM 交叉编译、crosscargo-zigbuild 与 GitHub Actions。

Part II — Measure & Verify
第二部分:度量与验证

#ChapterDifficultyDescription
3Benchmarking — Measuring What Matters
基准测试:衡量真正重要的东西
🟡Criterion.rs, Divan, perf flamegraphs, PGO, continuous benchmarking in CI
涵盖 Criterion.rs、Divan、perf 火焰图、PGO 与 CI 中的持续基准测试。
4Code Coverage — Seeing What Tests Miss
代码覆盖率:看见测试遗漏的部分
🟢cargo-llvm-cov, cargo-tarpaulin, grcov, Codecov/Coveralls CI integration
涵盖 cargo-llvm-covcargo-tarpaulingrcov,以及与 Codecov / Coveralls 的集成。
5Miri, Valgrind, and Sanitizers
Miri、Valgrind 与 Sanitizer
🔴MIR interpreter, Valgrind memcheck/Helgrind, ASan/MSan/TSan, cargo-fuzz, loom
涵盖 MIR 解释器、Valgrind 的 memcheck / Helgrind、ASan / MSan / TSan,以及 cargo-fuzz 与 loom。

Part III — Harden & Optimize
第三部分:加固与优化

#ChapterDifficultyDescription
6Dependency Management and Supply Chain Security
依赖管理与供应链安全
🟢cargo-audit, cargo-deny, cargo-vet, cargo-outdated, cargo-semver-checks
涵盖 cargo-auditcargo-denycargo-vetcargo-outdatedcargo-semver-checks
7Release Profiles and Binary Size
发布配置与二进制体积
🟡Release profile anatomy, LTO trade-offs, cargo-bloat, cargo-udeps
涵盖发布配置结构、LTO 取舍、cargo-bloatcargo-udeps
8Compile-Time and Developer Tools
编译期与开发者工具
🟡sccache, mold, cargo-nextest, cargo-expand, cargo-geiger, workspace lints, MSRV
涵盖 sccachemoldcargo-nextestcargo-expandcargo-geiger、工作区 lint 与 MSRV。
9no_std and Feature Verification
no_std 与特性验证
🔴cargo-hack, core/alloc/std layers, custom panic handlers, testing no_std code
涵盖 cargo-hackcore / alloc / std 分层、自定义 panic handler,以及 no_std 代码测试。
10Windows and Conditional Compilation
Windows 与条件编译
🟡#[cfg] patterns, windows-sys/windows crates, cargo-xwin, platform abstraction
涵盖 #[cfg] 模式、windows-sys / windows crate、cargo-xwin 与平台抽象。

Part IV — Integrate
第四部分:集成

#ChapterDifficultyDescription
11Putting It All Together — A Production CI/CD Pipeline
全部整合:生产级 CI/CD 流水线
🟡GitHub Actions workflow, cargo-make, pre-commit hooks, cargo-dist, capstone
涵盖 GitHub Actions 工作流、cargo-make、pre-commit hook、cargo-dist 与综合练习。
12Tricks from the Trenches
一线实战技巧
🟡10 battle-tested patterns: deny(warnings) trap, cache tuning, dep dedup, RUSTFLAGS, more
收录 10 个经实战验证的模式,包括 deny(warnings) 陷阱、缓存调优、依赖去重、RUSTFLAGS 等。
13Quick Reference Card
快速参考卡片
Commands at a glance, 60+ decision table entries, further reading links
整理常用命令、60 多条决策表项以及延伸阅读链接。