You can only import modules of files within the module of your main file ( main.rs
, lib.rs
or mod.rs
generally), as Caio said. This basically means that to use both game.rs
and game_state.rs
you should import game.rs
and game_state.rs
all through main.rs
:
main.rs
// Importa game.rs ou game/mod.rs
mod game;
// Importa game_state.rs ou game_state/mod.rs
mod game_state;
The Rust compiler sees the structure of the modules in a very different way from C # ( csc
) and other languages out there. Let's reflect a bit ... main.rs
belongs to a module, the root of your crate, but all other rs
files, divergent from the main file, main.rs
, belong to the child modules or children of children. To access a statement from the parent module you use super::Decl
, for example, let's assume that ...
-
main.rs
has a struct Global
,
- The module that
main.rs
belongs to is the relative of the module to which game.rs
belongs and
- which
game.rs
uses the struct Global
of main.rs
.
main.rs
pub struct Global {}
mod game;
game.rs
use super::Global;
// Sinta-se livre para usar a struct Global
// pelo game.rs inteiro.
This is an example of how to use parent module declarations. However, to use child modules, you always need self::
, because what you type in a use
statement is absolute path to the root module of your crate, ie, that code would not work there:
main.rs
mod game;
use game::Shared; // Falha, pois 'game' não existe
// na sua crate.
game.rs
pub struct Shared {}
Here you should use use self::game::Shared
instead of game::Shared
. And I think that's enough to understand Rust's modules.