← Table of contents

0.3 The Familiar

Every wizard has a familiar, and every apprentice spends weeks discovering what the familiar can do beyond the obvious. Cargo has already answered once - and that was enough for something to appear. But it has commands that have not been asked about yet. The silence does not mean they are not there.

What You Need

cargo run launched tq - enough to get started. But one command is not enough for real work on any real project. Code needs to be checked without running it. Formatted, without thinking about spaces. Analysed beyond what syntax alone can catch.

Cargo has a command for each of these. It is worth knowing them all.

What Can It Do?

cargo check

Checks the code without compiling it. Cargo parses the source, applies the compiler’s rules, and reports errors - but produces no executable. It is faster than cargo build, which makes it useful when you want to confirm the code is correct without waiting for a full build.

cargo check
    Checking tq v0.1.0 (.../tq)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.18s

cargo build

Compiles the project and produces an executable at target/debug/tq. Unlike cargo run, it does not launch the result - only builds it.

cargo build
   Compiling tq v0.1.0 (.../tq)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.42s

cargo run

Already familiar: compiles and immediately runs. Everything cargo build does, plus the launch. In day-to-day work it is used more than any other.

cargo fmt

Formats the source code to the Rust standard. No need to think about indentation, spaces around operators, or line length - cargo fmt handles all of it.

cargo fmt

The command produces no output if there is nothing to format. Otherwise it silently rewrites the files. Run it before every review: a reader notices formatting before they notice meaning.

cargo clippy

Runs the linter - a tool that looks not for errors but for bad habits: non-idiomatic constructs, suspicious patterns, things that work but could work better.

cargo clippy
    Checking tq v0.1.0 (.../tq)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.18s

With a single line in tq, Clippy has nothing to say. That will change.

The Result

Cargo is no longer a black box. It has five commands, each with a purpose: check, build, run, fmt, clippy. They can now be called by name.

The tq code did not change in this chapter - we were only learning the familiar’s skills. For consistency it is still at 0-the-workshop/03-the-familiar/.


Lore: The target/ Directory

After the first build, a target/ directory appears in the project folder. Cargo puts everything it creates there: compiled files, cache, intermediate artifacts. On a large project it can reach several gigabytes, and it is rebuilt whenever the code changes. It does not belong in version control.

cargo new creates a .gitignore alongside the project. The right line is already there:

/target

Nothing else is needed. If the file is missing for some reason, restore it manually.


Lore: Configuring the Formatter

cargo fmt follows the standard rules - and for most projects that is enough. If you need to change something, create a rustfmt.toml file in the project root. There you can set, for example, a maximum line length:

max_width = 120

The full list of options is available via rustfmt --help=config. Most teams never open this file - the standard is good enough that arguing with it costs more than it saves.