PageSourceSearch

https://noir-lang.org/docs/assets/js/6b3e3269.2363ad04.js

js noir-lang.org collected 2026-10-03 22:53:02 UTC 5,351 bytes, 1 lines download raw bytes

1"use strict";(globalThis.webpackChunkdocs=globalThis.webpackChunkdocs||[]).push([[2471],{44540(e,t,n){n.r(t),n.d(t,{assets:()=>c,contentTitle:()=>l,default:()=>u,frontMatter:()=>i,metadata:()=>s,toc:()=>o});const s=JSON.parse('{"id":"language/data_types/structs","title":"Structs","description":"Explore the Struct data type in Noir. Learn about its methods, see real-world examples, and grasp how to effectively define and use Structs in your Noir programs.","source":"@site/processed-docs/language/data_types/structs.md","sourceDirName":"language/data_types","slug":"/language/data_types/structs","permalink":"/docs/dev/language/data_types/structs","draft":false,"unlisted":false,"editUrl":"https://github.com/noir-lang/noir/edit/master/docs/docs/language/data_types/structs.md","tags":[],"version":"current","frontMatter":{"title":"Structs","description":"Explore the Struct data type in Noir. Learn about its methods, see real-world examples, and grasp how to effectively define and use Structs in your Noir programs.","keywords":["noir","struct type","methods","examples","data structures"]},"sidebar":"sidebar","previous":{"title":"Tuples","permalink":"/docs/dev/language/data_types/tuples"},"next":{"title":"References","permalink":"/docs/dev/language/data_types/references"}}');var a=n(74848),r=n(28453);const i={title:"Structs",description:"Explore the Struct data type in Noir. Learn about its methods, see real-world examples, and grasp how to effectively define and use Structs in your Noir programs.",keywords:["noir","struct type","methods","examples","data structures"]},l=void 0,c={},o=[{value:"Visibility",id:"visibility",level:3}];function d(e){const t={blockquote:"blockquote",code:"code",em:"em",h3:"h3",p:"p",pre:"pre",strong:"strong",...(0,r.R)(),...e.components};return(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)(t.p,{children:"A struct also allows for grouping multiple values of different types. Unlike tuples, we can also\nname each field."}),"\n",(0,a.jsxs)(t.blockquote,{children:["\n",(0,a.jsxs)(t.p,{children:[(0,a.jsx)(t.strong,{children:"Note:"})," The usage of ",(0,a.jsx)(t.em,{children:"field"})," here refers to each element of the struct and is unrelated to the\nfield type of Noir."]}),"\n"]}),"\n",(0,a.jsxs)(t.p,{children:["Defining a struct requires giving it a name and listing each field within as ",(0,a.jsx)(t.code,{children:"<Key>: <Type>"})," pairs:"]}),"\n",(0,a.jsx)(t.pre,{children:(0,a.jsx)(t.code,{className:"language-rust",children:"struct Animal {\n    hands: Field,\n    legs: Field,\n    eyes: u8,\n}\n"})}),"\n",(0,a.jsxs)(t.p,{children:["An instance of a struct can then be created with actual values in ",(0,a.jsx)(t.code,{children:"<Key>: <Value>"})," pairs in any\norder. Struct fields are accessible using their given names:"]}),"\n",(0,a.jsx)(t.pre,{children:(0,a.jsx)(t.code,{className:"language-rust",children:"fn main() {\n    let legs = 4;\n\n    let dog = Animal {\n        eyes: 2,\n        hands: 0,\n        legs,\n    };\n\n    let zero = dog.hands;\n}\n"})}),"\n",(0,a.jsx)(t.p,{children:"Structs can also be destructured in a pattern, binding each field to a new variable:"}),"\n",(0,a.jsx)(t.pre,{children:(0,a.jsx)(t.code,{className:"language-rust",children:"fn main() {\n    let Animal { hands, legs: feet, eyes } = get_octopus();\n\n    let ten = hands + feet + eyes as Field;\n}\n\nfn get_octopus() -> Animal {\n    let octopus = Animal {\n        hands: 0,\n        legs: 8,\n        eyes: 2,\n    };\n\n    octopus\n}\n"})}),"\n",(0,a.jsxs)(t.p,{children:["The new variables can be bound with names different from the original struct field names, as\nshowcased in the ",(0,a.jsx)(t.code,{children:"legs --\x3e feet"})," binding in the example above."]}),"\n",(0,a.jsx)(t.h3,{id:"visibility",children:"Visibility"}),"\n",(0,a.jsxs)(t.p,{children:["By default, like functions, structs are private to the module they exist in. You can use ",(0,a.jsx)(t.code,{children:"pub"}),"\nto make the struct public or ",(0,a.jsx)(t.code,{children:"pub(crate)"})," to make it public to just its crate:"]}),"\n",(0,a.jsx)(t.pre,{children:(0,a.jsx)(t.code,{className:"language-rust",children:"// This struct is now public\npub struct Animal {\n    hands: Field,\n    legs: Field,\n    eyes: u8,\n}\n"})}),"\n",(0,a.jsxs)(t.p,{children:["The same applies to struct fields: by default they are private to the module they exist in,\nbut they can be made ",(0,a.jsx)(t.code,{children:"pub"})," or ",(0,a.jsx)(t.code,{children:"pub(crate)"}),":"]}),"\n",(0,a.jsx)(t.pre,{children:(0,a.jsx)(t.code,{className:"language-rust",children:"// This struct is now public\npub struct Animal {\n    hands: Field,           // private to its module\n    pub(crate) legs: Field, // accessible from the entire crate\n    pub eyes: u8,           // accessible from anywhere\n}\n"})})]})}function u(e={}){const{wrapper:t}={...(0,r.R)(),...e.components};return t?(0,a.jsx)(t,{...e,children:(0,a.jsx)(d,{...e})}):d(e)}},28453(e,t,n){n.d(t,{R:()=>i,x:()=>l});var s=n(96540);const a={},r=s.createContext(a);function i(e){const t=s.useContext(r);return s.useMemo(function(){return"function"==typeof e?e(t):{...t,...e}},[t,e])}function l(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(a):e.components||a:i(e.components),s.createElement(r.Provider,{value:t},e.children)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.