In Rust, what is the purpose of the mod.rs file?

In some Rust projects that I saw (i.e. pczarn / rustboot ), I saw mod.rs files in directories for any reason, I could not find the documentation for this, and I saw this in many other Rust projects. What is the purpose of the mod.rs file and when should I use it?

+6
source share
1 answer

Imagine the following directory structure:

  code /
   `- main.rs
    - something /
      `- mod.rs

If in main.rs you are doing mod something; then it will look in the something/mod.rs for use as the contents of the module declaration for something .

An alternative to this is the presence of the something.rs file in the code/ directory.

So, to write when you write an empty module declaration, for example mod something; , it looks either in:

  • file named something.rs in the same directory
  • a file called mod.rs in a folder named something in the same directory

It then uses the contents of any of these files to be used as the contents of the module declaration.

+6
source

Source: https://habr.com/ru/post/976897/


All Articles