Skip to content

Rust Modules — Organizing Code with Modules, Files, and Visibility

DodaTech Updated 2026-06-28 1 min read

In this tutorial, you will learn about Rust Modules. We cover key concepts, practical examples, and best practices to help you master this topic.

Rust modules organize code with mod declarations, pub visibility, use for imports, and file system hierarchy for project structure.

What You'll Learn

  • Module declarations
  • Visibility (pub)
  • Use and as
  • File system hierarchy

Why It Matters

Modules structure real projects. DodaZIP uses modules to separate compression, encryption, and I/O concerns.

Real-World Use

Library organization, project structure, API visibility control, code reuse.

// src/lib.rs
pub mod front_of_house {
    pub mod hosting {
        pub fn add_to_waitlist() {}
        fn seat_at_table() {}
    }

    mod serving {
        fn take_order() {}
        fn serve_order() {}
    }
}

pub fn eat_at_restaurant() {
    // Absolute path
    crate::front_of_house::hosting::add_to_waitlist();

    // Relative path
    front_of_house::hosting::add_to_waitlist();
}

// Using 'use' for shorter paths
use crate::front_of_house::hosting;

pub fn eat_at_restaurant_2() {
    hosting::add_to_waitlist();
}

// Re-exporting
pub use crate::front_of_house::hosting;

// External crate
// use rand::Rng;

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro