Keep a Rust Project Clean

RustBeginner
Practice Now

Introduction

Working code is only one part of a project that another person can safely maintain. A handoff-ready Rust crate should also use consistent formatting, avoid suspicious patterns, explain its public interface, and keep its tests passing.

In this lab, you will repair a small library with Rust's standard project tools. You will introduce rustfmt, Clippy, and rustdoc one at a time, then combine them with the test suite into a repeatable quality loop. The code is intentionally small so you can focus on what each tool proves.

Format the Source Consistently

In this step, you will use rustfmt to detect and repair layout differences without changing program behavior.

Enter the prepared library and open its source:

cd /home/labex/project/handoff-helpers
nano src/lib.rs

The first function is valid Rust, but its spacing and indentation differ from the rest of the file. Formatting rules are mechanical, so a tool can apply them more reliably than each contributor doing it by hand. Press Ctrl+X without editing.

First use check mode:

cargo fmt -- --check

This command is expected to fail and display a diff. cargo fmt selects the package's Rust files. The first -- ends Cargo's options, and the second --check is passed to rustfmt. Check mode reports differences but does not rewrite the file, which makes it useful in automated checks.

Now apply the formatter:

cargo fmt

Open the source once more:

nano src/lib.rs

The first function now has consistent spaces, line breaks, and indentation. Its names and logic are unchanged. Exit nano and confirm that check mode is quiet:

cargo fmt -- --check

No output and a successful exit mean every Rust file already matches rustfmt's rules.

Repair Clippy Warnings

In this step, you will use Clippy to find code that compiles but can express its intent more clearly.

The Rust compiler checks whether code is valid and type-safe. Clippy adds lints for suspicious, needlessly complex, or non-idiomatic patterns. Run it with warnings promoted to errors:

cargo clippy -- -D warnings

This first run is expected to fail. As with rustfmt, -- passes the remaining option to the underlying tool. -D warnings means deny warnings, so the quality check exits nonzero until every reported warning is repaired.

Clippy identifies two focused improvements: use the direct emptiness method instead of comparing a length with zero, and accept a slice instead of requiring callers to own a Vec. Open the source:

nano src/lib.rs

Change:

if cleaned.len() == 0 {

to:

if cleaned.is_empty() {

Then change the open_count parameter from:

tasks: &Vec<bool>

to:

tasks: &[bool]

is_empty() states the question directly. A slice accepts borrowed sequence data without unnecessarily requiring the concrete vector container. Save and exit nano, format the small edit, and rerun Clippy:

cargo fmt
cargo clippy -- -D warnings

A final Finished line with no warning proves the library compiles cleanly under the stricter lint policy.

Document the Public Interface

In this step, you will add documentation comments and generate browsable API documentation.

Comments beginning with /// document the item immediately below them. Comments beginning with //! describe the enclosing crate or module. Rustdoc turns both forms into linked HTML documentation.

Open the library source:

nano src/lib.rs

Add these two lines at the very top:

//! Small helpers for preparing task data for reports.
#![deny(missing_docs)]

The inner attribute makes missing documentation on public items a build error. It converts documentation from a suggestion into an explicit project policy.

Add this comment immediately above normalize_title:

/// Returns a trimmed title, or `Untitled` when the input is blank.

Add this comment immediately above open_count:

/// Counts entries whose completion value is `false`.

Save and exit nano. Generate documentation for this package only:

cargo doc --no-deps

cargo doc runs rustdoc. The --no-deps option skips documentation for dependency crates, keeping the result focused and faster. The generated entry page is target/doc/handoff_helpers/index.html; Cargo changes the package hyphen to an underscore in the Rust crate name.

ls target/doc/handoff_helpers/index.html

Seeing that path proves rustdoc produced the crate page and that the missing-docs policy passed.

Run the Complete Quality Loop

In this step, you will combine the individual tools into a predictable pre-handoff sequence.

Formatting, linting, documentation, and tests answer different questions:

  • rustfmt asks whether the source has the standard layout;
  • Clippy asks whether known suspicious or unclear patterns remain;
  • tests ask whether required behavior still works;
  • rustdoc asks whether the public interface can be documented under the project's policy.

Run each check separately so a failure points to one clear boundary. Start with formatting:

cargo fmt -- --check

Run strict linting:

cargo clippy -- -D warnings

Run the library tests:

cargo test

The output should report two passed tests. Finally, regenerate focused documentation:

cargo doc --no-deps

When all four commands succeed in this order, the crate is consistently formatted, lint-clean, behaviorally tested, and documented. Running the same loop before handoff turns quality into repeatable evidence rather than a final visual guess.

Summary

You repaired formatting with rustfmt, resolved strict Clippy findings, documented a public library interface, generated rustdoc output, and kept the tests passing. More importantly, you combined those tools into a repeatable quality loop that can support a reliable project handoff.