Open In App

Rust – File Hierarchy of Modules

Improve
Improve
Like Article
Like
Save
Share
Report

In the Rust programming language, modules are used to organize code into logical units and control their visibility (i.e., whether they can be accessed from other parts of the code). Modules can be nested within other modules, and this can help to create a hierarchical structure for organizing your code.

In Rust, the file hierarchy of modules is determined by the module’s path. The path of a module is the sequence of module names that must be traversed to reach the module from the root of the crate (i.e., the top-level module). For example, consider the following module hierarchy:

rust hierarchy

In this hierarchy, the crate is the root of the module tree, and src is the child of the crate. lib is a child of src, and mod1.rs and mod2 are children of lib. mod3.rs is a child of mod2.

To define a module in Rust, you can use the mod keyword, followed by the name of the module and the code for the module in a block. For example, you could define the mod1 and mod3 modules in the hierarchy above as follows:

Example 1:

Rust




// Rust code 
mod mod1 {
    // code for mod1 goes here
}
  
mod mod2 {
    mod mod3 {
        // code for mod3 goes here
    }
}


In this code, mod1 is defined at the same level as lib, so its path is src: :lib: :mod1. mod3 is defined inside mod2, so its path is src : : lib : : mod2 : : mod3.

To reference a module from another module, you can use the use keyword, followed by the path to the module. For example, you could reference the mod3 module from the mod1 module as follows:

Example 2:

Rust




// Rust code
mod mod1 {
    use src::lib::mod2::mod3;
  
    // code that uses mod3 goes here
 }
  
mod mod2 {
    mod mod3 {
        // code for mod3 goes here
    }
}


In this code, the use statement allows mod1 to access the mod3 module, and the mod3 module can be used in the code for mod1 by prefixing its name with mod3 : :

Overall, the file hierarchy of modules in Rust is determined by the module’s path, which is a sequence of module names that must be traversed to reach the module from the root of the crate. This hierarchical structure allows you to organize your code into logical units and control its visibility.

In addition to organizing your code into a hierarchical structure, the file hierarchy of modules in Rust also determines how you specify the location of a module’s source code. In Rust, a module’s source code is typically stored in a file with the same name as the module and a .rs file extension. For example, the source code for the mod1 module would be stored in a file named mod1.rs, and the source code for the mod3 module would be stored in a file named mod3.rs.

The location of a module’s source code is determined by the module’s path. For example, the mod1 module’s source code would be stored in the file src/lib/mod1.rs, and the mod3 module’s source code would be stored in the file src/lib/mod2/mod3.rs. This file hierarchy allows you to easily find and modify the source code for a particular module.

In summary, the file hierarchy of modules in Rust is an important aspect of the language that allows you to organize your code into logical units, control their visibility, and easily find and modify the source code for each module. By using the mod and use keywords, you can create a hierarchical structure for your code, and specify the location of each module’s source code in the file system.



Last Updated : 12 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads