1"use strict";(globalThis.webpackChunkdocs=globalThis.webpackChunkdocs||[]).push([[510],{71264(e,n,r){r.r(n),r.d(n,{assets:()=>a,contentTitle:()=>o,default:()=>h,frontMatter:()=>l,metadata:()=>s,toc:()=>i});const s=JSON.parse('{"id":"libraries/standard_library/containers/boundedvec","title":"Bounded Vectors","description":"Growable vectors with a fixed maximum length; safer and more efficient than vectors, with rich methods for access and mutation.","source":"@site/versioned_docs/version-v1.0.0-rc.1/libraries/standard_library/containers/boundedvec.md","sourceDirName":"libraries/standard_library/containers","slug":"/libraries/standard_library/containers/boundedvec","permalink":"/docs/v1.0.0-rc.1/libraries/standard_library/containers/boundedvec","draft":false,"unlisted":false,"editUrl":"https://github.com/noir-lang/noir/edit/master/docs/versioned_docs/version-v1.0.0-rc.1/libraries/standard_library/containers/boundedvec.md","tags":[],"version":"v1.0.0-rc.1","frontMatter":{"title":"Bounded Vectors","description":"Growable vectors with a fixed maximum length; safer and more efficient than vectors, with rich methods for access and mutation.","keywords":["noir","vector","bounded vector","vector"]},"sidebar":"sidebar","previous":{"title":"Bn254 Field Library","permalink":"/docs/v1.0.0-rc.1/libraries/standard_library/bn254"},"next":{"title":"UHashMap","permalink":"/docs/v1.0.0-rc.1/libraries/standard_library/containers/uhashmap"}}');var t=r(74848),c=r(28453);const l={title:"Bounded Vectors",description:"Growable vectors with a fixed maximum length; safer and more efficient than vectors, with rich methods for access and mutation.",keywords:["noir","vector","bounded vector","vector"]},o=void 0,a={},i=[{value:"Methods",id:"methods",level:2},{value:"new",id:"new",level:3},{value:"get",id:"get",level:3},{value:"get_unchecked",id:"get_unchecked",level:3},{value:"set",id:"set",level:3},{value:"set_unchecked",id:"set_unchecked",level:3},{value:"push",id:"push",level:3},{value:"pop",id:"pop",level:3},{value:"len",id:"len",level:3},{value:"max_len",id:"max_len",level:3},{value:"set_len",id:"set_len",level:3},{value:"truncate",id:"truncate",level:3},{value:"storage",id:"storage",level:3},{value:"extend_from_array",id:"extend_from_array",level:3},{value:"extend_from_bounded_vec",id:"extend_from_bounded_vec",level:3},{value:"from_array",id:"from_array",level:3},{value:"from_parts",id:"from_parts",level:3},{value:"map",id:"map",level:3},{value:"mapi",id:"mapi",level:3},{value:"for_each",id:"for_each",level:3},{value:"for_eachi",id:"for_eachi",level:3},{value:"any",id:"any",level:3}];function d(e){const n={a:"a",blockquote:"blockquote",code:"code",h2:"h2",h3:"h3",p:"p",pre:"pre",sub:"sub",sup:"sup",...(0,c.R)(),...e.components};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsxs)(n.p,{children:["A ",(0,t.jsx)(n.code,{children:"BoundedVec<T, MaxLen>"})," is a growable storage backed by a normal array under the hood,\nbounded with a maximum possible length."]}),"\n",(0,t.jsx)(n.p,{children:"Since a BoundedVec is backed by a normal array, growing the BoundedVec by pushing an\nadditional element is efficient - the length only needs to be increased by one."}),"\n",(0,t.jsx)(n.p,{children:"Example:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"let mut vector: BoundedVec<Field, 10> = BoundedVec::new();\nfor i in 0..5 {\n vector.push(i);\n}\nassert(vector.len() == 5);\nassert(vector.max_len() == 10);\n"})}),"\n",(0,t.jsx)(n.h2,{id:"methods",children:"Methods"}),"\n",(0,t.jsx)(n.h3,{id:"new",children:"new"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"pub fn new() -> Self\n"})}),"\n",(0,t.jsx)(n.p,{children:"Creates a new, empty vector of length zero."}),"\n",(0,t.jsxs)(n.p,{children:["Since this container is backed by an array internally, it still needs an initial value\nto give each element. To resolve this, each element is zeroed internally. This value\nis guaranteed to be inaccessible unless ",(0,t.jsx)(n.code,{children:"get_unchecked"})," is used."]}),"\n",(0,t.jsx)(n.p,{children:"Example:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"let empty_vector: BoundedVec<Field, 10> = BoundedVec::new();\nassert(empty_vector.len() == 0);\n"})}),"\n",(0,t.jsxs)(n.p,{children:["Note that whenever calling ",(0,t.jsx)(n.code,{children:"new"})," the maximum length of the vector should always be specified\nvia a type signature:"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",metastring:'title="new_example" showLineNumbers ',children:"fn good() -> BoundedVec<Field, 10> {\n // Ok! MaxLen is specified with a type annotation\n let v1: BoundedVec<Field, 3> = BoundedVec::new();\n let v2 = BoundedVec::new();\n\n // Ok! MaxLen is known from the type of `good`'s return value\n v2\n}\n"})}),"\n",(0,t.jsxs)(n.blockquote,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.sup,{children:(0,t.jsx)(n.sub,{children:(0,t.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/test_programs/noir_test_success/bounded_vec/src/main.nr#L6-L15",target:"_blank",rel:"noopener noreferrer",children:"Source code: test_programs/noir_test_success/bounded_vec/src/main.nr#L6-L15"})})})}),"\n"]}),"\n",(0,t.jsxs)(n.p,{children:["This defaulting of ",(0,t.jsx)(n.code,{children:"MaxLen"})," (and numeric generics in general) to zero may change in future noir versions\nbut for now make sure to use type annotations when using bounded vectors. Otherwise, you will receive a constraint failure at runtime when the vec is pushed to."]}),"\n",(0,t.jsx)(n.h3,{id:"get",children:"get"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"pub fn get(self, index: u64) -> T {\n"})}),"\n",(0,t.jsx)(n.p,{children:"Retrieves an element from the vector at the given index, starting from zero."}),"\n",(0,t.jsx)(n.p,{children:"If the given index is equal to or greater than the length of the vector, this\nwill issue a constraint failure."}),"\n",(0,t.jsx)(n.p,{children:"Example:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"fn foo<N>(v: BoundedVec<u32, N>) {\n let first = v.get(0);\n let last = v.get(v.len() - 1);\n assert(first != last);\n}\n"})}),"\n",(0,t.jsx)(n.h3,{id:"get_unchecked",children:"get_unchecked"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"pub fn get_unchecked(self, index: u64) -> T {\n"})}),"\n",(0,t.jsx)(n.p,{children:"Retrieves an element from the vector at the given index, starting from zero, without\nperforming a bounds check."}),"\n",(0,t.jsx)(n.p,{children:"Since this function does not perform a bounds check on length before accessing the element,\nit is unsafe! Use at your own risk!"}),"\n",(0,t.jsx)(n.p,{children:"Example:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",metastring:'title="get_unchecked_example" showLineNumbers ',children:"fn sum_of_first_three<let N: u32>(v: BoundedVec<u32, N>) -> u32 {\n // Always ensure the length is larger than the largest\n // index passed to get_unchecked\n assert(v.len() > 2);\n let first = v.get_unchecked(0);\n let second = v.get_unchecked(1);\n let third = v.get_unchecked(2);\n first + second + third\n}\n"})}),"\n",(0,t.jsxs)(n.blockquote,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.sup,{children:(0,t.jsx)(n.sub,{children:(0,t.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/test_programs/noir_test_success/bounded_vec/src/main.nr#L42-L52",target:"_blank",rel:"noopener noreferrer",children:"Source code: test_programs/noir_test_success/bounded_vec/src/main.nr#L42-L52"})})})}),"\n"]}),"\n",(0,t.jsx)(n.h3,{id:"set",children:"set"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"pub fn set(&mut self: Self, index: u64, value: T) {\n"})}),"\n",(0,t.jsx)(n.p,{children:"Writes an element to the vector at the given index, starting from zero."}),"\n",(0,t.jsx)(n.p,{children:"If the given index is equal to or greater than the length of the vector, this will issue a constraint failure."}),"\n",(0,t.jsx)(n.p,{children:"Example:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"fn foo<N>(v: BoundedVec<u32, N>) {\n let first = v.get(0);\n assert(first != 42);\n v.set(0, 42);\n let new_first = v.get(0);\n assert(new_first == 42);\n}\n"})}),"\n",(0,t.jsx)(n.h3,{id:"set_unchecked",children:"set_unchecked"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"pub fn set_unchecked(&mut self: Self, index: u64, value: T) -> T {\n"})}),"\n",(0,t.jsx)(n.p,{children:"Writes an element to the vector at the given index, starting from zero, without performing a bounds check."}),"\n",(0,t.jsx)(n.p,{children:"Since this function does not perform a bounds check on length before accessing the element, it is unsafe! Use at your own risk!"}),"\n",(0,t.jsx)(n.p,{children:"Example:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",metastring:'title="set_unchecked_example" showLineNumbers ',children:"fn set_unchecked_example() {\n let mut vec: BoundedVec<u32, 5> = BoundedVec::new();\n vec.extend_from_array([1, 2]);\n\n // Here we're safely writing within the valid range of `vec`\n // `vec` now has the value [42, 2]\n vec.set_unchecked(0, 42);\n\n // We can then safely read this value back out of `vec`.\n // Notice that we use the checked version of `get` which would prevent reading unsafe values.\
1n assert_eq(vec.get(0), 42);\n\n // We've now written past the end of `vec`.\n // As this index is still within the maximum potential length of `v`,\n // it won't cause a constraint failure.\n vec.set_unchecked(2, 42);\n println(vec);\n\n // This will write past the end of the maximum potential length of `vec`,\n // it will then trigger a constraint failure.\n vec.set_unchecked(5, 42);\n println(vec);\n}\n"})}),"\n",(0,t.jsxs)(n.blockquote,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.sup,{children:(0,t.jsx)(n.sub,{children:(0,t.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/test_programs/noir_test_success/bounded_vec/src/main.nr#L55-L79",target:"_blank",rel:"noopener noreferrer",children:"Source code: test_programs/noir_test_success/bounded_vec/src/main.nr#L55-L79"})})})}),"\n"]}),"\n",(0,t.jsx)(n.h3,{id:"push",children:"push"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"pub fn push(&mut self, elem: T) {\n"})}),"\n",(0,t.jsx)(n.p,{children:"Pushes an element to the end of the vector. This increases the length\nof the vector by one."}),"\n",(0,t.jsx)(n.p,{children:"Panics if the new length of the vector will be greater than the max length."}),"\n",(0,t.jsx)(n.p,{children:"Example:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",metastring:'title="bounded-vec-push-example" showLineNumbers ',children:'let mut v: BoundedVec<Field, 2> = BoundedVec::new();\n\nv.push(1);\nv.push(2);\n\n// Panics with failed assertion "push out of bounds"\nv.push(3);\n'})}),"\n",(0,t.jsxs)(n.blockquote,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.sup,{children:(0,t.jsx)(n.sub,{children:(0,t.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/test_programs/noir_test_success/bounded_vec/src/main.nr#L83-L91",target:"_blank",rel:"noopener noreferrer",children:"Source code: test_programs/noir_test_success/bounded_vec/src/main.nr#L83-L91"})})})}),"\n"]}),"\n",(0,t.jsx)(n.h3,{id:"pop",children:"pop"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"pub fn pop(&mut self) -> T\n"})}),"\n",(0,t.jsx)(n.p,{children:"Pops the element at the end of the vector. This will decrease the length\nof the vector by one."}),"\n",(0,t.jsx)(n.p,{children:"Panics if the vector is empty."}),"\n",(0,t.jsx)(n.p,{children:"Example:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",metastring:'title="bounded-vec-pop-example" showLineNumbers ',children:"let mut v: BoundedVec<Field, 2> = BoundedVec::new();\nv.push(1);\nv.push(2);\n\nlet two = v.pop();\nlet one = v.pop();\n\nassert(two == 2);\nassert(one == 1);\n// error: cannot pop from an empty vector\n// let _ = v.pop();\n"})}),"\n",(0,t.jsxs)(n.blockquote,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.sup,{children:(0,t.jsx)(n.sub,{children:(0,t.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/test_programs/noir_test_success/bounded_vec/src/main.nr#L96-L108",target:"_blank",rel:"noopener noreferrer",children:"Source code: test_programs/noir_test_success/bounded_vec/src/main.nr#L96-L108"})})})}),"\n"]}),"\n",(0,t.jsx)(n.h3,{id:"len",children:"len"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"pub fn len(self) -> u64 {\n"})}),"\n",(0,t.jsx)(n.p,{children:"Returns the current length of this vector"}),"\n",(0,t.jsx)(n.p,{children:"Example:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",metastring:'title="bounded-vec-len-example" showLineNumbers ',children:"let mut v: BoundedVec<Field, 4> = BoundedVec::new();\nassert(v.len() == 0);\n\nv.push(100);\nassert(v.len() == 1);\n\nv.push(200);\nv.push(300);\nv.push(400);\nassert(v.len() == 4);\n\nlet _ = v.pop();\nlet _ = v.pop();\nassert(v.len() == 2);\n"})}),"\n",(0,t.jsxs)(n.blockquote,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.sup,{children:(0,t.jsx)(n.sub,{children:(0,t.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/test_programs/noir_test_success/bounded_vec/src/main.nr#L113-L128",target:"_blank",rel:"noopener noreferrer",children:"Source code: test_programs/noir_test_success/bounded_vec/src/main.nr#L113-L128"})})})}),"\n"]}),"\n",(0,t.jsx)(n.h3,{id:"max_len",children:"max_len"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"pub fn max_len(_self: BoundedVec<T, MaxLen>) -> u64 {\n"})}),"\n",(0,t.jsxs)(n.p,{children:["Returns the maximum length of this vector. This is always\nequal to the ",(0,t.jsx)(n.code,{children:"MaxLen"})," parameter this vector was initialized with."]}),"\n",(0,t.jsx)(n.p,{children:"Example:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",metastring:'title="bounded-vec-max-len-example" showLineNumbers ',children:"let mut v: BoundedVec<Field, 5> = BoundedVec::new();\n\nassert(v.max_len() == 5);\nv.push(10);\nassert(v.max_len() == 5);\n"})}),"\n",(0,t.jsxs)(n.blockquote,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.sup,{children:(0,t.jsx)(n.sub,{children:(0,t.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/test_programs/noir_test_success/bounded_vec/src/main.nr#L133-L139",target:"_blank",rel:"noopener noreferrer",children:"Source code: test_programs/noir_test_success/bounded_vec/src/main.nr#L133-L139"})})})}),"\n"]}),"\n",(0,t.jsx)(n.h3,{id:"set_len",children:"set_len"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"pub fn set_len(&mut self, len: u32) {\n"})}),"\n",(0,t.jsxs)(n.p,{children:["Sets the length of the BoundedVec manually to any value between ",(0,t.jsx)(n.code,{children:"0"})," and ",(0,t.jsx)(n.code,{children:"MaxLen"}),". Increasing the\nlength is useful to expose elements written past the current length with ",(0,t.jsx)(n.code,{children:"set_unchecked"}),"; decreasing\nit discards trailing elements. This does not zero out the backing storage, so elements beyond the new\nlength remain readable through ",(0,t.jsx)(n.a,{href:"#storage",children:(0,t.jsx)(n.code,{children:"storage"})}),"."]}),"\n",(0,t.jsx)(n.p,{children:"Example:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",metastring:'title="bounded-vec-set-len-example" showLineNumbers ',children:"let mut v: BoundedVec<Field, 10> = BoundedVec::from_array([1, 2, 3, 4, 5]);\n\nv.set_unchecked(5, 6);\nv.set_unchecked(6, 7);\nv.set_unchecked(7, 8);\nassert(v.len() == 5);\n\n// Grow the length to expose the elements written above.\nv.set_len(8);\nassert(v.len() == 8);\nassert(v.get(7) == 8);\n\n// The length can also be reduced.\nv.set_len(2);\nassert(v.len() == 2);\n"})}),"\n",(0,t.jsxs)(n.blockquote,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.sup,{children:(0,t.jsx)(n.sub,{children:(0,t.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/test_programs/noir_test_success/bounded_vec/src/main.nr#L144-L160",target:"_blank",rel:"noopener noreferrer",children:"Source code: test_programs/noir_test_success/bounded_vec/src/main.nr#L144-L160"})})})}),"\n"]}),"\n",(0,t.jsx)(n.h3,{id:"truncate",children:"truncate"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"pub fn truncate(&mut self, len: u32) {\n"})}),"\n",(0,t.jsxs)(n.p,{children:["Shortens the BoundedVec to ",(0,t.jsx)(n.code,{children:"len"}),", keeping the first ",(0,t.jsx)(n.code,{children:"len"})," elements. If ",(0,t.jsx)(n.code,{children:"len"})," is greater than or\nequal to the current length, the vector is left unchanged. Regardless of the current length, after\nthis call the vector is guaranteed to hold no more than ",(0,t.jsx)(n.code,{children:"len"})," elements."]}),"\n",(0,t.jsx)(n.p,{children:"Example:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",metastring:'title="bounded-vec-truncate-example" showLineNumbers ',children:"let mut v: BoundedVec<Field, 10> = BoundedVec::from_array([1, 2, 3, 4, 5]);\n\nv.truncate(3);\nassert(v.len() == 3);\n\n// Truncating to a length greater than the current length is a no-op.\nv.truncate(8);\nassert(v.len() == 3);\n"})}),"\n",(0,t.jsxs)(n.blockquote,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.sup,{children:(0,t.jsx)(n.sub,{children:(0,t.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/test_programs/noir_test_success/bounded_vec/src/main.nr#L165-L174",target:"_blank",rel:"noopener noreferrer",children:"Source code: test_programs/noir_test_success/bounded_vec/src/main.nr#L165-L174"})})})}),"\n"]}),"\n",(0,t.jsx)(n.h3,{id:"storage",children:"storage"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"pub fn storage(self) -> [T;
1 MaxLen] {\n"})}),"\n",(0,t.jsx)(n.p,{children:"Returns the internal array within this vector.\nSince arrays in Noir are immutable, mutating the returned storage array will not mutate\nthe storage held internally by this vector."}),"\n",(0,t.jsxs)(n.p,{children:["Note that the values of uninitialized elements may differ for two ",(0,t.jsx)(n.code,{children:"BoundedVecs"})," considered equal by ",(0,t.jsx)(n.code,{children:"impl Eq for BoundedVec"}),"!\nThis can lead to underconstrained bugs if a ",(0,t.jsx)(n.code,{children:"BoundedVec"})," or its storage is returned from unconstrained code into constrained\ncode and the values of the elements past the ",(0,t.jsx)(n.code,{children:"BoundedVec"}),"'s length are used in the circuit logic at all."]}),"\n",(0,t.jsx)(n.p,{children:"Example:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",metastring:'title="bounded-vec-storage-example" showLineNumbers ',children:"let mut v: BoundedVec<Field, 5> = BoundedVec::new();\n\nassert(v.storage() == [0, 0, 0, 0, 0]);\n\nv.push(57);\nassert(v.storage() == [57, 0, 0, 0, 0]);\n"})}),"\n",(0,t.jsxs)(n.blockquote,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.sup,{children:(0,t.jsx)(n.sub,{children:(0,t.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/test_programs/noir_test_success/bounded_vec/src/main.nr#L179-L186",target:"_blank",rel:"noopener noreferrer",children:"Source code: test_programs/noir_test_success/bounded_vec/src/main.nr#L179-L186"})})})}),"\n"]}),"\n",(0,t.jsx)(n.h3,{id:"extend_from_array",children:"extend_from_array"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"pub fn extend_from_array<Len>(&mut self, array: [T; Len])\n"})}),"\n",(0,t.jsx)(n.p,{children:"Pushes each element from the given array to this vector."}),"\n",(0,t.jsx)(n.p,{children:"Panics if pushing each element would cause the length of this vector\nto exceed the maximum length."}),"\n",(0,t.jsx)(n.p,{children:"Example:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",metastring:'title="bounded-vec-extend-from-array-example" showLineNumbers ',children:"let mut vec: BoundedVec<Field, 3> = BoundedVec::new();\nvec.extend_from_array([2, 4]);\n\nassert(vec.len() == 2);\nassert(vec.get(0) == 2);\nassert(vec.get(1) == 4);\n"})}),"\n",(0,t.jsxs)(n.blockquote,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.sup,{children:(0,t.jsx)(n.sub,{children:(0,t.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/test_programs/noir_test_success/bounded_vec/src/main.nr#L191-L198",target:"_blank",rel:"noopener noreferrer",children:"Source code: test_programs/noir_test_success/bounded_vec/src/main.nr#L191-L198"})})})}),"\n"]}),"\n",(0,t.jsx)(n.h3,{id:"extend_from_bounded_vec",children:"extend_from_bounded_vec"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"pub fn extend_from_bounded_vec<Len>(&mut self, vec: BoundedVec<T, Len>)\n"})}),"\n",(0,t.jsx)(n.p,{children:"Pushes each element from the other vector to this vector. The length of\nthe other vector is left unchanged."}),"\n",(0,t.jsx)(n.p,{children:"Panics if pushing each element would cause the length of this vector\nto exceed the maximum length."}),"\n",(0,t.jsx)(n.p,{children:"Example:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",metastring:'title="bounded-vec-extend-from-bounded-vec-example" showLineNumbers ',children:"let mut v1: BoundedVec<Field, 5> = BoundedVec::new();\nlet mut v2: BoundedVec<Field, 7> = BoundedVec::new();\n\nv2.extend_from_array([1, 2, 3]);\nv1.extend_from_bounded_vec(v2);\n\nassert(v1.storage() == [1, 2, 3, 0, 0]);\nassert(v2.storage() == [1, 2, 3, 0, 0, 0, 0]);\n"})}),"\n",(0,t.jsxs)(n.blockquote,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.sup,{children:(0,t.jsx)(n.sub,{children:(0,t.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/test_programs/noir_test_success/bounded_vec/src/main.nr#L203-L212",target:"_blank",rel:"noopener noreferrer",children:"Source code: test_programs/noir_test_success/bounded_vec/src/main.nr#L203-L212"})})})}),"\n"]}),"\n",(0,t.jsx)(n.h3,{id:"from_array",children:"from_array"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"pub fn from_array<Len>(array: [T; Len]) -> Self\n"})}),"\n",(0,t.jsx)(n.p,{children:"Creates a new vector, populating it with values derived from an array input.\nThe maximum length of the vector is determined based on the type signature."}),"\n",(0,t.jsx)(n.p,{children:"Example:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"let bounded_vec: BoundedVec<Field, 10> = BoundedVec::from_array([1, 2, 3])\n"})}),"\n",(0,t.jsx)(n.h3,{id:"from_parts",children:"from_parts"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"pub fn from_parts(mut array: [T; MaxLen], len: u32) -> Self\n"})}),"\n",(0,t.jsx)(n.p,{children:"Creates a new BoundedVec from the given array and length.\nThe given length must be less than or equal to the length of the array."}),"\n",(0,t.jsx)(n.p,{children:"Example:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",metastring:'title="from-parts" showLineNumbers ',children:"let vec: BoundedVec<u32, 4> = BoundedVec::from_parts([1, 2, 3, 0], 3);\nassert_eq(vec.len(), 3);\n\n// Any elements past the given length are ignored, so these\n// two BoundedVecs
1will be completely equal\nlet vec1: BoundedVec<u32, 4> = BoundedVec::from_parts([1, 2, 3, 1], 3);\nlet vec2: BoundedVec<u32, 4> = BoundedVec::from_parts([1, 2, 3, 2], 3);\nassert_eq(vec1, vec2);\n"})}),"\n",(0,t.jsxs)(n.blockquote,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.sup,{children:(0,t.jsx)(n.sub,{children:(0,t.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/collections/bounded_vec.nr#L1285-L1294",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/collections/bounded_vec.nr#L1285-L1294"})})})}),"\n"]}),"\n",(0,t.jsx)(n.h3,{id:"map",children:"map"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"pub fn map<U, Env>(self, f: fn[Env](T) -> U) -> BoundedVec<U, MaxLen>\n"})}),"\n",(0,t.jsx)(n.p,{children:"Creates a new vector of equal size by calling a closure on each element in this vector."}),"\n",(0,t.jsx)(n.p,{children:"Example:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",metastring:'title="bounded-vec-map-example" showLineNumbers ',children:"let vec: BoundedVec<u32, 4> = BoundedVec::from_array([1, 2, 3, 4]);\nlet result = vec.map(|value| value * 2);\n"})}),"\n",(0,t.jsxs)(n.blockquote,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.sup,{children:(0,t.jsx)(n.sub,{children:(0,t.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/collections/bounded_vec.nr#L915-L918",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/collections/bounded_vec.nr#L915-L918"})})})}),"\n"]}),"\n",(0,t.jsx)(n.h3,{id:"mapi",children:"mapi"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"pub fn mapi<U, Env>(self, f: fn[Env](u32, T) -> U) -> BoundedVec<U, MaxLen>\n"})}),"\n",(0,t.jsx)(n.p,{children:"Creates a new vector of equal size by calling a closure on each element in this\nvector, along with its index in the vector."}),"\n",(0,t.jsx)(n.p,{children:"Example:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",metastring:'title="bounded-vec-mapi-example" showLineNumbers ',children:"let vec: BoundedVec<u32, 4> = BoundedVec::from_array([1, 2, 3, 4]);\nlet result = vec.mapi(|i, value| i + value * 2);\n"})}),"\n",(0,t.jsxs)(n.blockquote,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.sup,{children:(0,t.jsx)(n.sub,{children:(0,t.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/collections/bounded_vec.nr#L978-L981",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/collections/bounded_vec.nr#L978-L981"})})})}),"\n"]}),"\n",(0,t.jsx)(n.h3,{id:"for_each",children:"for_each"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"pub fn for_each<Env>(self, f: fn[Env](T) -> ())\n"})}),"\n",(0,t.jsx)(n.p,{children:"Calls a closure on each element in this vector."}),"\n",(0,t.jsx)(n.p,{children:"Example:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",metastring:'title="bounded-vec-for-each-example" showLineNumbers ',children:"let vec: BoundedVec<u32, 3> = BoundedVec::from_array([1, 2, 3]);\nvec.for_each(|value| { *acc_ref += value; });\n"})}),"\n",(0,t.jsxs)(n.blockquote,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.sup,{children:(0,t.jsx)(n.sub,{children:(0,t.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/collections/bounded_vec.nr#L1036-L1039",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/collections/bounded_vec.nr#L1036-L1039"})})})}),"\n"]}),"\n",(0,t.jsx)(n.h3,{id:"for_eachi",children:"for_eachi"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"pub fn for_eachi<Env>(self, f: fn[Env](u32, T) -> ())\n"})}),"\n",(0,t.jsx)(n.p,{children:"Calls a closure on each element in this vector, along with its index in the vector."}),"\n",(0,t.jsx)(n.p,{children:"Example:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",metastring:'title="bounded-vec-for-eachi-example" showLineNumbers ',children:"let vec: BoundedVec<u32, 3> = BoundedVec::from_array([1, 2, 3]);\nvec.for_eachi(|i, value| { *acc_ref += i * value; });\n"})}),"\n",(0,t.jsxs)(n.blockquote,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.sup,{children:(0,t.jsx)(n.sub,{children:(0,t.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/collections/bounded_vec.nr#L1110-L1113",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/collections/bounded_vec.nr#L1110-L1113"})})})}),"\n"]}),"\n",(0,t.jsx)(n.h3,{id:"any",children:"any"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",children:"pub fn any<Env>(self, predicate: fn[Env](T) -> bool) -> bool\n"})}),"\n",(0,t.jsx)(n.p,{children:"Returns true if the given predicate returns true for any element\nin this vector."}),"\n",(0,t.jsx)(n.p,{children:"Example:"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-rust",metastring:'title="bounded-vec-any-example" showLineNumbers ',children:"let mut v: BoundedVec<u32, 3> = BoundedVec::new();\nv.extend_from_array([2, 4, 6]);\n\nlet all_even = !v.any(|elem: u32| elem % 2 != 0);\nassert(all_even);\n"})}),"\n",(0,t.jsxs)(n.blockquote,{children:["\n",(0,t.jsx)(n.p,{children:(0,t.jsx)(n.sup,{children:(0,t.jsx)(n.sub,{children:(0,t.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/test_programs/noir_test_success/bounded_vec/src/main.nr#L279-L285",target:"_blank",rel:"noopener noreferrer",children:"Source code: test_programs/noir_test_success/bounded_vec/src/main.nr#L279-L285"})})})}),"\n"]})]})}function h(e={}){const{wrapper:n}={...(0,c.R)(),...e.components};return n?(0,t.jsx)(n,{...e,children:(0,t.jsx)(d,{...e})}):d(e)}},28453(e,n,r){r.d(n,{R:()=>l,x:()=>o});var s=r(96540);const t={}
1,c=s.createContext(t);function l(e){const n=s.useContext(c);return s.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(t):e.components||t:l(e.components),s.createElement(c.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.