cargo check for faster checking
cargo check
is a new subcommand that should speed up the developmentworkflow in many cases.
What does it do? Let's take a step back and talk about how rustc
compilesyour code. Compilation has many "passes", that is, there are many distinctsteps that the compiler takes on the road from your source code to producingthe final binary. However, you can think of this process in two big steps:first, rustc
does all of its safety checks, makes sure your syntax iscorrect, all that stuff. Second, once it's satisfied that everything is inorder, it produces the actual binary code that you end up executing.
It turns out that that second step takes a lot of time. And most of the time,it's not neccesary. That is, when you're working on some Rust code, manydevelopers will get into a workflow like this:
- Write some code.
- Run
cargo build
to make sure it compiles. - Repeat 1-2 as needed.
- Run
cargo test
to make sure your tests pass. - Try the binary yourself
- GOTO 1.In step two, you never actually run your code. You're looking for feedbackfrom the compiler, not to actually run the binary.
cargo check
supportsexactly this use-case: it runs all of the compiler's checks, but doesn'tproduce the final binary. To use it:
$ cargo check
where you may normally cargo build
. The workflow now looks like:
- Write some code.
- Run
cargo check
to make sure it compiles. - Repeat 1-2 as needed.
- Run
cargo test
to make sure your tests pass. - Run
cargo build
to build a binary and try it yourself - GOTO 1.So how much speedup do you actually get? Like most performance relatedquestions, the answer is "it depends." Here are some very un-scientificbenchmarks at the time of writing.
use case | build performance | check performance | speedup |
---|---|---|---|
initial compile | 11s | 5.6s | 1.96x |
second compile (no changes) | 3s | 1.9s | 1.57x |
third compile with small change | 5.8s | 3s | 1.93x |