Rust module system

Hi Team,

I have gone through many websites and articles to understand how the module system works. But none of them didn't explain in a clear concise way. How to create modules and access them in any other file.

Could you please explain how how to access a module anywhere is the application directory hierarchy irrespective of the hierarchy at which it is declared.

For example I might create a module many levels deep /src/one/two/three/four/my_module.rs

and want to access it a file at /src/one/access_modue.rs

I'd mostly go the main.rs as the entry point to the application.

Thanks,
Arun

In your case it would be super::two::three::four::my_module, for a "relative" path.
For an absolute path, it would be crate::one::two::three::four::my_module.
In general, for helping in visualization, you can draw a tree for the modules and find a suitable path.

There are three simple rules for namespacing and paths:

  1. Absolute paths like foo::bar::Baz are assumed to begin with a top-level crate name unless the first path element is already in scope. IOW, foo::bar::Baz designates the type Baz from module bar of crate foo.
  2. Accessing other modules relative to the current module can be done by prefixing the path with self:: (for child modules) or super:: (for parent modules).
  3. Accessing other modules of your own crate in an absolute manner is done by prefixing the path with crate::.

So in order to access my_module, you could for example write use crate::one::two::three::four::my_module::SomeType;.

2 Likes

Where should I use the mod key word?

By default, the Rust compiler only reads one file: main.rs. The mod keyword directs the compiler to read in some other file as a module, which can then be referred to elsewhere via use statements.

I'm getting this error

4 | crate::modules::frontend_module::front_end_function;
  |                                                    ^ expected one of `!` or `::`

error: aborting due to previous error

error: could not compile `rustExample`

To learn more, run the command again with --v

You should use the mod keyword when you are declaring or defining a module. It is not used for accessing (importing) a module; for that, use the use keyword, as demonstrated in my previous answer.

Yes, that's precisely because you omitted the use keyword.

1 Like

Should I use both use and mod key word where we want to access the module?

No. Again, mod is for defining a module. It is not used for accessing a module.

2 Likes

Yes. I use the use keyword to access a module. How should I put the path to declare/define a file as a module?

The mod statement should appear in the parent of the module you're defining. For crate::modules::frontend_module::front_end_function:

  • main.rs should contain the statement pub mod modules;
  • modules.rs should contain the statement pub mod frontend_module;
  • modules/frontend_module.rs should contain pub fn front_end_function() { ... }
1 Like

mod keyword behaves exactly like struct, enum, and fn keywords. It defines a new named item in the file where it exists.

4 Likes

My file structure is like this. I want to access frontend_module.rs functions in test_modules.rs.

You need a modules.rs (see my previous comment for details). Additionally:

  • main.rs needs to contain the statement mod test_module; (so that the file is read at all), and
  • test_module.rs should contain one of these statements:
    • use crate::modules::frontend_module; to use names like frontend_module::some_fn
    • use crate::modules::frontend_module::*; to use names like some_fn, or
    • use crate::modules::frontend_module::{function1, function2}; to only use function1 and function2

Her is my code: https://github.dev/arunkumar413/rustExample/tree/master/src

It's throwing the error:

   Compiling rustExample v0.1.0 (/home/arun/Documents/Rust projects/rustExample)
error[E0432]: unresolved import `crate::modules`
 --> src/test_module.rs:3:12
  |
3 | use crate::modules::frontend_module;
  |            ^^^^^^^ maybe a missing crate `modules`?

As was said before:

Specifically:

1 Like

Why should I create a additional module.rs file which will not be used?

It is used in the crate::modules path. If you don't need this intermediate step, well, just place frontend_module.rs in the crate root and use it as crate::frontend_module.

1 Like

I just read this article it doesn't use any use crate:: syntax to access the file.

Well, when you at the crate root, crate::foo is the same as foo, so the prefix can be omitted.

1 Like