Modular Rust File Organization

RustRustBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, the file hierarchy of the modules in the code example can be represented as follows: There is a directory named "my" which contains two files, "inaccessible.rs" and "nested.rs". Additionally, there is a file named "my.rs" and a file named "split.rs". The "split.rs" file includes the module "my" which is defined in the "my.rs" file, and the "my.rs" file includes the modules "inaccessible" and "nested" which are defined in the "inaccessible.rs" and "nested.rs" files respectively.

Note: If the lab does not specify a file name, you can use any file name you want. For example, you can use main.rs, compile and run it with rustc main.rs && ./main.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL rust(("`Rust`")) -.-> rust/FunctionsandClosuresGroup(["`Functions and Closures`"]) rust(("`Rust`")) -.-> rust/DataStructuresandEnumsGroup(["`Data Structures and Enums`"]) rust(("`Rust`")) -.-> rust/ProjectManagementandOrganizationGroup(["`Project Management and Organization`"]) rust/FunctionsandClosuresGroup -.-> rust/function_syntax("`Function Syntax`") rust/FunctionsandClosuresGroup -.-> rust/expressions_statements("`Expressions and Statements`") rust/DataStructuresandEnumsGroup -.-> rust/method_syntax("`Method Syntax`") rust/ProjectManagementandOrganizationGroup -.-> rust/module_system("`Module System`") subgraph Lab Skills rust/function_syntax -.-> lab-99336{{"`Modular Rust File Organization`"}} rust/expressions_statements -.-> lab-99336{{"`Modular Rust File Organization`"}} rust/method_syntax -.-> lab-99336{{"`Modular Rust File Organization`"}} rust/module_system -.-> lab-99336{{"`Modular Rust File Organization`"}} end

File hierarchy

Modules can be mapped to a file/directory hierarchy. Let's break down the visibility example in files:

$ tree .
.
├── my
│   ├── inaccessible.rs
│   └── nested.rs
├── my.rs
└── split.rs

In split.rs:

// This declaration will look for a file named `my.rs` and will
// insert its contents inside a module named `my` under this scope
mod my;

fn function() {
    println!("called `function()`");
}

fn main() {
    my::function();

    function();

    my::indirect_access();

    my::nested::function();
}

In my.rs:

// Similarly `mod inaccessible` and `mod nested` will locate the `nested.rs`
// and `inaccessible.rs` files and insert them here under their respective
// modules
mod inaccessible;
pub mod nested;

pub fn function() {
    println!("called `my::function()`");
}

fn private_function() {
    println!("called `my::private_function()`");
}

pub fn indirect_access() {
    print!("called `my::indirect_access()`, that\n> ");

    private_function();
}

In my/nested.rs:

pub fn function() {
    println!("called `my::nested::function()`");
}

#[allow(dead_code)]
fn private_function() {
    println!("called `my::nested::private_function()`");
}

In my/inaccessible.rs:

#[allow(dead_code)]
pub fn public_function() {
    println!("called `my::inaccessible::public_function()`");
}

Let's check that things still work as before:

$ rustc split.rs && ./split
called `my::function()`
called `function()`
called `my::indirect_access()`, that
> called `my::private_function()`
called `my::nested::function()`

Summary

Congratulations! You have completed the File Hierarchy lab. You can practice more labs in LabEx to improve your skills.

Other Rust Tutorials you may like