1"use strict";(globalThis.webpackChunkdocs=globalThis.webpackChunkdocs||[]).push([[352],{27393(e,n,a){a.r(n),a.d(n,{assets:()=>l,contentTitle:()=>r,default:()=>u,frontMatter:()=>o,metadata:()=>t,toc:()=>c});const t=JSON.parse('{"id":"language/shadowing","title":"Shadowing","description":"Rebind variables with the same name to derive new values, enabling ergonomic \\"temporary mutability\\" patterns in Noir.","source":"@site/versioned_docs/version-v1.0.0-rc.2/language/shadowing.md","sourceDirName":"language","slug":"/language/shadowing","permalink":"/docs/language/shadowing","draft":false,"unlisted":false,"editUrl":"https://github.com/noir-lang/noir/edit/master/docs/versioned_docs/version-v1.0.0-rc.2/language/shadowing.md","tags":[],"version":"v1.0.0-rc.2","frontMatter":{"title":"Shadowing","description":"Rebind variables with the same name to derive new values, enabling ergonomic \\"temporary mutability\\" patterns in Noir."},"sidebar":"sidebar","previous":{"title":"Comments","permalink":"/docs/language/comments"},"next":{"title":"Data Bus","permalink":"/docs/language/data_bus"}}');var i=a(74848),s=a(28453);const o={title:"Shadowing",description:'Rebind variables with the same name to derive new values, enabling ergonomic "temporary mutability" patterns in Noir.'},r=void 0,l={},c=[{value:"Temporal mutability",id:"temporal-mutability",level:2}];function d(e){const n={code:"code",h2:"h2",p:"p",pre:"pre",...(0,s.R)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(n.p,{children:"Noir allows for inheriting variables' values and re-declaring them with the same name similar to Rust, known as shadowing."}),"\n",(0,i.jsx)(n.p,{children:"For example, the following function is valid in Noir:"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-rust",children:"fn main() {\n let x = 5;\n\n {\n let x = x * 2;\n assert (x == 10);\n }\n\n assert (x == 5);\n}\n"})}),"\n",(0,i.jsx)(n.p,{children:"In this example, a variable x is first defined with the value 5."}),"\n",(0,i.jsx)(n.p,{children:"The local scope that follows shadows the original x, i.e. creates a local mutable x based on the value of the original x. It is given a value of 2 times the original x."}),"\n",(0,i.jsx)(n.p,{children:"When we return to the main scope, x once again refers to just the original x, which stays at the value of 5."}),"\n",(0,i.jsx)(n.h2,{id:"temporal-mutability",children:"Temporal mutability"}),"\n",(0,i.jsx)(n.p,{children:"One way that shadowing is useful, in addition to ergonomics across scopes, is for temporarily mutating variables."}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-rust",children:"fn main() {\n let age = 30;\n // age = age + 5; // Would error as `age` is immutable by default.\n\n let mut age = age + 5; // Temporarily mutates `age` with a new value.\n\n let age = age; // Locks `age`'s mutability again.\n\n assert (age == 35);\n}\n"})})]})}function u(e={}){const{wrapper:n}={...(0,s.R)(),...e.components};return n?(0,i.jsx)(n,{...e,children:(0,i.jsx)(d,{...e})}):d(e)}},28453(e,n,a){a.d(n,{R:()=>o,x:()=>r});var t=a(96540);const i={},s=t.createContext(i);function o(e){const n=t.useContext(s);return t.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:o(e.components),t.createElement(s.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.