liveBook · Manning

Mod.rs

As explained in Rust Servers, Services, and Apps

mod.rs is a Rust source-file convention for declaring the contents and child modules of a directory-based module.

mod.rs is the directory entry point that turns grouped Rust source files into a module. In a reorganized web application, handlers, models, and database-access code each receive a directory and a mod.rs file so the compiler can locate dependent files. section 6.1

Declaring child modules

A models/mod.rs file can declare its children with `pub mod course;` and `pub mod tutor;`. Code elsewhere can then import a type through `use crate::models::course::Course;`. A handlers/mod.rs similarly declares `course`, `tutor`, and `general`.

A directory-based module structure can separate:

  • handlers/mod.rs from course.rs, tutor.rs, and general.rs section 6.1
  • models/mod.rs from course.rs and tutor.rs section 6.1
  • dbaccess/mod.rs from course.rs and tutor.rs section 6.1

How Rust Servers, Services, and Apps covers it

How deep it goes
Explains child-module declarations and imports, but does not descend into compiler or module-resolution internals.
Examples
Uses handlers, models, and database-access directories containing mod.rs plus feature-specific Rust files.
What it assumes
Assumes familiarity with Rust files and imports while demonstrating the directory layout from an existing application.
What you can do after
Apply it by reorganizing an existing Rust application into directory modules and importing their nested definitions.
How it connects
Connects directory modules with Rust module paths, visibility, imports, and project structure.

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.