liveBook · Manning

Chapter 5: Clean up your codebase

What you will learn in chapter 5 of Rust Web Development

This introduction motivates cleaning up the growing web-service codebase and describes Rust’s documentation, linting, and formatting support.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This introduction motivates cleaning up the growing web-service codebase and describes Rust’s documentation, linting, and formatting support.

Key terms: main.rs, route handler, commenting system, code documentation, Clippy, linter, Cargo, autoformat

5.1 Modularizing your code

This section explains why to organize a growing Rust application into logical units and distinguishes binary crates from library crates.

Key terms: logical components, main.rs, folders and files, binary crate, library crate, lib.rs, --lib

5.1.1 Using Rust’s built-in mod system

This section groups error handling into a Rust module and resolves the resulting scope, path, and visibility errors.

Key terms: mod, module, snake_case, error module, scope, import, Reject, error::Error, error::return_error, pub

5.1.2 Practical folder structure for different use cases

This section moves code out of main.rs and shows how to connect and access modules organized as files and folders.

Key terms: main.rs, error.rs, routes, mod {}, scope, error::, mod, mod.rs, pub mod, private

5.1.3 Creating libraries and sub-crates

This section explains when to extract code into a library and demonstrates moving application error handling into a local sub-crate.

Key terms: sub-crate, folders, separate files, Git flow, library, lib.rs, Cargo.toml, local path, handle-errors, Warp

5.2 Documenting your code

This section introduces Rust’s built-in documentation system, its treatment of comments, and the value of documenting applications.

Key terms: documentation, Cargo, traits, functions, public comments, private comments, doc comments, Markdown, comments

5.2.1 Using doc comments and private comments

This section distinguishes Rust documentation comments from unpublished comments, recommends what to document, and shows how to generate and browse the documentation.

Key terms: doc comments, one-line doc comment, block doc comment, business perspective, examples section, cargo doc, cargo doc --open, docs.rs, crates.io

5.2.2 Adding code in your comments

This section explains how to add and generate documentation with Rust code examples and how testing those examples helps keep them current.

Key terms: doc comments, Markdown, cargo doc --open, syntax highlighting, tests, library, binary

5.3 Linting and formatting your codebase

The section introduces Rust’s standard code-quality tools and their value for consistent tooling.

Key terms: Clippy, Rustfmt, linting, standard tools

5.3.1 Installing and using Clippy

This section shows how to configure and run Clippy, use its warnings to improve code, and select collections of linting rules.

Key terms: Clippy, rustup, clippy.toml, .clippy.toml, main.rs, lib.rs, cargo clippy, cargo clean, is_empty, linting rule

5.3.2 Formatting your code with Rustfmt

This section introduces Rustfmt for automatically formatting a Rust project and configuring its formatting rules.

Key terms: Rustfmt, rustfmt.toml, Cargo.toml, standard format settings

5.4 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 4: Implement a RESTful API Chapter 6: Logging, tracing, and debugging →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.