PageSourceSearch

https://noir-lang.org/docs/assets/js/c4227cfd.7a9aaa18.js

js noir-lang.org collected 2026-10-03 22:50:18 UTC 6,574 bytes, 1 lines download raw bytes

1"use strict";(globalThis.webpackChunkdocs=globalThis.webpackChunkdocs||[]).push([[582],{57483(e,n,r){r.r(n),r.d(n,{assets:()=>c,contentTitle:()=>o,default:()=>u,frontMatter:()=>a,metadata:()=>t,toc:()=>d});const t=JSON.parse('{"id":"libraries/standard_library/mem","title":"Memory Module","description":"This module contains functions which manipulate memory in a low-level way","source":"@site/versioned_docs/version-v1.0.0-rc.0/libraries/standard_library/mem.md","sourceDirName":"libraries/standard_library","slug":"/libraries/standard_library/mem","permalink":"/docs/v1.0.0-rc.0/libraries/standard_library/mem","draft":false,"unlisted":false,"editUrl":"https://github.com/noir-lang/noir/edit/master/docs/versioned_docs/version-v1.0.0-rc.0/libraries/standard_library/mem.md","tags":[],"version":"v1.0.0-rc.0","frontMatter":{"title":"Memory Module","description":"This module contains functions which manipulate memory in a low-level way","keywords":["mem","memory","zeroed","transmute","checked_transmute"]},"sidebar":"sidebar","previous":{"title":"Logging and Panics","permalink":"/docs/v1.0.0-rc.0/libraries/standard_library/logging"},"next":{"title":"Metaprogramming","permalink":"/docs/v1.0.0-rc.0/libraries/standard_library/meta/"}}');var i=r(74848),s=r(28453);const a={title:"Memory Module",description:"This module contains functions which manipulate memory in a low-level way",keywords:["mem","memory","zeroed","transmute","checked_transmute"]},o="std::mem::zeroed",c={},d=[];function l(e){const n={code:"code",h1:"h1",header:"header",li:"li",p:"p",pre:"pre",ul:"ul",...(0,s.R)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(n.header,{children:(0,i.jsx)(n.h1,{id:"stdmemzeroed",children:(0,i.jsx)(n.code,{children:"std::mem::zeroed"})})}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-rust",children:"fn zeroed<T>() -> T\n"})}),"\n",(0,i.jsx)(n.p,{children:"Returns a zeroed value of any type.\nThis function is generally unsafe to use as the zeroed bit pattern is not guaranteed to be valid for all types.\nIt can however, be useful in cases when the value is guaranteed not to be used such as in a BoundedVec library implementing a growable vector, up to a certain length, backed by an array.\nThe array can be initialized with zeroed values which are guaranteed to be inaccessible until the vector is pushed to.\nSimilarly, enumerations in noir can be implemented using this method by providing zeroed values for the unused variants."}),"\n",(0,i.jsx)(n.p,{children:"This function currently supports the following types:"}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:"Field"}),"\n",(0,i.jsx)(n.li,{children:"Bool"}),"\n",(0,i.jsx)(n.li,{children:"Uint"}),"\n",(0,i.jsx)(n.li,{children:"Array"}),"\n",(0,i.jsx)(n.li,{children:"Vector"}),"\n",(0,i.jsx)(n.li,{children:"String"}),"\n",(0,i.jsx)(n.li,{children:"Tuple"}),"\n",(0,i.jsx)(n.li,{children:"Functions"}),"\n"]}),"\n",(0,i.jsx)(n.p,{children:"Using it on other types could result in unexpected behavior."}),"\n",(0,i.jsx)(n.h1,{id:"stdmemchecked_transmute",children:(0,i.jsx)(n.code,{children:"std::mem::checked_transmute"})}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-rust",children:"fn checked_transmute<T, U>(value: T) -> U\n"})}),"\n",(0,i.jsxs)(n.p,{children:["Transmutes a value of one type into the same value but with a new type ",(0,i.jsx)(n.code,{children:"U"}),"."]}),"\n",(0,i.jsxs)(n.p,{children:["This function is safe to use since both types are asserted to be equal later during compilation after the concrete values for generic types become known.\nThis function is useful for cases where the compiler may fail a type check that is expected to pass where\na user knows the two types to be equal. For example, when using arithmetic generics there are cases the compiler\ndoes not see as equal, such as ",(0,i.jsx)(n.code,{children:"[Field; N*(A + B)]"})," and ",(0,i.jsx)(n.code,{children:"[Field; N*A + N*B]"}),", which users may know to be equal.\nIn these cases, ",(0,i.jsx)(n.code,{children:"checked_transmute"})," can be used to cast the value to the desired type while also preserving safety\nby checking this equality once ",(0,i.jsx)(n.code,{children:"N"}),", ",(0,i.jsx)(n.code,{children:"A"}),", ",(0,i.jsx)(n.code,{children:"B"})," are fully resolved."]}),"\n",(0,i.jsxs)(n.p,{children:["Note that since this safety check is performed after type checking rather than during, no error is issued if the function\ncontaining ",(0,i.jsx)(n.code,{children:"checked_transmute"})," is never called."]}),"\n",(0,i.jsx)(n.h1,{id:"stdmemarray_refcount",children:(0,i.jsx)(n.code,{children:"std::mem::array_refcount"})}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-rust",children:"fn array_refcount<T, let N: u32>(array: [T; N]) -> u32 {}\n"})}),"\n",(0,i.jsx)(n.p,{children:"Returns the internal reference count of an array value in unconstrained code."}),"\n",(0,i.jsx)(n.p,{children:"Arrays only have reference count in unconstrained code - using this anywhere\nelse will return zero."}),"\n",(0,i.jsx)(n.p,{children:"This function is mostly intended for debugging compiler optimizations but can also be used\nto find where array copies may be happening in unconstrained code by placing it before array\nmutations."}),"\n",(0,i.jsx)(n.h1,{id:"stdmemvector_refcount",children:(0,i.jsx)(n.code,{children:"std::mem::vector_refcount"})}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-rust",children:"fn vector_refcount<T>(vector: [T]) -> u32 {}\n"})}),"\n",(0,i.jsx)(n.p,{children:"Returns the internal reference count of a vector value in unconstrained code."}),"\n",(0,i.jsx)(n.p,{children:"Vectors only have reference count in unconstrained code - using this anywhere\nelse will return zero."}),"\n",(0,i.jsx)(n.p,{children:"This function is mostly intended for debugging compiler optimizations but can also be used\nto find where vector copies may be happening in unconstrained code by placing it before vector\nmutations."})]})}function u(e={}){const{wrapper:n}={...(0,s.R)(),...e.components};return n?(0,i.jsx)(n,{...e,children:(0,i.jsx)(l,{...e})}):l(e)}},28453(e,n,r){r.d(n,{R:()=>a,x:()=>o});var t=r(96540);const i={},s=t.createContext(i);function a(e){const n=t.useContext(s);return t.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function o(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(i):e.components||i:a(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.