liveBook · Manning

Refactoring

As explained in Rust Servers, Services, and Apps

Code refactoring is the restructuring of existing source code without changing its externally observable behavior.

Code refactoring restructures existing source code without changing externally observable behavior. A practical refactoring separates handlers, database access, models, routes, errors, scripts, and application state into focused Rust modules. section 6.1

A safe structural refactoring involves:

  • Creating module files and grouping related code under folders such as handlers and dbaccess. section 6.1
  • Moving health_check_handler into general.rs and updating module declarations and import paths. section 6.1
  • Running cargo check, then cargo test, and revisiting the changed steps if errors appear. section 6.1

Restructuring a Rust web project

Move the health-check handler from course.rs to general.rs, declare the new module, adjust imports across the binary, database access, handlers, and routes, then verify the result with cargo check and cargo test. section 6.1

Figure 6.1 Project structure for chapters 5 and 6

Use the project-structure figure to inspect how related Rust files are grouped after refactoring. section 6.1

How Rust Servers, Services, and Apps covers it

How deep it goes
Walks through file moves, module declarations, import updates, compilation, and tests, but does not detail behavioral refactoring.
Examples
Uses handler relocation, folder-based module grouping, updated imports, cargo check, and cargo test.
What it assumes
Assumes familiarity with the existing Rust project and its modules, while demonstrating the restructuring steps from the current layout.
What you can do after
Apply it to reorganize a Rust project and verify the result through compilation and tests.
How it connects
Connects single responsibility, Rust modules, namespaces, project structure, compilation, and testing.

Also explained in

Compare how 22 books explain refactoring →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust Servers, Services, and Apps. The section links point to the parts of the book it draws on.