PageSourceSearch

https://noir-lang.org/docs/assets/js/bcf1b93c.7b7b50ab.js

js noir-lang.org collected 2026-10-03 22:52:00 UTC 4,818 bytes, 1 lines download raw bytes

1"use strict";(globalThis.webpackChunkdocs=globalThis.webpackChunkdocs||[]).push([[1840],{15126(e,n,l){l.r(n),l.d(n,{assets:()=>t,contentTitle:()=>r,default:()=>u,frontMatter:()=>s,metadata:()=>a,toc:()=>c});const a=JSON.parse('{"id":"language/globals","title":"Global Variables","description":"Learn about global variables in Noir. Discover how to declare, modify, and use them in your programs.","source":"@site/versioned_docs/version-v1.0.0-rc.0/language/globals.md","sourceDirName":"language","slug":"/language/globals","permalink":"/docs/v1.0.0-rc.0/language/globals","draft":false,"unlisted":false,"editUrl":"https://github.com/noir-lang/noir/edit/master/docs/versioned_docs/version-v1.0.0-rc.0/language/globals.md","tags":[],"version":"v1.0.0-rc.0","frontMatter":{"title":"Global Variables","description":"Learn about global variables in Noir. Discover how to declare, modify, and use them in your programs.","keywords":["noir programming language","globals","global variables","constants"]},"sidebar":"sidebar","previous":{"title":"Generics","permalink":"/docs/v1.0.0-rc.0/language/generics"},"next":{"title":"Mutability","permalink":"/docs/v1.0.0-rc.0/language/mutability"}}');var i=l(74848),o=l(28453);const s={title:"Global Variables",description:"Learn about global variables in Noir. Discover how to declare, modify, and use them in your programs.",keywords:["noir programming language","globals","global variables","constants"]},r=void 0,t={},c=[{value:"Globals",id:"globals",level:2},{value:"Visibility",id:"visibility",level:3}];function d(e){const n={admonition:"admonition",code:"code",h2:"h2",h3:"h3",p:"p",pre:"pre",...(0,o.R)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(n.h2,{id:"globals",children:"Globals"}),"\n",(0,i.jsx)(n.p,{children:"Noir supports global variables. The global's type must be specified by the user:"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-rust",children:"global N: Field = 5;\n\nglobal TUPLE: (Field, Field) = (3, 2);\n\nfn main() {\n    assert(N == 5);\n    assert(N == TUPLE.0 + TUPLE.1);\n}\n"})}),"\n",(0,i.jsxs)(n.admonition,{type:"info",children:[(0,i.jsx)(n.p,{children:"Globals can be defined as any expression, so long as they don't depend on themselves - otherwise there would be a dependency cycle! For example:"}),(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-rust",children:"global T: u32 = foo(T); // dependency error\n"})})]}),"\n",(0,i.jsx)(n.p,{children:"If they are initialized to a literal integer, globals can be used to specify an array's length:"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-rust",children:"global N: u32 = 2;\n\nfn main(y : [Field; N]) {\n    assert(y[0] == y[1])\n}\n"})}),"\n",(0,i.jsx)(n.p,{children:"A global from another module can be imported or referenced externally like any other name:"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-rust",children:"global N: Field = 20;\n\nfn main() {\n    assert(my_submodule::N != N);\n}\n\nmod my_submodule {\n    global N: Field = 10;\n}\n"})}),"\n",(0,i.jsx)(n.p,{children:"When a global is used, Noir replaces the name with its definition on each occurrence.\nThis means globals defined using function calls will repeat the call each time they're used:"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-rust",children:"global RESULT: [Field; 100] = foo();\n\nfn foo() -> [Field; 100] { ... }\n"})}),"\n",(0,i.jsxs)(n.p,{children:["This is usually fine since Noir will generally optimize any function call that does not\nrefer to a program input into a constant. It should be kept in mind however, if the called\nfunction performs side-effects like ",(0,i.jsx)(n.code,{children:"println"}),", as these will still occur on each use."]}),"\n",(0,i.jsx)(n.h3,{id:"visibility",children:"Visibility"}),"\n",(0,i.jsxs)(n.p,{children:["By default, like functions, globals are private to the module they exist in. You can use ",(0,i.jsx)(n.code,{children:"pub"}),"\nto make the global public or ",(0,i.jsx)(n.code,{children:"pub(crate)"})," to make it public to just its crate:"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-rust",children:"// This global is now public\npub global N: u32 = 5;\n"})})]})}function u(e={}){const{wrapper:n}={...(0,o.R)(),...e.components};return n?(0,i.jsx)(n,{...e,children:(0,i.jsx)(d,{...e})}):d(e)}},28453(e,n,l){l.d(n,{R:()=>s,x:()=>r});var a=l(96540);const i={},o=a.createContext(i);function s(e){const n=a.useContext(o);return a.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function r(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(i):e.components||i:s(e.components),a.createElement(o.Provider,{value:n},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.