0Pricing
Learn Rust Coding · Lesson

Organizing Code with Modules

Learn to use Rust's module system to logically group code, manage privacy, and make your projects scalable.

Organizing Code with Modules is a free Learn Rust Coding lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Organize Your Code with Modules

As your Rust projects grow, keeping all your code in one file becomes messy. Rust's module system helps you organize code into logical units.

Modules are like folders for your code. They group related functions, structs, enums, and constants together, improving readability and maintainability.

Declaring Your First Module

You declare a module using the mod keyword, followed by the module's name. It's like creating a sub-namespace within your program.

Let's create a simple greetings module:

mod greetings {
    // This is where greeting functions would go
}

fn main() {
    println!("Hello from main!");
}

Understanding Module Privacy

By default, everything inside a module is private. This means you can't access it from outside the module without explicitly making it public.

If you try to call a private function, Rust will give you a compile error. Try uncommenting the line below and running it:

mod greetings {
    fn hello() {
        println!("Hello from the greetings module!");
    }
}

fn main() {
    // Uncomment the line below and try to run!
    // greetings::hello();
    println!("Main function running.");
}

Making Items Public with `pub`

To make an item (like a function, struct, or enum) visible from outside its module, you use the pub keyword.

  • pub fn makes a function public.
  • pub struct makes a struct public.
  • pub mod makes a submodule public, allowing its contents to be accessed.

Privacy is a core part of Rust's safety and organization!

`pub` in Action

Now, let's make our hello function public using pub. This allows the main function to call it without a privacy error.

Run this code to see the greeting from our module:

mod greetings {
    pub fn hello() {
        println!("Hello from the greetings module!");
    }
}

fn main() {
    greetings::hello();
}

Simplifying Paths with `use`

Typing out full paths like greetings::hello() can get repetitive. The use keyword lets you bring items into scope, making them easier to refer to.

It's similar to import in Python or using in C#. You can bring a specific item or an entire module into scope.

`use` in Action

Here's how you can use use to shorten the path to our hello function. Notice how we no longer need the greetings:: prefix.

This makes your code cleaner and easier to read.

mod greetings {
    pub fn hello() {
        println!("Hello, module user!");
    }
}

use greetings::hello; // Bring `hello` into scope

fn main() {
    hello(); // Now we can call it directly
}

Nested Modules & Relative Paths

Modules can be nested! You can define modules inside other modules for even finer organization. For navigating these, Rust provides super and self.

  • super refers to the parent module.
  • self refers to the current module.

These are useful for relative paths within complex module hierarchies.

mod outer {
    pub mod inner {
        pub fn call_me() {
            println!("Called from inner module!");
        }
    }

    pub fn call_inner() {
        // Accessing inner module from parent
        inner::call_me();
    }
}

fn main() {
    outer::call_inner();
    outer::inner::call_me();
}

Modules and File Structure

For larger modules, you can put their code into separate files. Rust automatically looks for module definitions in specific locations:

  • For mod my_module; in src/main.rs: Rust looks for src/my_module.rs or src/my_module/mod.rs.
  • This helps keep your main file clean and delegates content to appropriate files.

This lesson uses single files for simplicity, but remember this for bigger projects!

Module Access Check

Consider the following Rust code. Which lines will compile without a privacy error?

mod calculator {
    fn add(a: i32, b: i32) -> i32 {
        a + b
    }

    pub fn subtract(a: i32, b: i32) -> i32 {
        a - b
    }

    mod advanced {
        pub fn multiply(a: i32, b: i32) -> i32 {
            a * b
        }
    }
}

fn main() {
    // Line A
    // calculator::add(5, 3);

    // Line B
    // calculator::subtract(10, 4);

    // Line C
    // calculator::advanced::multiply(2, 6);

    // Line D
    // calculator::advanced::add(1, 1);
}

Modules: Organize & Control

Great job! You've learned how Rust's module system helps you structure your code:

  • Use mod to declare modules and submodules.
  • Items are private by default; use pub to make them accessible.
  • use simplifies long paths to items.
  • Modules can be spread across multiple files for large projects.

Modules are fundamental for managing complexity in Rust. Keep practicing!

Frequently asked questions

Is the “Organizing Code with Modules” lesson free?

Yes — the full text of “Organizing Code with Modules” is free to read here on the web, and the Learn Rust Coding course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.

What will I learn in “Organizing Code with Modules”?

Learn to use Rust's module system to logically group code, manage privacy, and make your projects scalable. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn Rust Coding?

No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Organizing Code with Modules” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn Rust Coding lesson?

Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Organizing Code with Modules
  2. Managing Dependencies with Crates
  3. Robust Error Handling with `Result`
← Back to Learn Rust Coding