Boost Your Rust 🦀 Development with Hot Reload using cargo-watch
2 min read
Most of the developers with a
node.js background are pretty familiar with the package called
nodemon is a tool is automatically restarting the node application whenever we change a file in a directory. This is tremendously useful during development, as we don't have to restart the application manually every time we make a change. And as we all know, we developers are lazy!
Or you may be familiar with the tool called
entr, which is a command-line tool that runs arbitrary commands when files change.
In this blog article, I will show you a similar command-line tool with the same productivity boost but for Rust. This tool is called
cargo-watch watches your Rust project for any changes and runs cargo commands when a change is detected. The installation is very simple, just run the following command:
cargo install cargo-watch
The simplest usage of
cargo-watch is to run the
cargo run command whenever a file in the current directory changes:
cargo watch -x run
If you want to run multiple commands, you can use the
-x flag multiple times. In the following example, we will run the
cargo test and
cargo run commands whenever a file in the current directory changes:
cargo watch -x test -x run
If you want to suppress the output of the
cargo run command, you add the
-q flag to the
cargo run command:
cargo watch -x test -x 'run -q'
-w flag, you can specify a directory to watch. In the following example, we will watch the
src directory for changes:
cargo watch -w src/ -x test -x 'run -q'
Ignoring files and directories
cargo-watch ignores per default all files and directories listed in the
.gitignore file. With the flag
--no-vcs-ignores you can disable this behavior. Secondly,
cargo-watch ignores all files and directories that are listed in the
.ignore file. With the flag
--no-dot-ignores you can also disable this behavior.
Additionally, you can ignore files and directories by using the
-i flag. This uses the Glob pattern matching syntax.
cargo-watch we have a similar tool by hand, which we are already familiar from other programming languages.
I use this tool every day and I can highly recommend it! The boost in your development productivity will be huge!