File hierarchy
Modules can be mapped to a file/directory hierarchy. Let's break down the visibility example in files:
$ tree .
.
├── src
│ ├── my
│ │ ├── inaccessible.cairo
│ │ └── nested.cairo
│ ├── my.cairo
│ └── lib.cairo
└── Scarb.toml
In src/lib.cairo:
// This declaration will look for a file named `my.cairo` and will
// insert its contents inside a module named `my` under this scope
mod my;
fn function() {
println!("called `function()`");
}
fn main() {
my::function();
function();
my::indirect_access();
my::nested::function();
}
In src/my.cairo:
// Similarly `mod inaccessible` and `mod nested` will locate the `nested.cairo`
// and `inaccessible.cairo` files and insert them here under their respective
// modules
mod inaccessible;
pub mod nested;
pub fn function() {
println!("called `my::function()`");
}
fn private_function() {
println!("called `my::private_function()`");
}
pub fn indirect_access() {
println!("called `my::indirect_access()`, that");
println!("> ");
private_function();
}
In src/my/inaccessible.cairo:
pub fn public_function() {
println!("called `my::inaccessible::public_function()`");
}
Let's check that things still work as before:
$ scarb cairo-run
warn: `scarb cairo-run` will be deprecated soon
help: use `scarb execute` instead
Compiling split v0.1.0 (listings/mod/split/Scarb.toml)
Finished `dev` profile target(s) in 3 seconds
Running split
called `my::function()`
called `function()`
called `my::indirect_access()`, that
>
called `my::private_function()`
called `my::nested::function()`
Run completed successfully, returning []