Linux command
cargo-watch 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Watch and rebuild
cargo watch
Watch and run tests
cargo watch -x test
Watch and run a specific example
cargo watch -x "run --example [example_name]"
Watch and run multiple commands
cargo watch -x check -x test -x run
Watch with clear screen
cargo watch -c
Watch specific files or directories
cargo watch -w [src/] -w [tests/]
Ignore specific patterns
cargo watch -i "*.txt" -i "target/"
Watch with shell command
cargo watch -s "echo 'Changed!' && cargo build"
说明
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.
参数
- -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.
FAQ
What is the cargo-watch command used for?
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.
How do I run a basic cargo-watch example?
Run `cargo watch` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -x _command_ do in cargo-watch?
Cargo command to run (default: check).