snippetrustCritical
How can I get cargo to recompile changed files automatically?
Viewed 0 times
recompilehowfilescancargochangedautomaticallyget
Problem
I have heard cargo has the ability to automatically recompile changed source files, but I'm having a hard time figuring out how to tell it to do so.
For now, I am manually running
In case you still have no idea what I'm talking about, I'm looking for the cargo equivalent of
It seems strangely hard to find, so I'm starting to wonder if it's really supported. It's possible someone had said cargo could detect changed files and recompile them when what he meant to say was that cargo could detect unchanged files and avoid recompiling them, like
For now, I am manually running
cargo build or cargo run every time I want to type check my code. I would prefer to instead simply save the file and see the results in the neighboring terminal window.In case you still have no idea what I'm talking about, I'm looking for the cargo equivalent of
sbt ~compile or sbt ~run.It seems strangely hard to find, so I'm starting to wonder if it's really supported. It's possible someone had said cargo could detect changed files and recompile them when what he meant to say was that cargo could detect unchanged files and avoid recompiling them, like
make.Solution
bacon
Nowadays the recommended tool is bacon. Install:
Then run:
Which will keep running
Historical answers
cargo watch
In case you are working on a server project (e.g. hyper, iron, etc) that keeps running and you need it to be restarted when files change, you can use
And then run:
And to watch changes in only the
See the cargo-watch README for more examples.
watchexec
Alternatively, you can use watchexec. Install it:
And then use it like this:
Nowadays the recommended tool is bacon. Install:
cargo install --locked baconThen run:
baconWhich will keep running
cargo check. See docs for more examples.Historical answers
cargo watch
In case you are working on a server project (e.g. hyper, iron, etc) that keeps running and you need it to be restarted when files change, you can use
cargo watch. Install:cargo install cargo-watchAnd then run:
cargo watch -x runAnd to watch changes in only the
src folder and clear the console use:cargo watch -c -w src -x runSee the cargo-watch README for more examples.
watchexec
Alternatively, you can use watchexec. Install it:
cargo install watchexec-cliAnd then use it like this:
watchexec -r cargo runCode Snippets
cargo install --locked baconcargo install cargo-watchcargo watch -x runcargo watch -c -w src -x runcargo install watchexec-cliContext
Stack Overflow Q#29461693, score: 171
Revisions (0)
No revisions yet.