Helo everyone 👋🏻
When we are developing a Rust application, sometimes we have the need to reduce the time of the cycle of changes, compilation, and execution. It sounds a bit complicated, but today I am going to show you a tool that does it in a very simple and automatic way. This tool is called cargo watch
, and it reduces the time of each cycle of changes in the background.
Cargo Watch
Cargo Watch creates a listener on your project changes and runs Cargo commands when they occur. At the time of writing this article the latest version is 8.1.2
.
Cargo Watch doc
Github Project
Disclaimer
For this tutorial I am assuming you are familiar with using Cargo.
Cargo is Rust’s build system and package manager. Most Rustaceans use this tool to manage their Rust projects because Cargo handles a lot of tasks for you, such as building your code, downloading the libraries your code depends on, and building those libraries
You can read more about cargo here
Change Cycle
We start by creating a new project. For our example we will call it cargo-watch-example
cargo new cargo-watch-example
If the project was created correctly you should see the following message in the terminal
Created binary (application)
cargo-watch-examplepackage
Install Cargo Watch
cargo install cargo-watch
Run project and observe changes
Inside the root directory:
cargo watch -x run
If you want to only listen for changes from only the working directory add the -w option to specify a file or directory from which you want to listen for changes.
cargo watch -w src -x run
Demo
The previous example was the simplest configuration observing changes on the whole project or on a specific directory, but you can do much more, Here's a copy of the help:
USAGE:
cargo watch [FLAGS] [OPTIONS]
FLAGS:
-c, --clear Clear the screen before each run
-h, --help Display this message
--ignore-nothing Ignore nothing, not even target/ and .git/
--debug Show debug output
--why Show paths that changed
-q, --quiet Suppress output from cargo-watch itself
--no-gitignore Don’t use .gitignore files
--no-ignore Don’t use .ignore files
--no-restart Don’t restart command while it’s still running
--poll Force use of polling for file changes
--postpone Postpone first run until a file changes
-V, --version Display version information
--watch-when-idle Ignore events emitted while the commands run.
Will become default behaviour in 8.0.
OPTIONS:
-x, --exec <cmd>...
Cargo command(s) to execute on changes [default: check]
-s, --shell <cmd>... Shell command(s) to execute on changes
-d, --delay <delay>
File updates debounce delay in seconds [default: 0.5]
--features <features>
List of features passed to cargo invocations
-i, --ignore <pattern>... Ignore a glob/gitignore-style pattern
-B <rust-backtrace>
Inject RUST_BACKTRACE=VALUE (generally you want to set it to 1)
into the environment
--use-shell <use-shell>
Use a different shell. E.g. --use-shell=bash. On Windows, try
--use-shell=powershell, which will become the default in 8.0.
-w, --watch <watch>...
Watch specific file(s) or folder(s) [default: .]
-C, --workdir <workdir>
Change working directory before running command [default: crate root]
ARGS:
<cmd:trail>... Full command to run. -x and -s will be ignored!
Cargo commands (-x) are always executed before shell commands (-s). You can use
the `-- command` style instead, note you'll need to use full commands, it won't
prefix `cargo` for you.
By default, your entire project is watched, except for the target/ and .git/
folders, and your .ignore and .gitignore files are used to filter paths.
On Windows, patterns given to -i have forward slashes (/) automatically
converted to backward ones (\) to ease command portability.
In the next tutorial we will apply this example using Docker Containers
😎
If you like my content and want to support my work, you can give me a cup of coffee ☕️ 🥰