Can I enable modules sideways in Rust?

I want to split the Rust program into several files, but using mod does not allow me to refer to files in the same directory from files other than main.rs

For example, if I have main.rs , game.rs and matrix.rs in the same folder, I can refer to structures / functions from game.rs or matrix.rs in main.rs using mod game; or mod matrix; . However, I cannot refer matrix.rs to game.rs to an instruction like mod matrix .

I looked through several resources, and all of them have only modules, such as trees, which do not refer to each other. Is it possible to use structures / functions from files in each other in Rust, or is this against the rules? If so, why does Rust not allow you to do this?

+6
source share
1 answer

mod is a module declaration. This directive declares a module and all its contents. It so happened that this content may be located in another file. So:

 mod game; mod matrix; 

roughly equivalent to this:

 mod game { // game.rs contents } mod matrix { // matrix.rs contents } 

Naturally, since mod is a module declaration, you cannot do this several times for the same module. That is, you can try to write something like

 mod game { mod matrix; ... } mod matrix; 

but, as you can see, matrix and game::matrix are different modules, and naturally, rustc requires different paths to its corresponding files, if they are external.

use , however, is an import declaration. use declarations draw names from other modules for use in the current module. You can use any module and any public elements from it any number of times from any place where this module is available.

So, for the matrix from game link, you need to use it:

 // game.rs use matrix; 

Naturally, in order for this to work, the matrix must be declared with mod in the root directory.

As a note, I personally believe that the easiest way to understand the Rust module system is to first forget that the modules can be placed in different files. That is, think that a box can only be defined in one file. In Rust mod directives can have bodies and can nest, so nested mod actually define a modular box system:

 mod foo { mod bax { ... } mod baz { ... } } mod bar { mod qux { mod zux { ... } } } 

If you have only one file, you can easily see how the mod and use directives will work, and the connection between the modules should be clear.

And now you only need to add to the image the fact that if a module is declared without a body, for example, in mod name; , its contents are loaded either from name.rs or name/mod.rs , regardless of what is available. However, the complete picture does not change the slightest - these are still nested modules, which can always be represented as a single source file with nested mod directives. In fact, cargo rustc -Z unstable-options --pretty=normal will print your box in this form after all modules from external source files are assembled into one document. I suggest running this command on some boxes with a complex module structure to see how it looks in practice.

+11
source

All Articles