LinuxCommandLibrary

cargo-watch

TLDR

Watch and rebuild on changes

$ cargo watch
copy
Watch and run tests
$ cargo watch -x test
copy
Watch and run a specific example
$ cargo watch -x "run --example [example_name]"
copy
Watch and run multiple commands
$ cargo watch -x check -x test -x run
copy
Watch with clear screen before each run
$ cargo watch -c
copy
Watch specific files or directories
$ cargo watch -w [src/] -w [tests/]
copy
Ignore specific patterns
$ cargo watch -i "*.txt" -i "target/"
copy
Watch with shell command
$ cargo watch -s "echo 'Changed!' && cargo build"
copy

SYNOPSIS

cargo watch [options] [-x command]...

DESCRIPTION

cargo-watch is a Cargo subcommand that watches project source files and runs Cargo commands when files change. It provides a convenient development workflow for continuous compilation, testing, or running.
By default, cargo watch runs `cargo check` on changes. Multiple commands can be chained with `-x` flags and will run in sequence. The tool debounces rapid file changes to avoid excessive rebuilds.
It watches all files that Cargo considers part of the project, including src/, tests/, benches/, examples/, and Cargo.toml. Custom watch paths and ignore patterns can be specified.

PARAMETERS

-x command

Cargo command to run (default: check).
-s command
Shell command to run.
-c, --clear
Clear screen before each run.
-w path
Watch specific path (can repeat).
-i pattern
Ignore files matching pattern.
-d delay
Debounce delay in seconds.
--poll
Use polling instead of events.
--postpone
Postpone first run until change.
-q, --quiet
Suppress output from watch itself.
--no-gitignore
Don't use .gitignore patterns.
--why
Show which file triggered the run.
-B cmd
Run command before watched command.
-N
Send desktop notification on finish.

CAVEATS

Requires installation via `cargo install cargo-watch`. File system events may not work in all environments (use --poll as fallback). Rapid consecutive saves may be debounced into single runs. Large projects may benefit from increased debounce delay.

HISTORY

cargo-watch was created by F\u00e9lix Saparelli (passcod) in 2015 to provide file watching functionality for Rust development. It built upon the notify crate for cross-platform file system events. The tool has become a standard part of many Rust developers' workflows, inspired by similar tools in other ecosystems like nodemon for Node.js.

SEE ALSO

cargo(1), cargo-check(1), watchexec(1), entr(1)

Copied to clipboard