noir by example

nested modules

nesting modules allow for more complex package structures

in this example project,

├── Nargo.toml
└── src
   ├── foo
   │  └── bar.nr
   ├── foo.nr
   └── main.nr

we declare the foo module in our main entrypoint

mod foo; // <-- declare the foo module

fn main(x: Field) {
    assert(foo::hello(x) != foo::bar::goodbye(x));
}

#[test]
fn test_main() {
    main(1);
}

its implementation lives in foo.nr at the same folder depth,

mod bar; // <-- declare the nested bar module

fn hello(x : Field) -> Field {
    x
}

while the bar module’s implementation goes under a foo folder, inside bar.nr

fn goodbye(x : Field) -> Field {
    x - 1
}

try it out install the noir toolchain if you haven't yet:
        curl -L https://noirup.org/install | bash
        noirup --nightly
        
then checkout and run the example:
        git clone https://github.com/sambarnes/noir-by-example.git
        cd noir-by-example/circuits/examples/modules-nested
        nargo test --show-output
        

prev (modules) next (submodules)