Live-Reload for Rust Services (cargo-watch)
cargo-watch is the Rust-ecosystem equivalent of Air for Go or nodemon for Node — it watches your source tree and automatically recompiles and reruns your service on save.
1. Install cargo-watch
cargo install cargo-watch
This compiles the tool from source, so the first install takes a minute or two.
2. Run With Hot Reloading
From the root of the Rust service (where its Cargo.toml lives):
cargo watch -x run
cargo watch monitors the project; -x run tells it to execute cargo run every time a .rs file changes — killing the running process, recompiling, and starting it back up.
3. Watch Non-Rust Files
By default cargo-watch only reacts to .rs file changes. To also restart on a config or .env change, add explicit -w (watch) paths:
cargo watch -w src -w Cargo.toml -w .env -x run
4. Check and Test Before Running
Chain commands to catch a compile error or a broken test before the service restarts:
cargo watch -x check -x test -x run
Each command runs in order; run only executes if check and test both succeed.
5. Avoid Restart Loops
If the service writes logs, a local database file, or other dynamic data inside the project directory, that write can itself trigger another restart. Exclude those paths with -i (ignore):
cargo watch -i "*.log" -i "storage/*" -x run