Automate Tasks by Watching

While I like several Rust testing frameworks, the one testing tool that is indispensable is cargo-watch.

It is similar to other "watch" programs which:

  1. Detects changes under a directory (e.g. a file was added/updated)
  2. If a change occurs, run a command (or commands).

cargo watch can be used like any other CLI tool, so it works regardless if the directory watched is a Rust project or not.

Try It

cargo install cargo-watch --locked

Then start a new shell, go to a project directory and run:

cargo watch -x test

Make some changes in the project and watch the tests automatically run.

That's it!

The test in -x test is a shortcut for cargo test, but cargo watch can run arbitrary commands.

There are several options to customize things like running multiple commands. There are even some workflows described in the project's README which trigger chains of commands depending on the success/failure of previous ones.

Why

It may be obvious, but if you constantly run cargo test or some other action after every change, it is better to automate the process. After, all, the machine works for you.

Improving the developer feedback loop is essential and with the compiler taking a few seconds/minutes to compile the code, it is better to remove any delays in getting the feedback.

Even if cargo watch is not the specific tool used, having any tool watch a directory for changes and automating the build process is highly recommended.