5 Clean up your codebase

 

This chapter covers

  • Grouping your functions into modules
  • Splitting your modules into files
  • Creating a practical folder structure within your Rust project
  • Understanding the difference between doc comments and hidden comments
  • Adding example code in your comments and testing it
  • Using Clippy to lint your code
  • Using Cargo to format and compile your codebase

Rust comes equipped with a large set of tools that makes it easy to organize, structure, test, and comment it. The ecosystem values good documentation style, which is the reason Rust has a built-in commenting system that generates code documentation on the fly, and even tests the code in your comments so your documentation is never out-of-date.

A widely supported linter called Clippy is the de facto standard in the Rust world. It comes with many preset rules and helps point out best practices or missing implementations. In addition, Rust’s package manager Cargo helps autoformat your code based on predefined rules.

In the preceding chapter, we built out API routes for our Q&A applications, extracted information out of URL parameters, and most importantly, added our own structures and error implementations and handling. We added all this into the main.rs file, which grew with every route handler we added.

5.1 Modularizing your code

5.1.1 Using Rust’s built-in mod system

5.1.2 Practical folder structure for different use cases

5.1.3 Creating libraries and sub-crates

5.2 Documenting your code

5.2.1 Using doc comments and private comments

5.2.2 Adding code in your comments

5.3 Linting and formatting your codebase

5.3.1 Installing and using Clippy

5.3.2 Formatting your code with Rustfmt

Summary