Skip to Content
Course content

241: cargo-watch for Development Workflow

Click on the "Edit" button in the top corner of the screen to edit your slide content.

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 function pub 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:
    1. Run cargo watch -x test in your terminal.
    2. Initially, leave the clean_input function empty (returning an empty string) and observe all tests failing in the watcher.
    3. Implement the logic incrementally (first to_lowercase(), then trim(), then handling internal whitespace) and watch the tests turn green one by one without manually restarting the command.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.