Rust
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Ownership and Borrowing
-
Section 4: Structuring Data
-
Section 5: Collections and Error Handling
-
Section 6: Traits and Generics
-
Section 7: Concurrency
-
Section 8: Building for the Web
-
Section 9: Memory and Performance
-
Section 10: More Standard Library and Ecosystem
-
Section 11: Advanced Rust
-
Section 12: Rust for Systems and WebAssembly
-
Section 13: Tooling and Best Practices
-
Section 14: Data Structures and Algorithms in Rust
-
Section 15: Practical Projects
-
Section 16: Interview Practice
-
Section 17: std::collections In Depth
-
Section 18: std::io and std::fs In Depth
-
Section 19: std::net
-
Section 20: std::option and std::result In Depth
-
Section 21: std::iter In Depth
-
Section 22: std::sync In Depth
-
Section 23: std::string and std::str
-
Section 24: Cargo and Crates.io Ecosystem
-
Section 25: Popular Crates Ecosystem
-
Section 26: Rust Memory Model Deep Dive
-
Section 27: More Practice Exercises
-
Section 28: More Interview Practice
-
Section 29: Async Rust Deep Dive
-
Section 30: Tokio Ecosystem In Depth
-
Section 31: Error Handling Ecosystem Deep Dive
-
Section 32: Serde In Depth
-
Section 33: Web Development with Rust Deep Dive
-
Section 34: Database Access Ecosystem
-
Section 35: Rust for Embedded Systems Deep Dive
-
Section 36: Rust Macros In Depth
-
Section 37: Advanced Trait System
-
Section 38: Unsafe Rust In Depth
-
Section 39: Rust CLI Development
-
Section 40: Testing Ecosystem Deep Dive
-
Section 41: WebAssembly Deep Dive
-
Section 42: Rust Design Patterns
-
Section 43: More Data Structures in Rust
-
Section 44: Final Practice Projects
-
Section 45: Rust Performance Optimization
-
Section 46: Rust Ecosystem Tooling
-
Section 47: More Interview and Review
124: Cargo.toml Configuration In Depth
How do I actually squeeze the most performance out of my release build?
By default, cargo build --release does a great job, but if you're building something performance-critical—like a game engine or a high-frequency trading tool—the defaults aren't aggressive enough. You need to touch the [profile.release] section.
I usually recommend starting with Link Time Optimization (LTO). Normally, Rust optimizes each crate individually. LTO tells the compiler to look across the entire dependency graph to find optimization opportunities. It makes your compile times crawl, but the binary is leaner and faster.
[profile.release]
lto = true
codegen-units = 1
panic = 'abort'
opt-level = 3
Notice codegen-units = 1. By default, Cargo splits your crate into multiple units to parallelize the build. Setting this to 1 forces the compiler to process everything as a single unit, which allows for better global optimizations. I've seen this shave a few percentage points off execution time in tight loops. Also, panic = 'abort' removes the stack unwinding machinery, which reduces binary size and can slightly improve speed if you don't need to catch panics.
I want to add a feature to my library, but only some people will want it. How do I set that up?
You don't want to force every user of your crate to download 20 dependencies if they only need the core logic. This is where [features] come in. Think of features as "conditional compilation" flags that the user chooses at the dependency level.
Let's say you're building a data-processor crate. You want a json feature that enables Serde support, but it should be optional. First, mark the dependency as optional in your [dependencies] section, then map it in your [features] section:
[dependencies]
serde = { version = "1.0", optional = true }
serde_json = { version = "1.0", optional = true }
[features]
default = []
json = ["serde", "serde_json"]
Now, in your code, you can use #[cfg(feature = "json")] to wrap the JSON-specific logic. If a user adds your crate as data-processor = { version = "0.1", features = ["json"] }, those dependencies are pulled in. If they don't, they never even hit the network. It's a huge win for compile times and binary bloat.
What if a crate I'm using is broken and I need to use my own fork for a while?
This is a situation I've been in more times than I'd like to admit. You find a bug in a deep dependency. You submit a PR, but the maintainer is on vacation. You can't wait two weeks to ship your project. You need a way to "override" that specific dependency globally across your entire project, even for other crates that depend on it.
The [patch] section is your best friend here. It allows you to replace a version of a crate with a different source (like a git repo) without having to manually edit every single Cargo.toml in a workspace.
[patch.crates-io]
some-broken-crate = { git = "https://github.com/yourname/some-broken-crate", branch = "fix-critical-bug" }
By putting this in your root Cargo.toml, Cargo will substitute the official some-broken-crate from crates.io with your fork wherever it appears in the dependency tree. Once the official PR is merged, you just delete these lines and bump the version number. It's a clean, temporary surgical strike.
📋 Practical Task
Configuring a High-Performance Data Parser
You are working on a crate called fast-parse. Your goal is to optimize the release binary for maximum speed and implement a feature-gated "advanced" mode that requires the regex crate.
Modify the provided Cargo.toml to achieve the following:
- Enable full Link Time Optimization (LTO) in the release profile.
- Set
codegen-unitsto 1 for release builds to maximize optimization. - Set the release panic strategy to
abort. - Make the
regexdependency optional. - Create a feature named
advancedthat enables theregexdependency.
[package]
name = "fast-parse"
version = "0.1.0"
edition = "2021"
[dependencies]
regex = "1.10"
# Add your profile and feature configurations below
There are no comments for now.