How to Organize a Large Rust Project into Modules

Split large Rust projects into modules using `pub mod` in `src/lib.rs` to organize code and control visibility.

Organize a large Rust project by splitting code into modules within a library crate and exposing them via pub mod declarations in src/lib.rs.

// src/lib.rs
pub mod database;
pub mod api;

// src/database.rs
pub fn connect() { /* ... */ }

// src/api.rs
pub fn handle_request() { /* ... */ }
  1. Create a library crate by running cargo new --lib my-project.
  2. Define a new module file for each logical component, such as src/database.rs.
  3. Declare the module as public in src/lib.rs using pub mod database;.
  4. Mark specific functions or structs as public with the pub keyword inside the module file.
  5. Use the module path to access items from other parts of the code, like my_project::database::connect().