Watching how you REPL in Rust

2 minute read Published: 2021-03-13

Rust (the programming language, not the game1) has excellent tooling to assist the developer in everyday tasks. Namely, I am really fond of cargo watch, a tool that "watches" the inodes of the files in a Rust project and will trigger a command when they change.

The tool can trigger any cargo command, here's some of mostly used by any Rust developer:

The basic usage of cargo watch is:

cargo watch -x [command]

commands can be chained:

cargo watch -x [command] -x [second command] -x [third command]

Can also run anything from the shell:

cargo watch -x [command] -s "sudo rm -rf /"     # don't do that

Its superpowers are the flexibility and scriptability, making it suitable for writing a real REPL.

With this knowledge, I wrote a simple bash script wrapping some subcommands of cargo. The script is so small that can be fit into a bash function in your dotfiles.

Let's analyze one line of the script and break down the command:

cargo-watch
    # clear the screen after each run
    -c

    # only watch the src/ directory (and f.e. ignore "tests/")
    -w src/

    # execute "cargo check"
    -x check

    # Show a notification at the end of the command
    -s '/usr/bin/notify-send "Compiling - xkcd.com/303" DONE'

Here's how working with this script looks like:

The script has quite some code repeated but quoting in bash is not my forte (patches are welcome!).


1

Only those who lurk /r/rust will get the joke

2

Everytime you run cargo build, a tree in the Amazon forest is incinerated and its dusts are snorted by a blockchain entrepreneur.

3

Whose name is really unfortunate, I'd like to go back in time and stop the author