Adding custom runnable tasks to a Rust project

Recently, I learned about the cargo xtask pattern for Rust projects. Basically, it is adding a binary Rust CLI application (cargo new xtask) to a Rust workspace with a project specific cargo alias config. Then, cargo xtask will run the CLI application.

cargo handles many built-in tasks such as compiling code and running tests. There is even a convention to add cargo subcommands, so there are projects like cargo watch, cargo deny, and cargo audit.

However, there are often other tasks which are very specific to the project and could be automated. For instance, a custom code generating process can be run. Or maybe, the project has a specific way to publish a release.

The tasks are written in Rust in the CLI application. Someone does not need to know write portable shell scripts in order to add a task. They only need to know Rust.

Having used the pattern in a couple projects, it is super convenient, mostly portable, and easy to customize.