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.
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:
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.
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).
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;.
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.
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.
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.