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() { /* ... */ }
- Create a library crate by running
cargo new --lib my-project. - Define a new module file for each logical component, such as
src/database.rs. - Declare the module as public in
src/lib.rsusingpub mod database;. - Mark specific functions or structs as public with the
pubkeyword inside the module file. - Use the module path to access items from other parts of the code, like
my_project::database::connect().