liveBook · Manning

Module organization

As explained in Rust Servers, Services, and Apps

Module organization is the arrangement of Rust modules and their source files together with the paths used to declare and import them.

Organize a Rust application by splitting responsibilities across folders, modules, and source files, then align declarations and imports with that structure. section 6.1

A practical reorganization follows these steps:

  • Declare directory modules in each mod.rs file, such as models, dbaccess, and handlers. section 6.1
  • Split handlers by responsibility, moving health_check_handler into general.rs while retaining course-specific handlers. section 6.1
  • Update #[path] declarations in the binary and revise crate, super, and grouped imports in dependent modules. section 6.1
  • Run cargo check, then cargo test, and revisit the refactoring steps if compilation or tests fail. section 6.1

Reorganizing an EzyTutor subsystem

Place Course under models/course.rs, expose it through models/mod.rs, and import it with use crate::models::course::Course;. Declare handlers through handlers/mod.rs, map the binary to iter5 modules with #[path], and update routes.rs to use crate::handlers::{course::*, general::*};. section 6.1

Figure 6.1 Project structure for chapters 5 and 6

Use the project-structure diagram to connect folder layout with module organization before editing declarations and imports. section 6.1

How Rust Servers, Services, and Apps covers it

How deep it goes
Walks through directory declarations, handler movement, import corrections, and compilation tests, stopping short of deeper module-system theory.
Examples
Uses an EzyTutor layout with models, dbaccess, handlers, routes, a moved health-check handler, and Cargo verification commands.
What it assumes
Assumes an existing Rust server project and familiarity with its handlers, models, routes, and database-access modules.
What you can do after
Apply module declarations and imports while reorganizing a Rust server, then diagnose failures with cargo check and cargo test.
How it connects
Connects module organization with module paths, imports, project structure, single responsibility, compilation, and testing.

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.