PageSourceSearch

https://noir-lang.org/docs/assets/js/8f58f955.c4f292de.js

js noir-lang.org collected 2026-10-03 22:54:00 UTC 6,542 bytes, 1 lines download raw bytes

1"use strict";(globalThis.webpackChunkdocs=globalThis.webpackChunkdocs||[]).push([[3231],{74374(e,s,t){t.r(s),t.d(s,{assets:()=>c,contentTitle:()=>o,default:()=>p,frontMatter:()=>i,metadata:()=>n,toc:()=>l});const n=JSON.parse('{"id":"language/assert","title":"Assert Function","description":"Learn about the `assert` and `static_assert` functions in Noir, which can be used to explicitly constrain the predicate or comparison expression that follows to be true, and what happens if the expression is false at runtime or compile-time, respectively.","source":"@site/versioned_docs/version-v1.0.0-beta.26/language/assert.md","sourceDirName":"language","slug":"/language/assert","permalink":"/docs/v1.0.0-beta.26/language/assert","draft":false,"unlisted":false,"editUrl":"https://github.com/noir-lang/noir/edit/master/docs/versioned_docs/version-v1.0.0-beta.26/language/assert.md","tags":[],"version":"v1.0.0-beta.26","frontMatter":{"title":"Assert Function","description":"Learn about the `assert` and `static_assert` functions in Noir, which can be used to explicitly constrain the predicate or comparison expression that follows to be true, and what happens if the expression is false at runtime or compile-time, respectively.","keywords":["Noir programming language","assert statement","predicate expression","comparison expression"]},"sidebar":"sidebar","previous":{"title":"Logical Operations","permalink":"/docs/v1.0.0-beta.26/language/ops"},"next":{"title":"Unconstrained Functions","permalink":"/docs/v1.0.0-beta.26/language/unconstrained"}}');var a=t(74848),r=t(28453);const i={title:"Assert Function",description:"Learn about the `assert` and `static_assert` functions in Noir, which can be used to explicitly constrain the predicate or comparison expression that follows to be true, and what happens if the expression is false at runtime or compile-time, respectively.",keywords:["Noir programming language","assert statement","predicate expression","comparison expression"]},o=void 0,c={},l=[];function d(e){const s={a:"a",blockquote:"blockquote",code:"code",p:"p",pre:"pre",...(0,r.R)(),...e.components};return(0,a.jsxs)(a.Fragment,{children:[(0,a.jsxs)(s.p,{children:["Noir includes a special ",(0,a.jsx)(s.code,{children:"assert"})," function which will explicitly constrain the predicate/comparison\nexpression that follows to be true. If this expression is false at runtime, the program will fail to\nbe proven. As of v1.0.0-beta.2, assert statements are expressions and can be used in value contexts."]}),"\n",(0,a.jsx)(s.p,{children:"Example:"}),"\n",(0,a.jsx)(s.pre,{children:(0,a.jsx)(s.code,{className:"language-rust",children:"fn main(x : Field, y : Field) {\n    assert(x == y);\n}\n"})}),"\n",(0,a.jsxs)(s.blockquote,{children:["\n",(0,a.jsxs)(s.p,{children:["Assertions only work for predicate operations, such as ",(0,a.jsx)(s.code,{children:"=="}),". If there's any ambiguity on the operation, the program will fail to compile. For example, it is unclear if ",(0,a.jsx)(s.code,{children:"assert(x + y)"})," would check for ",(0,a.jsx)(s.code,{children:"x + y == 0"})," or simply would return ",(0,a.jsx)(s.code,{children:"true"}),"."]}),"\n"]}),"\n",(0,a.jsx)(s.p,{children:"You can optionally provide a message to be logged when the assertion fails:"}),"\n",(0,a.jsx)(s.pre,{children:(0,a.jsx)(s.code,{className:"language-rust",children:'assert(x == y, "x and y are not equal");\n'})}),"\n",(0,a.jsxs)(s.p,{children:["Aside string literals, the optional message can be a format string or any other type supported as input for Noir's ",(0,a.jsx)(s.a,{href:"/docs/v1.0.0-beta.26/libraries/standard_library/logging",children:"print"})," functions. This feature lets you incorporate runtime variables into your failed assertion logs:"]}),"\n",(0,a.jsx)(s.pre,{children:(0,a.jsx)(s.code,{className:"language-rust",children:'assert(x == y, f"Expected x == y, but got {x} == {y}");\n'})}),"\n",(0,a.jsx)(s.p,{children:"Using a variable as an assertion message directly:"}),"\n",(0,a.jsx)(s.pre,{children:(0,a.jsx)(s.code,{className:"language-rust",children:"struct myStruct {\n  myField: Field\n}\n\nlet s = myStruct { myField: y };\nassert(s.myField == x, s);\n"})}),"\n",(0,a.jsxs)(s.p,{children:["There is also a special ",(0,a.jsx)(s.code,{children:"static_assert"})," function that behaves like ",(0,a.jsx)(s.code,{children:"assert"}),",\nbut that runs at compile-time."]}),"\n",(0,a.jsx)(s.pre,{children:(0,a.jsx)(s.code,{className:"language-rust",children:'fn main(xs: [Field; 3]) {\n    let x = 2;\n    let y = 4;\n    static_assert(x + x == y, "expected 2 + 2 to equal 4");\n\n    // This passes since the length of `xs` is known at compile-time\n    static_assert(xs.len() == 3, "expected the input to have 3 elements");\n}\n'})}),"\n",(0,a.jsxs)(s.p,{children:["Like ",(0,a.jsx)(s.code,{children:"assert"}
1),", the message can be a format string or any other type supported as input for Noir's ",(0,a.jsx)(s.a,{href:"/docs/v1.0.0-beta.26/libraries/standard_library/logging",children:"print"})," functions.\nThis feature lets you incorporate runtime variables into your failed assertion logs:"]}),"\n",(0,a.jsx)(s.pre,{children:(0,a.jsx)(s.code,{className:"language-rust",children:'static_assert(x + x == y, f"Expected 2 + 2 to equal 4 but got: {x} + {x} == {y}");\n'})}),"\n",(0,a.jsx)(s.p,{children:"This function fails when passed a dynamic (run-time) argument:"}),"\n",(0,a.jsx)(s.pre,{children:(0,a.jsx)(s.code,{className:"language-rust",children:'fn main(x : Field, y : Field) {\n    // this fails because `x` is not known at compile-time\n    static_assert(x == 2, "expected x to be known at compile-time and equal to 2");\n\n    let mut example_vector = @[];\n    if y == 4 {\n        example_vector = example_vector.push_back(0);\n    }\n\n    // This fails because the length of `example_vector` is not known at\n    // compile-time\n    let error_message = "expected an empty vector, known at compile-time";\n    static_assert(example_vector.len() == 0, error_message);\n}\n'})})]})}function p(e={}){const{wrapper:s}={...(0,r.R)(),...e.components};return s?(0,a.jsx)(s,{...e,children:(0,a.jsx)(d,{...e})}):d(e)}},28453(e,s,t){t.d(s,{R:()=>i,x:()=>o});var n=t(96540);const a={},r=n.createContext(a);function i(e){const s=n.useContext(r);return n.useMemo(function(){return"function"==typeof e?e(s):{...s,...e}},[s,e])}function o(e){let s;return s=e.disableParentContext?"function"==typeof e.components?e.components(a):e.components||a:i(e.components),n.createElement(r.Provider,{value:s},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.