Deep Dive: egui/eframe for instant UIs — why Immediate Mode fits system languages, State management with Rust's ownership, Layout & Style, Custom painting, and when Retained Mode is the better choice.
Deep dive: atomic operations, memory orderings, and CAS loops — tools without locks but with sharper contracts. When atomics shine and when the mutex stays.
Cargo has two default profiles:
cargo build uses dev, cargo build --release uses release. In your
Cargo.toml you can adjust profiles:
[profile.dev]
opt-level = 0 # no optimization, fast build
// → fast builds for development
[profile.release]
opt-level = 3 # maximum optimization
// → enables maximum optimizations for release builds
opt-level ranges from 0 (fast build, slow run) to 3 (slow build, fast run).
dev vs. release — a deliberate trade-off
The profiles trade compile time against runtime performance. During
development you want fast builds (dev, opt-level = 0). When shipping you want
fast programs (release, opt-level = 3). The compiler optimizer produces very
different speeds from the same code.
Check: What's the difference between the dev and release profiles?
Answer: dev compiles fast with little optimization, release slowly with much
dev (cargo build) is for fast development cycles, release (cargo build --release) for maximally optimized programs.
Takeaways:
⚠ Common mistake: Using
cargo buildfor performance testing instead ofcargo build --release— the huge speed difference (factor 10+) fromopt-level=0vsopt-level=3leads to wrong conclusions about actual runtime behavior.
You can set each profile individually:
[profile.release]
opt-level = 3
debug = false # no debug info
lto = true # link-time optimization
codegen-units = 1 # more optimization, slower build
lto = true — optimizes across module boundaries (slower link)codegen-units = 1 — one codegen unit → better optimizationpanic = "abort" — smaller binary, but no unwindingDefaults are good for most cases. opt-level = 3, lto = true, and
codegen-units = 1 often bring noticeable gains for libraries with many small
functions — at the cost of longer builds.
Measure optimization, don't guess
Before cranking up profiles, measure: cargo build --release and benchmark
your real workload. Many optimization flags have trade-offs (bigger binary,
slower builds). Only optimize what's provably a bottleneck — the ponytail:
rule applies here too.
Check: What does lto = true do in a profile?
Answer: Link-time optimization across module boundaries
lto = true lets the compiler optimize across module boundaries — at the cost of a longer link time.
Takeaways:
⚠ Common mistake: Blindly combining
lto = trueandcodegen-units = 1leads to extremely long build times — these flags only make sense when benchmarks actually show the code benefits from them.
A workspace is a group of crates sharing a Cargo.lock and target/
directory. Ideal for monorepos or libraries with several parts:
workspace/
├── Cargo.toml # workspace root
├── adder/
│ ├── Cargo.toml
│ └── src/lib.rs
└── add_one/
├── Cargo.toml
└── src/lib.rs
In the root Cargo.toml:
[workspace]
members = ["adder", "add_one"]
Cargo.lock → consistent dependenciescargo build builds all members# adder/Cargo.toml
[dependencies]
add_one = { path = "../add_one" }
Workspace = shared infrastructure
All members share target/ (build artifacts) and Cargo.lock (versions). That
saves space and keeps dependencies consistent — every crate in the workspace
uses exactly the same versions.
Check: What do crates in a workspace share?
Answer: Cargo.lock and the target/ directory
Workspace members share Cargo.lock (consistent versions) and target/ (build artifacts). That keeps dependencies uniform.
Takeaways:
⚠ Common mistake: Building workspace crates individually with
cargo buildinstead of at the root — this misses recent changes in dependencies or links against stale artifacts.
With /// comments you document items. cargo doc builds HTML documentation
from them:
/// Adds two numbers.
///
/// # Examples
///
/// ```
/// let result = add(2, 3);
/// assert_eq!(result, 5);
/// // → 5
/// ```
pub fn add(a: i32, b: i32) -> i32 {
// → 5
a + b
}
The code blocks in the /// comment run as doc tests during cargo test —
documentation that checks itself.
//! comments (at the file top) document the whole crate or module, not a
single item.
Doc tests keep examples current
Code examples in /// comments run during cargo test. If your code behaves
differently than documented, the doc test fails. So Rust prevents your docs
from going stale — a powerful tool.
Check: What happens to code blocks in /// comments during cargo test?
Answer: They run as doc tests
Code examples in /// comments run as doc tests during cargo test — so the documentation is provably correct.
Takeaways:
⚠ Common mistake: Writing doc tests that depend on implementation-specific behavior — these tests break with every internal change even when the public API remains correct.
To make your library available to others, publish it on crates.io. For that
your Cargo.toml needs metadata:
[package]
name = "my_crate"
version = "0.1.0"
edition = "2021"
description = "A short description"
license = "MIT OR Apache-2.0"
Key fields:
description — required for crates.iolicense — the license of your codeversion — follows SemVer (Major.Minor.Patch)Then cargo publish (after cargo login with your crates.io token).
publish is irrevocable
Once published, a version stays forever on crates.io (you can only publish
new versions, never remove any). So check metadata and API before the first
cargo publish.
Check: Which Cargo.toml field is required for crates.io?
Answer: description
crates.io requires at least a description (and recommends license/repository). version and name are also required, of course.
Takeaways:
⚠ Common mistake: Publishing a crate with
version = "0.1.0"even though the API is already stable and publicly used — a later0.2.0is considered breaking even though it is only a minor version bump.
With cargo install you install binary crates globally (e.g. ripgrep, bat):
cargo install ripgrep
That downloads the crate from crates.io, builds it, and puts the binary in
~/.cargo/bin/.
| Command | Purpose |
|---|---|
cargo build | compile the project (dev) |
cargo build --release | compile optimized |
cargo run | build + run |
cargo test | run tests |
cargo check | quickly check for errors |
cargo doc | build documentation |
cargo fmt | format code |
cargo clippy | lint warnings (catches pitfalls) |
cargo publish | publish to crates.io |
cargo check & clippy are your friends
cargo check checks only for errors without a full build — much faster in
the editor loop. cargo clippy catches idiomatic errors and pitfalls the
compiler doesn't report. Run both regularly.
Check: What does cargo check do, unlike cargo build?
Answer: It checks for errors without fully compiling
cargo check checks the code for errors but produces no executable — thus much faster than a full build.
Takeaways:
⚠ Common mistake: Using
cargo installwithout--lockedor--version— this installs the latest version which may be incompatible with your own code.
You build a workspace separating library and binary:
members = ["lib", "bin"].lib with a function double(n: i32) -> i32, and
document it with a /// comment + doc-test example.bin that uses lib as a path dependency and calls
double in main.cargo test in the root — confirm the doc test runs too.cargo build --release and verify both crates compile.This connects workspaces, path dependencies, doc tests, and release profiles.
Recipes from this module
[profile.release] opt-level = 3[workspace] members = [...]/// (item) and //! (crate)crate = { path = "../crate" }cargo publishScenario: The slow export
Takeaways:
⚠ Common mistake: Testing the library crate in the workspace with
cargo testin its own directory — this misses the doc test that verifies integration with the binary crate and doesn't build against the current version of the binary.
Course navigation: ← Module 15 · All 27 modules · Module 17 →