Modules & Visibility in Rust

From the Rust cheat sheet · Modules & Cargo · verified Jul 2026

Modules & Visibility

Organize code into modules with pub/use and file-based structure.

rust
// Inline module
mod utils {
    pub fn helper() -> String {
        "help".to_string()
    }
}
use utils::helper;

// File-based modules:
// src/main.rs    — declares: mod routes;
// src/routes.rs  — or src/routes/mod.rs
💡 Everything is private by default — add pub to make it accessible outside the module
⚡ pub(crate) makes something public within your crate but hidden from external users
📌 Module files: mod routes; looks for src/routes.rs or src/routes/mod.rs
🟢 Use pub use for re-exports — lets users import from a convenient path
modulesvisibilityusecargo
Back to the full Rust cheat sheet