76

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 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.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
aij
  • 5,903
  • 3
  • 37
  • 41

7 Answers7

120

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-watch

And then run:

cargo watch -x run

And to watch changes in only the src folder and clear the console use:

cargo watch -c -w src -x run

See the cargo-watch README for more examples.

watchexec

Alternatively, you can use watchexec. Install it:

cargo install watchexec-cli

And then use it like this:

watchexec -r cargo run
Aalkhodiry
  • 2,178
  • 30
  • 25
robinst
  • 30,027
  • 10
  • 102
  • 108
  • 2
    Thank you for watchexec! cargo-watch only works for cargo. – stimulate Feb 15 '20 at 00:19
  • This only seem to work for a binary, when I tried with a library I got an error of `error: a bin target must be available for `cargo run``. – Chris Stryczynski Oct 17 '21 at 18:50
  • 1
    `watchexec` now has a separate CLI package so the actual install command is `cargo install watchexec-cli`. Also the proper invocation for the current version does not have quotes, so it's `watchexec --restart cargo run`. – Ilkka Jan 23 '22 at 09:56
4

There doesn't seem to be any support built in, but there is an extension (cargo-watch) to detect changes using inotify.

When I found it, it wouldn't work with stable (or current) Rust, but I've since patched it. It could still use some work, but it certainly speeds up the compile/fix-errors cycle.

malbarbo
  • 10,717
  • 1
  • 42
  • 57
aij
  • 5,903
  • 3
  • 37
  • 41
4

I use nodemon with this command:

nodemon --watch src -e rs --exec cargo check

All this does is watch all the rs files in the src folder and run cargo check.

2

I believe that the distinction is that running cargo run twice won't build the code twice, unless the input files have changed. As far as I know, Cargo doesn't have the functionality you want built-in. You could file a feature request. In the meantime, I'd suggest you just use watch. You could also use something like guard. Using watch is simpler, but would just run your code every N seconds. guard would require more setup, but would be a bit more efficient.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
2

Another option is using entr

ls -r | entr cargo run

This will recursively list all files in the current directory. Then redirect them to the entr command which watches for changes to those files and then finally runs cargo run

jasonleonhard
  • 12,047
  • 89
  • 66
0

The most stable option for me was to use npx with nodemon

npx nodemon --watch src -e rs --exec cargo run

You'll need nodejs and npm installed.

Oliver Dixon
  • 7,012
  • 5
  • 61
  • 95
-6

In you Visual Studio Code : Go to File and check Auto Save which will automatically save the changes made.