Estructura que vamos a recrear como ejemplo (tomada originalmente de https://doc.rust-lang.org/rust-by-example/mod/visibility.html) :
**.
├── my
│ ├── inaccessible.rs
│ └── nested.rs
├── my.rs
└── main.rs**
> main.rs
// This declaration will look for a file named `my.rs` (en el mismo directorio)
//and will insert its contents inside a module named `my` under this scope.
//Como que copia y pega el mod my (similar a C) para poder usarlo.
mod my;
fn function() {
println!("called `function()`");
}
fn main() {
my::function();
function();
my::indirect_access();
my::nested::function();
}
> my.rs
// Similarly, `mod inaccessible` and `mod nested` will locate the `nested.rs`
// and `inaccessible.rs` files (en una carpeta que debe tener el nombre del archivo
//en este caso, my) and insert them here under their respective modules.
mod inaccessible;
pub mod nested; //el pub hace que al usar el mod my, tambien se pueda el nested
pub fn function() {
println!("called `my::function()`");
}
fn private_function() {
println!("called `my::private_function()`");
}
pub fn indirect_access() {
print!("called `my::indirect_access()`, that\\n> ");
private_function();
}
> my/nested.rs
pub fn function() {
println!("called `my::nested::function()`");
}
fn private_function() {
println!("called `my::nested::private_function()`");
}
> my/inaccessible.rs
pub fn public_function() {
println!("called `my::inaccessible::public_function()`");
}