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
241: cargo-watch for Development Workflow
Imagine you're working with a sous-chef in a professional kitchen. You're the head chef, and your job is to refine a complex sauce. Instead of you having to stop what you're doing, walk across the kitchen, and manually tell the sous-chef, "Okay, I just added a pinch of salt, now taste it and tell me if it's right," the sous-chef is just watching you. The moment you set the salt shaker down, they've already dipped a spoon in and are giving you the verdict. You never stop your flow; you just keep adjusting, and the feedback loop is instantaneous.
In Rust, cargo-watch is that sous-chef. Normally, your development loop looks like this: write some code, hit Ctrl+S, switch to the terminal, type cargo run or cargo test, wait for the compiler, and see if it worked. It's a lot of context switching. cargo-watch eliminates that manual trigger. It monitors your source files and automatically triggers a command the second it detects a change.
Breaking the Manual Restart Cycle
I've spent far too many hours of my life typing cargo run over and over again. It feels like a small thing, but those three seconds of typing and switching windows eventually wear you down. Once you install cargo-watch, you stop thinking about "running" the program and start thinking about "evolving" the program.
First, you'll need to pull the tool into your environment:
cargo install cargo-watch
Now, let's say you're building a small CLI tool that parses a local JSON file to calculate total expenses. You're tweaking the logic in src/main.rs to handle different currency symbols. Instead of running the program manually every time you change a regex or a match arm, you run this in your terminal:
cargo watch -x run
The -x flag stands for "execute." You're telling the tool: "Watch my directory, and whenever something changes, execute the cargo run command." If you make a typo and the code doesn't compile, cargo-watch will show you the error. The moment you fix that typo and save, it triggers the build again. It's seamless.
Tuning the Loop for Testing
While run is great, I actually find cargo-watch most powerful when paired with tests. If you're practicing Test-Driven Development (TDD), you're writing a test, watching it fail, and then writing just enough code to make it pass. Doing that manually is a chore. Doing it with a watcher is a superpower.
Try running this instead:
cargo watch -x test
Now, your terminal becomes a real-time dashboard of your project's health. You change a function's implementation, and a split second later, you see a green test result: ok or a red failure. You don't even have to leave your editor. I usually keep my terminal split to the side of my screen; I can literally watch the tests flip from red to green as I type.
Handling Multiple Commands
Sometimes, just running the code isn't enough. Maybe you want to run your tests and then run the app if the tests pass. You can stack the -x flags. For example:
cargo watch -x test -x run
In this scenario, cargo-watch will run the tests first. If the tests fail, it stops right there (which is exactly what you want—no point running a broken app). If the tests pass, it proceeds to execute the program. It's a tight, automated quality gate that ensures you never accidentally run a version of your app that you know is broken.
📋 Practical Task
Implementing an Auto-Validated String Parser
Your task is to build a small utility that cleans up "messy" user input strings (removing extra whitespace and converting to lowercase) and use cargo-watch to iterate on the logic.
- The Setup: Create a new Rust project. In
src/lib.rs, create a functionpub fn clean_input(input: &str) -> String. - The Tests: In the same file, write a test module with three cases:
- An empty string should return an empty string.
- A string with leading/trailing spaces (e.g.,
" Hello ") should return"hello". - A string with mixed case and internal tabs (e.g.,
"Rust\tLanguage") should return"rust language".
- The Workflow:
- Run
cargo watch -x testin your terminal. - Initially, leave the
clean_inputfunction empty (returning an empty string) and observe all tests failing in the watcher. - Implement the logic incrementally (first
to_lowercase(), thentrim(), then handling internal whitespace) and watch the tests turn green one by one without manually restarting the command.
- Run
There are no comments for now.