What about submodules?

When we declare top-level modules like this, Rust will expect there to be either a directory for each of these modules or four source files (Trigonometry.rs, RegressionAnalysis.rs, Conversions.rs, and Bases.rs). If the directory structure is used, Rust will expect a file called mod.rs in each directory.

Let's compare how these two systems look, and then we can examine the relative benefits of each. The structure of MathsLibMultiFile will be as follows:

Figure 3a

The structure of MathsLibMultiFileDirs will be as follows:

Figure 3b

At first glance, they seem to be very similar; the only difference is that multifiledir (Fig 3b) has modules broken down into separate files whereas multifile (Fig 3a) only has a single file for each module. This is a limitation of the non-directory structure; the submodules are kept in a single file, which is fine for a very small module but no good for larger modules.

In the directory structure version, there is a mod.rs file. This is completely empty but is there to let the compiler know that we have submodules. If (say) RegressionAnalysis::Statistics were to be further broken down, it would be a case of creating a new directory within the RegressionAnalysis directory called Statistics (the directory must have the same name as the module) and adding a new mod.rs file as well as new submodules.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset