Cargo rerun to restart a `cargo run` automatically?

I'm wondering if there is an extension to cargo which I imagine as cargo rerun, which will do cargo run, but will then rebuild and restart the job if the code changes? It would be wonderful when developing my web app, and I can't have been the first to think of this...

To answer my own question, a bit more searching revealed cargo-watch, which enables doing what I want (with two commands running simultaneously in two terminals). If there is a simpler tool that is more targeted, I'd still love to hear about it!

The “watch” program for Unix-like systems can run any command on file changes.

Dedicated programs for this are more important for non-IDE coding and interpreted languages. Usually Rust users would prefer “cargo check” but I believe both official Rust extensions for VS Code allow this to be changed to any command.

Could you elaborate what you mean with two commands in two terminals? I would've imagined that something like cargo watch -x run would do the job.

cargo watch -x build -s 'touch .trigger'


cargo watch --no-gitignore -w .trigger -x run

so that I don't have the code stop running for two minutes to recompile.

Also to avoid stopping the program when the code is currently not compiling, e.g. if I edit two files and save changes in the wrong order.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.