noir by example

structs

structs group together multiple related values, with a name for each field

define custom functions on them using impl blocks

struct Foo {
    bar: Field,
    baz: [Field; 2],
}

impl Foo {
    // implementations can be methods on struct instances
    fn some_fn(self: Self) -> Field {
        self.bar + self.baz[0] + self.baz[1]
    }

    // or static functions on the type itself
    fn default() -> Self {
        let bar = 0;
        Self { bar , baz: [1, 2] }
    }
}

// structs can be inputs to a main function, but not outputs
fn main(foo: Foo) {
    assert(foo.some_fn() == 3);
}

#[test]
fn test_main() {
    let foo = Foo::default();
    main(foo);
}

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/structs
        nargo test --show-output
        

prev (tuples) next (modules)