| ... | @@ -31,221 +31,10 @@ | ... | @@ -31,221 +31,10 @@ |
| 31 | </head> | 31 | </head> |
| 32 | <body> | 32 | <body> |
| 33 | <div id="nav"> | 33 | <div id="nav"> |
| 34 | <ul> | 34 | {#nav#} |
| 35 | <li><a href="#introduction">Introduction</a></li> | | |
| 36 | <li><a href="#hello-world">Hello World</a></li> | | |
| 37 | <li><a href="#source-encoding">Source Encoding</a></li> | | |
| 38 | <li><a href="#values">Values</a></li> | | |
| 39 | <ul> | | |
| 40 | <li><a href="#primitive-types">Primitive Types</a></li> | | |
| 41 | <li><a href="#primitive-values">Primitive Values</a></li> | | |
| 42 | <li><a href="#string-literals">String Literals</a> | | |
| 43 | <ul> | | |
| 44 | <li><a href="#string-literal-escapes">Escape Sequences</a></li> | | |
| 45 | <li><a href="#multiline-string-literals">Multiline String Literals</a></li> | | |
| 46 | </ul> | | |
| 47 | </li> | | |
| 48 | <li><a href="#values-assignment">Assignment</a></li> | | |
| 49 | </ul> | | |
| 50 | </li> | | |
| 51 | <li><a href="#integers">Integers</a> | | |
| 52 | <ul> | | |
| 53 | <li><a href="#integer-literals">Integer Literals</a></li> | | |
| 54 | <li><a href="#runtime-integer-values">Runtime Integer Values</a></li> | | |
| 55 | </ul> | | |
| 56 | </li> | | |
| 57 | <li><a href="#floats">Floats</a> | | |
| 58 | <ul> | | |
| 59 | <li><a href="#float-literals">Float Literals</a></li> | | |
| 60 | <li><a href="#float-operations">Floating Point Operations</a></li> | | |
| 61 | </ul> | | |
| 62 | </li> | | |
| 63 | <li><a href="#operators">Operators</a> | | |
| 64 | <ul> | | |
| 65 | <li><a href="#operators-table">Table of Operators</a></li> | | |
| 66 | <li><a href="#operators-precedence">Precedence</a></li> | | |
| 67 | </ul> | | |
| 68 | </li> | | |
| 69 | <li><a href="#arrays">Arrays</a></li> | | |
| 70 | <li><a href="#pointers">Pointers</a> | | |
| 71 | <ul> | | |
| 72 | <li><a href="#alignment">Alignment</a></li> | | |
| 73 | <li><a href="#type-based-alias-analysis">Type Based Alias Analysis</a></li> | | |
| 74 | </ul> | | |
| 75 | </li> | | |
| 76 | <li><a href="#slices">Slices</a></li> | | |
| 77 | <li><a href="#struct">struct</a></li> | | |
| 78 | <li><a href="#enum">enum</a></li> | | |
| 79 | <li><a href="#union">union</a></li> | | |
| 80 | <li><a href="#switch">switch</a></li> | | |
| 81 | <li><a href="#while">while</a></li> | | |
| 82 | <li><a href="#for">for</a></li> | | |
| 83 | <li><a href="#if">if</a></li> | | |
| 84 | <li><a href="#goto">goto</a></li> | | |
| 85 | <li><a href="#defer">defer</a></li> | | |
| 86 | <li><a href="#unreachable">unreachable</a> | | |
| 87 | <ul> | | |
| 88 | <li><a href="#unreachable-basics">Basics</a></li> | | |
| 89 | <li><a href="#unreachable-comptime">At Compile-Time</a></li> | | |
| 90 | </ul> | | |
| 91 | </li> | | |
| 92 | <li><a href="#noreturn">noreturn</a></li> | | |
| 93 | <li><a href="#functions">Functions</a> | | |
| 94 | <ul> | | |
| 95 | <li><a href="#functions-by-val-params">Pass-by-val Parameters</a> | | |
| 96 | </ul> | | |
| 97 | </li> | | |
| 98 | <li><a href="#errors">Errors</a></li> | | |
| 99 | <li><a href="#nullables">Nullables</a></li> | | |
| 100 | <li><a href="#casting">Casting</a></li> | | |
| 101 | <li><a href="#void">void</a></li> | | |
| 102 | <li><a href="#this">this</a></li> | | |
| 103 | <li><a href="#comptime">comptime</a> | | |
| 104 | <ul> | | |
| 105 | <li><a href="#introducing-compile-time-concept">Introducing the Compile-Time Concept</a></li> | | |
| 106 | <ul> | | |
| 107 | <li><a href="#compile-time-parameters">Compile-time parameters</a></li> | | |
| 108 | <li><a href="#compile-time-variables">Compile-time variables</a></li> | | |
| 109 | <li><a href="#compile-time-expressions">Compile-time expressions</a></li> | | |
| 110 | </ul> | | |
| 111 | <li><a href="#generic-data-structures">Generic Data Structures</a></li> | | |
| 112 | <li><a href="#case-study-printf">Case Study: printf in Zig</a></li> | | |
| 113 | </ul> | | |
| 114 | </li> | | |
| 115 | <li><a href="#inline">inline</a></li> | | |
| 116 | <li><a href="#assembly">assembly</a></li> | | |
| 117 | <li><a href="#atomics">Atomics</a></li> | | |
| 118 | <li><a href="#builtin-functions">Builtin Functions</a> | | |
| 119 | <ul> | | |
| 120 | <li><a href="#builtin-addWithOverflow">@addWithOverflow</a></li> | | |
| 121 | <li><a href="#builtin-alignCast">@alignCast</a></li> | | |
| 122 | <li><a href="#builtin-alignOf">@alignOf</a></li> | | |
| 123 | <li><a href="#builtin-ArgType">@ArgType</a></li> | | |
| 124 | <li><a href="#builtin-bitCast">@bitCast</a></li> | | |
| 125 | <li><a href="#builtin-breakpoint">@breakpoint</a></li> | | |
| 126 | <li><a href="#builtin-cDefine">@cDefine</a></li> | | |
| 127 | <li><a href="#builtin-cImport">@cImport</a></li> | | |
| 128 | <li><a href="#builtin-cInclude">@cInclude</a></li> | | |
| 129 | <li><a href="#builtin-cUndef">@cUndef</a></li> | | |
| 130 | <li><a href="#builtin-canImplicitCast">@canImplicitCast</a></li> | | |
| 131 | <li><a href="#builtin-clz">@clz</a></li> | | |
| 132 | <li><a href="#builtin-cmpxchg">@cmpxchg</a></li> | | |
| 133 | <li><a href="#builtin-compileError">@compileError</a></li> | | |
| 134 | <li><a href="#builtin-compileLog">@compileLog</a></li> | | |
| 135 | <li><a href="#builtin-ctz">@ctz</a></li> | | |
| 136 | <li><a href="#builtin-divExact">@divExact</a></li> | | |
| 137 | <li><a href="#builtin-divFloor">@divFloor</a></li> | | |
| 138 | <li><a href="#builtin-divTrunc">@divTrunc</a></li> | | |
| 139 | <li><a href="#builtin-embedFile">@embedFile</a></li> | | |
| 140 | <li><a href="#builtin-export">@export</a></li> | | |
| 141 | <li><a href="#builtin-tagName">@tagName</a></li> | | |
| 142 | <li><a href="#builtin-TagType">@TagType</a></li> | | |
| 143 | <li><a href="#builtin-EnumTagType">@EnumTagType</a></li> | | |
| 144 | <li><a href="#builtin-errorName">@errorName</a></li> | | |
| 145 | <li><a href="#builtin-errorReturnTrace">@errorReturnTrace</a></li> | | |
| 146 | <li><a href="#builtin-fence">@fence</a></li> | | |
| 147 | <li><a href="#builtin-fieldParentPtr">@fieldParentPtr</a></li> | | |
| 148 | <li><a href="#builtin-frameAddress">@frameAddress</a></li> | | |
| 149 | <li><a href="#builtin-import">@import</a></li> | | |
| 150 | <li><a href="#builtin-inlineCall">@inlineCall</a></li> | | |
| 151 | <li><a href="#builtin-intToPtr">@intToPtr</a></li> | | |
| 152 | <li><a href="#builtin-IntType">@IntType</a></li> | | |
| 153 | <li><a href="#builtin-maxValue">@maxValue</a></li> | | |
| 154 | <li><a href="#builtin-memberCount">@memberCount</a></li> | | |
| 155 | <li><a href="#builtin-memberName">@memberName</a></li> | | |
| 156 | <li><a href="#builtin-memberType">@memberType</a></li> | | |
| 157 | <li><a href="#builtin-memcpy">@memcpy</a></li> | | |
| 158 | <li><a href="#builtin-memset">@memset</a></li> | | |
| 159 | <li><a href="#builtin-minValue">@minValue</a></li> | | |
| 160 | <li><a href="#builtin-mod">@mod</a></li> | | |
| 161 | <li><a href="#builtin-mulWithOverflow">@mulWithOverflow</a></li> | | |
| 162 | <li><a href="#builtin-noInlineCall">@noInlineCall</a></li> | | |
| 163 | <li><a href="#builtin-offsetOf">@offsetOf</a></li> | | |
| 164 | <li><a href="#builtin-OpaqueType">@OpaqueType</a></li> | | |
| 165 | <li><a href="#builtin-panic">@panic</a></li> | | |
| 166 | <li><a href="#builtin-ptrCast">@ptrCast</a></li> | | |
| 167 | <li><a href="#builtin-ptrToInt">@ptrToInt</a></li> | | |
| 168 | <li><a href="#builtin-rem">@rem</a></li> | | |
| 169 | <li><a href="#builtin-returnAddress">@returnAddress</a></li> | | |
| 170 | <li><a href="#builtin-setDebugSafety">@setDebugSafety</a></li> | | |
| 171 | <li><a href="#builtin-setEvalBranchQuota">@setEvalBranchQuota</a></li> | | |
| 172 | <li><a href="#builtin-setFloatMode">@setFloatMode</a></li> | | |
| 173 | <li><a href="#builtin-setGlobalLinkage">@setGlobalLinkage</a></li> | | |
| 174 | <li><a href="#builtin-setGlobalSection">@setGlobalSection</a></li> | | |
| 175 | <li><a href="#builtin-shlExact">@shlExact</a></li> | | |
| 176 | <li><a href="#builtin-shlWithOverflow">@shlWithOverflow</a></li> | | |
| 177 | <li><a href="#builtin-shrExact">@shrExact</a></li> | | |
| 178 | <li><a href="#builtin-sizeOf">@sizeOf</a></li> | | |
| 179 | <li><a href="#builtin-subWithOverflow">@subWithOverflow</a></li> | | |
| 180 | <li><a href="#builtin-truncate">@truncate</a></li> | | |
| 181 | <li><a href="#builtin-typeId">@typeId</a></li> | | |
| 182 | <li><a href="#builtin-typeName">@typeName</a></li> | | |
| 183 | <li><a href="#builtin-typeOf">@typeOf</a></li> | | |
| 184 | </ul> | | |
| 185 | </li> | | |
| 186 | <li><a href="#build-mode">Build Mode</a> | | |
| 187 | <ul> | | |
| 188 | <li><a href="#build-mode-debug">Debug</a></li> | | |
| 189 | <li><a href="#build-mode-release-fast">ReleaseFast</a></li> | | |
| 190 | <li><a href="#build-mode-release-safe">ReleaseSafe</a></li> | | |
| 191 | </ul> | | |
| 192 | </li> | | |
| 193 | <li><a href="#undefined-behavior">Undefined Behavior</a> | | |
| 194 | <ul> | | |
| 195 | <li><a href="#undef-unreachable">Reaching Unreachable Code</a></li> | | |
| 196 | <li><a href="#undef-index-out-of-bounds">Index out of Bounds</a></li> | | |
| 197 | <li><a href="#undef-cast-negative-unsigned">Cast Negative Number to Unsigned Integer</a></li> | | |
| 198 | <li><a href="#undef-cast-truncates-data">Cast Truncates Data</a></li> | | |
| 199 | <li><a href="#undef-integer-overflow">Integer Overflow</a> | | |
| 200 | <ul> | | |
| 201 | <li><a href="#undef-int-overflow-default">Default Operations</a></li> | | |
| 202 | <li><a href="#undef-int-overflow-std">Standard Library Math Functions</a></li> | | |
| 203 | <li><a href="#undef-int-overflow-builtin">Builtin Overflow Functions</a></li> | | |
| 204 | <li><a href="#undef-int-overflow-wrap">Wrapping Operations</a></li> | | |
| 205 | | | |
| 206 | </ul> | | |
| 207 | </li> | | |
| 208 | <li><a href="#undef-shl-overflow">Exact Left Shift Overflow</a></li> | | |
| 209 | <li><a href="#undef-shr-overflow">Exact Right Shift Overflow</a></li> | | |
| 210 | <li><a href="#undef-division-by-zero">Division by Zero</a></li> | | |
| 211 | <li><a href="#undef-remainder-division-by-zero">Remainder Division by Zero</a></li> | | |
| 212 | <li><a href="#undef-exact-division-remainder">Exact Division Remainder</a></li> | | |
| 213 | <li><a href="#undef-slice-widen-remainder">Slice Widen Remainder</a></li> | | |
| 214 | <li><a href="#undef-attempt-unwrap-null">Attempt to Unwrap Null</a></li> | | |
| 215 | <li><a href="#undef-attempt-unwrap-error">Attempt to Unwrap Error</a></li> | | |
| 216 | <li><a href="#undef-invalid-error-code">Invalid Error Code</a></li> | | |
| 217 | <li><a href="#undef-invalid-enum-cast">Invalid Enum Cast</a></li> | | |
| 218 | <li><a href="#undef-incorrect-pointer-alignment">Incorrect Pointer Alignment</a></li> | | |
| 219 | <li><a href="#undef-bad-union-field">Wrong Union Field Access</a></li> | | |
| 220 | </ul> | | |
| 221 | </li> | | |
| 222 | <li><a href="#memory">Memory</a></li> | | |
| 223 | <li><a href="#compile-variables">Compile Variables</a></li> | | |
| 224 | <li><a href="#root-source-file">Root Source File</a></li> | | |
| 225 | <li><a href="#zig-test">Zig Test</a></li> | | |
| 226 | <li><a href="#zig-build-system">Zig Build System</a></li> | | |
| 227 | <li><a href="#c">C</a> | | |
| 228 | <ul> | | |
| 229 | <li><a href="#c-type-primitives">C Type Primitives</a></li> | | |
| 230 | <li><a href="#c-string-literals">C String Literals</a></li> | | |
| 231 | <li><a href="#c-import">Import from C Header File</a></li> | | |
| 232 | <li><a href="#mixing-object-files">Mixing Object Files</a></li> | | |
| 233 | </ul> | | |
| 234 | </li> | | |
| 235 | <li><a href="#targets">Targets</a></li> | | |
| 236 | <li><a href="#style-guide">Style Guide</a> | | |
| 237 | <ul> | | |
| 238 | <li><a href="#style-guide-whitespace">Whitespace</a></li> | | |
| 239 | <li><a href="#style-guide-names">Names</a></li> | | |
| 240 | <li><a href="#style-guide-examples">Examples</a></li> | | |
| 241 | </ul> | | |
| 242 | </li> | | |
| 243 | <li><a href="#grammar">Grammar</a></li> | | |
| 244 | <li><a href="#zen">Zen</a></li> | | |
| 245 | </ul> | | |
| 246 | </div> | 35 | </div> |
| 247 | <div id="contents"> | 36 | <div id="contents"> |
| 248 | <h1 id="introduction">Zig Documentation</h1> | 37 | {#header_open|Introduction#} |
| 249 | <p> | 38 | <p> |
| 250 | Zig is an open-source programming language designed for <strong>robustness</strong>, | 39 | Zig is an open-source programming language designed for <strong>robustness</strong>, |
| 251 | <strong>optimality</strong>, and <strong>clarity</strong>. | 40 | <strong>optimality</strong>, and <strong>clarity</strong>. |
| ... | @@ -264,7 +53,8 @@ | ... | @@ -264,7 +53,8 @@ |
| 264 | If you search for something specific in this documentation and do not find it, | 53 | If you search for something specific in this documentation and do not find it, |
| 265 | please <a href="https://github.com/zig-lang/www.ziglang.org/issues/new?title=I%20searched%20for%20___%20in%20the%20docs%20and%20didn%27t%20find%20it">file an issue</a> or <a href="https://webchat.freenode.net/?channels=%23zig">say something on IRC</a>. | 54 | please <a href="https://github.com/zig-lang/www.ziglang.org/issues/new?title=I%20searched%20for%20___%20in%20the%20docs%20and%20didn%27t%20find%20it">file an issue</a> or <a href="https://webchat.freenode.net/?channels=%23zig">say something on IRC</a>. |
| 266 | </p> | 55 | </p> |
| 267 | <h2 id="hello-world">Hello World</h2> | 56 | {#header_close#} |
| | 57 | {#header_open|Hello World#} |
| 268 | <pre><code class="zig">const std = @import("std"); | 58 | <pre><code class="zig">const std = @import("std"); |
| 269 | | 59 | |
| 270 | pub fn main() -&gt; %void { | 60 | pub fn main() -&gt; %void { |
| ... | @@ -294,7 +84,8 @@ pub fn main() -&gt; %void { | ... | @@ -294,7 +84,8 @@ pub fn main() -&gt; %void { |
| 294 | <li><a href="#errors">Errors</a></li> | 84 | <li><a href="#errors">Errors</a></li> |
| 295 | <li><a href="#root-source-file">Root Source File</a></li> | 85 | <li><a href="#root-source-file">Root Source File</a></li> |
| 296 | </ul> | 86 | </ul> |
| 297 | <h2 id="source-encoding">Source Encoding</h2> | 87 | {#header_close#} |
| | 88 | {#header_open|Source Encoding#} |
| 298 | <p>Zig source code is encoded in UTF-8. An invalid UTF-8 byte sequence results in a compile error.</p> | 89 | <p>Zig source code is encoded in UTF-8. An invalid UTF-8 byte sequence results in a compile error.</p> |
| 299 | <p>Throughout all zig source code (including in comments), some codepoints are never allowed:</p> | 90 | <p>Throughout all zig source code (including in comments), some codepoints are never allowed:</p> |
| 300 | <ul> | 91 | <ul> |
| ... | @@ -303,7 +94,8 @@ pub fn main() -&gt; %void { | ... | @@ -303,7 +94,8 @@ pub fn main() -&gt; %void { |
| 303 | </ul> | 94 | </ul> |
| 304 | <p>The codepoint U+000a (LF) (which is encoded as the single-byte value 0x0a) is the line terminator character. This character always terminates a line of zig source code (except possbly the last line of the file).</p> | 95 | <p>The codepoint U+000a (LF) (which is encoded as the single-byte value 0x0a) is the line terminator character. This character always terminates a line of zig source code (except possbly the last line of the file).</p> |
| 305 | <p>For some discussion on the rationale behind these design decisions, see <a href="https://github.com/zig-lang/zig/issues/663">issue #663</a></p> | 96 | <p>For some discussion on the rationale behind these design decisions, see <a href="https://github.com/zig-lang/zig/issues/663">issue #663</a></p> |
| 306 | <h2 id="values">Values</h2> | 97 | {#header_close#} |
| | 98 | {#header_open|Values#} |
| 307 | <pre><code class="zig">const warn = @import("std").debug.warn; | 99 | <pre><code class="zig">const warn = @import("std").debug.warn; |
| 308 | const os = @import("std").os; | 100 | const os = @import("std").os; |
| 309 | const assert = @import("std").debug.assert; | 101 | const assert = @import("std").debug.assert; |
| ... | @@ -373,7 +165,7 @@ value: error.ArgNotFound | ... | @@ -373,7 +165,7 @@ value: error.ArgNotFound |
| 373 | error union 2 | 165 | error union 2 |
| 374 | type: %i32 | 166 | type: %i32 |
| 375 | value: 1234</code></pre> | 167 | value: 1234</code></pre> |
| 376 | <h3 id="primitive-types">Primitive Types</h2> | 168 | {#header_open|Primitive Types#} |
| 377 | <table> | 169 | <table> |
| 378 | <tr> | 170 | <tr> |
| 379 | <th> | 171 | <th> |
| ... | @@ -606,7 +398,8 @@ value: 1234</code></pre> | ... | @@ -606,7 +398,8 @@ value: 1234</code></pre> |
| 606 | <li><a href="#void">void</a></li> | 398 | <li><a href="#void">void</a></li> |
| 607 | <li><a href="#errors">Errors</a></li> | 399 | <li><a href="#errors">Errors</a></li> |
| 608 | </ul> | 400 | </ul> |
| 609 | <h3 id="primitive-values">Primitive Values</h3> | 401 | {#header_close#} |
| | 402 | {#header_open|Primitive Values#} |
| 610 | <table> | 403 | <table> |
| 611 | <tr> | 404 | <tr> |
| 612 | <th> | 405 | <th> |
| ... | @@ -638,7 +431,8 @@ value: 1234</code></pre> | ... | @@ -638,7 +431,8 @@ value: 1234</code></pre> |
| 638 | <li><a href="#nullables">Nullables</a></li> | 431 | <li><a href="#nullables">Nullables</a></li> |
| 639 | <li><a href="#this">this</a></li> | 432 | <li><a href="#this">this</a></li> |
| 640 | </ul> | 433 | </ul> |
| 641 | <h3 id="string-literals">String Literals</h3> | 434 | {#header_close#} |
| | 435 | {#header_open|String Literals#} |
| 642 | <pre><code class="zig">const assert = @import("std").debug.assert; | 436 | <pre><code class="zig">const assert = @import("std").debug.assert; |
| 643 | const mem = @import("std").mem; | 437 | const mem = @import("std").mem; |
| 644 | | 438 | |
| ... | @@ -663,7 +457,7 @@ Test 1/1 string literals...OK</code></pre> | ... | @@ -663,7 +457,7 @@ Test 1/1 string literals...OK</code></pre> |
| 663 | <li><a href="#arrays">Arrays</a></li> | 457 | <li><a href="#arrays">Arrays</a></li> |
| 664 | <li><a href="#zig-test">Zig Test</a></li> | 458 | <li><a href="#zig-test">Zig Test</a></li> |
| 665 | </ul> | 459 | </ul> |
| 666 | <h4 id="string-literal-escapes">Escape Sequences</h4> | 460 | {#header_open|Escape Sequences#} |
| 667 | <table> | 461 | <table> |
| 668 | <tr> | 462 | <tr> |
| 669 | <th> | 463 | <th> |
| ... | @@ -711,7 +505,8 @@ Test 1/1 string literals...OK</code></pre> | ... | @@ -711,7 +505,8 @@ Test 1/1 string literals...OK</code></pre> |
| 711 | </tr> | 505 | </tr> |
| 712 | </table> | 506 | </table> |
| 713 | <p>Note that the maximum valid Unicode point is <code>0x10ffff</code>.</p> | 507 | <p>Note that the maximum valid Unicode point is <code>0x10ffff</code>.</p> |
| 714 | <h4 id="multiline-string-literals">Multiline String Literals</h4> | 508 | {#header_close#} |
| | 509 | {#header_open|Multiline String Literals#} |
| 715 | <p> | 510 | <p> |
| 716 | Multiline string literals have no escapes and can span across multiple lines. | 511 | Multiline string literals have no escapes and can span across multiple lines. |
| 717 | To start a multiline string literal, use the <code>\\</code> token. Just like a comment, | 512 | To start a multiline string literal, use the <code>\\</code> token. Just like a comment, |
| ... | @@ -747,7 +542,9 @@ Test 1/1 string literals...OK</code></pre> | ... | @@ -747,7 +542,9 @@ Test 1/1 string literals...OK</code></pre> |
| 747 | <ul> | 542 | <ul> |
| 748 | <li><a href="#builtin-embedFile">@embedFile</a></li> | 543 | <li><a href="#builtin-embedFile">@embedFile</a></li> |
| 749 | </ul> | 544 | </ul> |
| 750 | <h3 id="values-assignment">Assignment</h3> | 545 | {#header_close#} |
| | 546 | {#header_close#} |
| | 547 | {#header_open|Assignment#} |
| 751 | <p>Use <code>const</code> to assign a value to an identifier:</p> | 548 | <p>Use <code>const</code> to assign a value to an identifier:</p> |
| 752 | <pre><code class="zig">const x = 1234; | 549 | <pre><code class="zig">const x = 1234; |
| 753 | | 550 | |
| ... | @@ -798,14 +595,17 @@ test "init with undefined" { | ... | @@ -798,14 +595,17 @@ test "init with undefined" { |
| 798 | }</code></pre> | 595 | }</code></pre> |
| 799 | <pre><code class="sh">$ zig test test.zig | 596 | <pre><code class="sh">$ zig test test.zig |
| 800 | Test 1/1 init with undefined...OK</code></pre> | 597 | Test 1/1 init with undefined...OK</code></pre> |
| 801 | <h2 id="integers">Integers</h2> | 598 | {#header_close#} |
| 802 | <h3 id="integer-literals">Integer Literals</h3> | 599 | {#header_close#} |
| | 600 | {#header_open|Integers#} |
| | 601 | {#header_open|Integer Literals#} |
| 803 | <pre><code class="zig">const decimal_int = 98222; | 602 | <pre><code class="zig">const decimal_int = 98222; |
| 804 | const hex_int = 0xff; | 603 | const hex_int = 0xff; |
| 805 | const another_hex_int = 0xFF; | 604 | const another_hex_int = 0xFF; |
| 806 | const octal_int = 0o755; | 605 | const octal_int = 0o755; |
| 807 | const binary_int = 0b11110000;</code></pre> | 606 | const binary_int = 0b11110000;</code></pre> |
| 808 | <h3 id="runtime-integer-values">Runtime Integer Values</h3> | 607 | {#header_close#} |
| | 608 | {#header_open|Runtime Integer Values#} |
| 809 | <p> | 609 | <p> |
| 810 | Integer literals have no size limitation, and if any undefined behavior occurs, | 610 | Integer literals have no size limitation, and if any undefined behavior occurs, |
| 811 | the compiler catches it. | 611 | the compiler catches it. |
| ... | @@ -833,8 +633,11 @@ const binary_int = 0b11110000;</code></pre> | ... | @@ -833,8 +633,11 @@ const binary_int = 0b11110000;</code></pre> |
| 833 | <li><a href="#undef-division-by-zero">Division By Zero</a></li> | 633 | <li><a href="#undef-division-by-zero">Division By Zero</a></li> |
| 834 | <li><a href="#undef-int-overflow-wrap">Wrapping Operations</a></li> | 634 | <li><a href="#undef-int-overflow-wrap">Wrapping Operations</a></li> |
| 835 | </ul> | 635 | </ul> |
| 836 | <h2 id="floats">Floats</h2> | 636 | {#header_close#} |
| 837 | <h3 id="float-literals">Float Literals</h3> | 637 | {#header_close#} |
| | 638 | {#header_open|Floats#} |
| | 639 | {#header_close#} |
| | 640 | {#header_open|Float Literals#} |
| 838 | <pre><code class="zig">const floating_point = 123.0E+77; | 641 | <pre><code class="zig">const floating_point = 123.0E+77; |
| 839 | const another_float = 123.0; | 642 | const another_float = 123.0; |
| 840 | const yet_another = 123.0e+77; | 643 | const yet_another = 123.0e+77; |
| ... | @@ -842,7 +645,8 @@ const yet_another = 123.0e+77; | ... | @@ -842,7 +645,8 @@ const yet_another = 123.0e+77; |
| 842 | const hex_floating_point = 0x103.70p-5; | 645 | const hex_floating_point = 0x103.70p-5; |
| 843 | const another_hex_float = 0x103.70; | 646 | const another_hex_float = 0x103.70; |
| 844 | const yet_another_hex_float = 0x103.70P-5;</code></pre> | 647 | const yet_another_hex_float = 0x103.70P-5;</code></pre> |
| 845 | <h3 id="float-operations">Floating Point Operations</h3> | 648 | {#header_close#} |
| | 649 | {#header_open|Floating Point Operations#} |
| 846 | <p>By default floating point operations use <code>Optimized</code> mode, | 650 | <p>By default floating point operations use <code>Optimized</code> mode, |
| 847 | but you can switch to <code>Strict</code> mode on a per-block basis:</p> | 651 | but you can switch to <code>Strict</code> mode on a per-block basis:</p> |
| 848 | <p>foo.zig</p> | 652 | <p>foo.zig</p> |
| ... | @@ -881,8 +685,9 @@ strict = 9.765625e-3</code></pre> | ... | @@ -881,8 +685,9 @@ strict = 9.765625e-3</code></pre> |
| 881 | <li><a href="#builtin-setFloatMode">@setFloatMode</a></li> | 685 | <li><a href="#builtin-setFloatMode">@setFloatMode</a></li> |
| 882 | <li><a href="#undef-division-by-zero">Division By Zero</a></li> | 686 | <li><a href="#undef-division-by-zero">Division By Zero</a></li> |
| 883 | </ul> | 687 | </ul> |
| 884 | <h2 id="operators">Operators</h2> | 688 | {#header_close#} |
| 885 | <h3 id="operators-table">Table of Operators</h2> | 689 | {#header_open|Operators#} |
| | 690 | {#header_open|Table of Operators#} |
| 886 | <table> | 691 | <table> |
| 887 | <tr> | 692 | <tr> |
| 888 | <th> | 693 | <th> |
| ... | @@ -1470,7 +1275,8 @@ const ptr = &amp;x; | ... | @@ -1470,7 +1275,8 @@ const ptr = &amp;x; |
| 1470 | </td> | 1275 | </td> |
| 1471 | </tr> | 1276 | </tr> |
| 1472 | </table> | 1277 | </table> |
| 1473 | <h3 id="operators-precedence">Precedence</h3> | 1278 | {#header_close#} |
| | 1279 | {#header_open|Precedence#} |
| 1474 | <pre><code>x() x[] x.y | 1280 | <pre><code>x() x[] x.y |
| 1475 | !x -x -%x ~x *x &amp;x ?x %x %%x ??x | 1281 | !x -x -%x ~x *x &amp;x ?x %x %%x ??x |
| 1476 | x{} | 1282 | x{} |
| ... | @@ -1485,7 +1291,9 @@ and | ... | @@ -1485,7 +1291,9 @@ and |
| 1485 | or | 1291 | or |
| 1486 | ?? catch | 1292 | ?? catch |
| 1487 | = *= /= %= += -= &lt;&lt;= &gt;&gt;= &amp;= ^= |=</code></pre> | 1293 | = *= /= %= += -= &lt;&lt;= &gt;&gt;= &amp;= ^= |=</code></pre> |
| 1488 | <h2 id="arrays">Arrays</h2> | 1294 | {#header_close#} |
| | 1295 | {#header_close#} |
| | 1296 | {#header_open|Arrays#} |
| 1489 | <pre><code class="zig">const assert = @import("std").debug.assert; | 1297 | <pre><code class="zig">const assert = @import("std").debug.assert; |
| 1490 | const mem = @import("std").mem; | 1298 | const mem = @import("std").mem; |
| 1491 | | 1299 | |
| ... | @@ -1599,7 +1407,8 @@ Test 4/4 array initialization with function calls...OK</code></pre> | ... | @@ -1599,7 +1407,8 @@ Test 4/4 array initialization with function calls...OK</code></pre> |
| 1599 | <li><a href="#for">for</a></li> | 1407 | <li><a href="#for">for</a></li> |
| 1600 | <li><a href="#slices">Slices</a></li> | 1408 | <li><a href="#slices">Slices</a></li> |
| 1601 | </ul> | 1409 | </ul> |
| 1602 | <h2 id="pointers">Pointers</h2> | 1410 | {#header_close#} |
| | 1411 | {#header_open|Pointers#} |
| 1603 | <pre><code class="zig">const assert = @import("std").debug.assert; | 1412 | <pre><code class="zig">const assert = @import("std").debug.assert; |
| 1604 | | 1413 | |
| 1605 | test "address of syntax" { | 1414 | test "address of syntax" { |
| ... | @@ -1737,7 +1546,7 @@ Test 5/8 volatile...OK | ... | @@ -1737,7 +1546,7 @@ Test 5/8 volatile...OK |
| 1737 | Test 6/8 nullable pointers...OK | 1546 | Test 6/8 nullable pointers...OK |
| 1738 | Test 7/8 pointer casting...OK | 1547 | Test 7/8 pointer casting...OK |
| 1739 | Test 8/8 pointer child type...OK</code></pre> | 1548 | Test 8/8 pointer child type...OK</code></pre> |
| 1740 | <h3 id="alignment">Alignment</h3> | 1549 | {#header_open|Alignment#} |
| 1741 | <p> | 1550 | <p> |
| 1742 | Each type has an <strong>alignment</strong> - a number of bytes such that, | 1551 | Each type has an <strong>alignment</strong> - a number of bytes such that, |
| 1743 | when a value of the type is loaded from or stored to memory, | 1552 | when a value of the type is loaded from or stored to memory, |
| ... | @@ -1838,7 +1647,8 @@ Test 1/1 pointer alignment safety...incorrect alignment | ... | @@ -1838,7 +1647,8 @@ Test 1/1 pointer alignment safety...incorrect alignment |
| 1838 | | 1647 | |
| 1839 | Tests failed. Use the following command to reproduce the failure: | 1648 | Tests failed. Use the following command to reproduce the failure: |
| 1840 | ./test</code></pre> | 1649 | ./test</code></pre> |
| 1841 | <h3 id="type-based-alias-analysis">Type Based Alias Analysis</h3> | 1650 | {#header_close#} |
| | 1651 | {#header_open|Type Based Alias Analysis#} |
| 1842 | <p>Zig uses Type Based Alias Analysis (also known as Strict Aliasing) to | 1652 | <p>Zig uses Type Based Alias Analysis (also known as Strict Aliasing) to |
| 1843 | perform some optimizations. This means that pointers of different types must | 1653 | perform some optimizations. This means that pointers of different types must |
| 1844 | not alias the same memory, with the exception of <code>u8</code>. Pointers to | 1654 | not alias the same memory, with the exception of <code>u8</code>. Pointers to |
| ... | @@ -1854,7 +1664,9 @@ Tests failed. Use the following command to reproduce the failure: | ... | @@ -1854,7 +1664,9 @@ Tests failed. Use the following command to reproduce the failure: |
| 1854 | <li><a href="#slices">Slices</a></li> | 1664 | <li><a href="#slices">Slices</a></li> |
| 1855 | <li><a href="#memory">Memory</a></li> | 1665 | <li><a href="#memory">Memory</a></li> |
| 1856 | </ul> | 1666 | </ul> |
| 1857 | <h2 id="slices">Slices</h2> | 1667 | {#header_close#} |
| | 1668 | {#header_close#} |
| | 1669 | {#header_open|Slices#} |
| 1858 | <pre><code class="zig">const assert = @import("std").debug.assert; | 1670 | <pre><code class="zig">const assert = @import("std").debug.assert; |
| 1859 | | 1671 | |
| 1860 | test "basic slices" { | 1672 | test "basic slices" { |
| ... | @@ -1954,7 +1766,8 @@ Test 3/3 slice widening...OK</code></pre> | ... | @@ -1954,7 +1766,8 @@ Test 3/3 slice widening...OK</code></pre> |
| 1954 | <li><a href="#for">for</a></li> | 1766 | <li><a href="#for">for</a></li> |
| 1955 | <li><a href="#arrays">Arrays</a></li> | 1767 | <li><a href="#arrays">Arrays</a></li> |
| 1956 | </ul> | 1768 | </ul> |
| 1957 | <h2 id="struct">struct</h2> | 1769 | {#header_close#} |
| | 1770 | {#header_open|struct#} |
| 1958 | <pre><code class="zig">// Declare a struct. | 1771 | <pre><code class="zig">// Declare a struct. |
| 1959 | // Zig gives no guarantees about the order of fields and whether or | 1772 | // Zig gives no guarantees about the order of fields and whether or |
| 1960 | // not there will be padding. | 1773 | // not there will be padding. |
| ... | @@ -2099,7 +1912,8 @@ Test 4/4 linked list...OK</code></pre> | ... | @@ -2099,7 +1912,8 @@ Test 4/4 linked list...OK</code></pre> |
| 2099 | <li><a href="#comptime">comptime</a></li> | 1912 | <li><a href="#comptime">comptime</a></li> |
| 2100 | <li><a href="#builtin-fieldParentPtr">@fieldParentPtr</a></li> | 1913 | <li><a href="#builtin-fieldParentPtr">@fieldParentPtr</a></li> |
| 2101 | </ul> | 1914 | </ul> |
| 2102 | <h2 id="enum">enum</h2> | 1915 | {#header_close#} |
| | 1916 | {#header_open|enum#} |
| 2103 | <pre><code class="zig">const assert = @import("std").debug.assert; | 1917 | <pre><code class="zig">const assert = @import("std").debug.assert; |
| 2104 | const mem = @import("std").mem; | 1918 | const mem = @import("std").mem; |
| 2105 | | 1919 | |
| ... | @@ -2216,7 +2030,8 @@ Test 8/8 @tagName...OK</code></pre> | ... | @@ -2216,7 +2030,8 @@ Test 8/8 @tagName...OK</code></pre> |
| 2216 | <li><a href="#builtin-memberCount">@memberCount</a></li> | 2030 | <li><a href="#builtin-memberCount">@memberCount</a></li> |
| 2217 | <li><a href="#builtin-tagName">@tagName</a></li> | 2031 | <li><a href="#builtin-tagName">@tagName</a></li> |
| 2218 | </ul> | 2032 | </ul> |
| 2219 | <h2 id="union">union</h2> | 2033 | {#header_close#} |
| | 2034 | {#header_open|union#} |
| 2220 | <pre><code class="zig">const assert = @import("std").debug.assert; | 2035 | <pre><code class="zig">const assert = @import("std").debug.assert; |
| 2221 | const mem = @import("std").mem; | 2036 | const mem = @import("std").mem; |
| 2222 | | 2037 | |
| ... | @@ -2323,7 +2138,8 @@ Test 7/7 @tagName...OK</code></pre> | ... | @@ -2323,7 +2138,8 @@ Test 7/7 @tagName...OK</code></pre> |
| 2323 | Unions with an enum tag are generated as a struct with a tag field and union field. Zig | 2138 | Unions with an enum tag are generated as a struct with a tag field and union field. Zig |
| 2324 | sorts the order of the tag and union field by the largest alignment. | 2139 | sorts the order of the tag and union field by the largest alignment. |
| 2325 | </p> | 2140 | </p> |
| 2326 | <h2 id="switch">switch</h2> | 2141 | {#header_close#} |
| | 2142 | {#header_open|switch#} |
| 2327 | <pre><code class="zig">const assert = @import("std").debug.assert; | 2143 | <pre><code class="zig">const assert = @import("std").debug.assert; |
| 2328 | const builtin = @import("builtin"); | 2144 | const builtin = @import("builtin"); |
| 2329 | | 2145 | |
| ... | @@ -2426,7 +2242,8 @@ Test 3/3 switch inside function...OK</code></pre> | ... | @@ -2426,7 +2242,8 @@ Test 3/3 switch inside function...OK</code></pre> |
| 2426 | <li><a href="#builtin-compileError">@compileError</a></li> | 2242 | <li><a href="#builtin-compileError">@compileError</a></li> |
| 2427 | <li><a href="#compile-variables">Compile Variables</a></li> | 2243 | <li><a href="#compile-variables">Compile Variables</a></li> |
| 2428 | </ul> | 2244 | </ul> |
| 2429 | <h2 id="while">while</h2> | 2245 | {#header_close#} |
| | 2246 | {#header_open|while#} |
| 2430 | <pre><code class="zig">const assert = @import("std").debug.assert; | 2247 | <pre><code class="zig">const assert = @import("std").debug.assert; |
| 2431 | | 2248 | |
| 2432 | test "while basic" { | 2249 | test "while basic" { |
| ... | @@ -2595,7 +2412,8 @@ Test 8/8 inline while loop...OK</code></pre> | ... | @@ -2595,7 +2412,8 @@ Test 8/8 inline while loop...OK</code></pre> |
| 2595 | <li><a href="#comptime">comptime</a></li> | 2412 | <li><a href="#comptime">comptime</a></li> |
| 2596 | <li><a href="#unreachable">unreachable</a></li> | 2413 | <li><a href="#unreachable">unreachable</a></li> |
| 2597 | </ul> | 2414 | </ul> |
| 2598 | <h2 id="for">for</h2> | 2415 | {#header_close#} |
| | 2416 | {#header_open|for#} |
| 2599 | <pre><code class="zig">const assert = @import("std").debug.assert; | 2417 | <pre><code class="zig">const assert = @import("std").debug.assert; |
| 2600 | | 2418 | |
| 2601 | test "for basics" { | 2419 | test "for basics" { |
| ... | @@ -2696,7 +2514,8 @@ Test 4/4 inline for loop...OK</code></pre> | ... | @@ -2696,7 +2514,8 @@ Test 4/4 inline for loop...OK</code></pre> |
| 2696 | <li><a href="#arrays">Arrays</a></li> | 2514 | <li><a href="#arrays">Arrays</a></li> |
| 2697 | <li><a href="#slices">Slices</a></li> | 2515 | <li><a href="#slices">Slices</a></li> |
| 2698 | </ul> | 2516 | </ul> |
| 2699 | <h2 id="if">if</h2> | 2517 | {#header_close#} |
| | 2518 | {#header_open|if#} |
| 2700 | <pre><code class="zig">// If expressions have three uses, corresponding to the three types: | 2519 | <pre><code class="zig">// If expressions have three uses, corresponding to the three types: |
| 2701 | // * bool | 2520 | // * bool |
| 2702 | // * ?T | 2521 | // * ?T |
| ... | @@ -2814,7 +2633,8 @@ Test 3/3 if error union...OK</code></pre> | ... | @@ -2814,7 +2633,8 @@ Test 3/3 if error union...OK</code></pre> |
| 2814 | <li><a href="#nullables">Nullables</a></li> | 2633 | <li><a href="#nullables">Nullables</a></li> |
| 2815 | <li><a href="#errors">Errors</a></li> | 2634 | <li><a href="#errors">Errors</a></li> |
| 2816 | </ul> | 2635 | </ul> |
| 2817 | <h2 id="goto">goto</h2> | 2636 | {#header_close#} |
| | 2637 | {#header_open|goto#} |
| 2818 | <pre><code class="zig">const assert = @import("std").debug.assert; | 2638 | <pre><code class="zig">const assert = @import("std").debug.assert; |
| 2819 | | 2639 | |
| 2820 | test "goto" { | 2640 | test "goto" { |
| ... | @@ -2830,7 +2650,7 @@ label: | ... | @@ -2830,7 +2650,7 @@ label: |
| 2830 | Test 1/1 goto...OK | 2650 | Test 1/1 goto...OK |
| 2831 | </code></pre> | 2651 | </code></pre> |
| 2832 | <p>Note that there are <a href="https://github.com/zig-lang/zig/issues/346">plans to remove goto</a></p> | 2652 | <p>Note that there are <a href="https://github.com/zig-lang/zig/issues/346">plans to remove goto</a></p> |
| 2833 | <h2 id="defer">defer</h2> | 2653 | {{deheader_open:fer}} |
| 2834 | <pre><code class="zig">const assert = @import("std").debug.assert; | 2654 | <pre><code class="zig">const assert = @import("std").debug.assert; |
| 2835 | const printf = @import("std").io.stdout.printf; | 2655 | const printf = @import("std").io.stdout.printf; |
| 2836 | | 2656 | |
| ... | @@ -2920,7 +2740,8 @@ OK | ... | @@ -2920,7 +2740,8 @@ OK |
| 2920 | <ul> | 2740 | <ul> |
| 2921 | <li><a href="#errors">Errors</a></li> | 2741 | <li><a href="#errors">Errors</a></li> |
| 2922 | </ul> | 2742 | </ul> |
| 2923 | <h2 id="unreachable">unreachable</h2> | 2743 | {#header_close#} |
| | 2744 | {#header_open|unreachable#} |
| 2924 | <p> | 2745 | <p> |
| 2925 | In <code>Debug</code> and <code>ReleaseSafe</code> mode, and when using <code>zig test</code>, | 2746 | In <code>Debug</code> and <code>ReleaseSafe</code> mode, and when using <code>zig test</code>, |
| 2926 | <code>unreachable</code> emits a call to <code>panic</code> with the message <code>reached unreachable code</code>. | 2747 | <code>unreachable</code> emits a call to <code>panic</code> with the message <code>reached unreachable code</code>. |
| ... | @@ -2930,7 +2751,7 @@ OK | ... | @@ -2930,7 +2751,7 @@ OK |
| 2930 | will never be hit to perform optimizations. However, <code>zig test</code> even in <code>ReleaseFast</code> mode | 2751 | will never be hit to perform optimizations. However, <code>zig test</code> even in <code>ReleaseFast</code> mode |
| 2931 | still emits <code>unreachable</code> as calls to <code>panic</code>. | 2752 | still emits <code>unreachable</code> as calls to <code>panic</code>. |
| 2932 | </p> | 2753 | </p> |
| 2933 | <h3 id="unreachable-basics">Basics</h3> | 2754 | {#header_open|Basics#} |
| 2934 | <pre><code class="zig">// unreachable is used to assert that control flow will never happen upon a | 2755 | <pre><code class="zig">// unreachable is used to assert that control flow will never happen upon a |
| 2935 | // particular location: | 2756 | // particular location: |
| 2936 | test "basic math" { | 2757 | test "basic math" { |
| ... | @@ -2974,7 +2795,8 @@ lib/zig/std/special/bootstrap.zig:34:25: 0x0000000000214750 in ??? (test) | ... | @@ -2974,7 +2795,8 @@ lib/zig/std/special/bootstrap.zig:34:25: 0x0000000000214750 in ??? (test) |
| 2974 | | 2795 | |
| 2975 | Tests failed. Use the following command to reproduce the failure: | 2796 | Tests failed. Use the following command to reproduce the failure: |
| 2976 | ./test</code></pre> | 2797 | ./test</code></pre> |
| 2977 | <h3 id="unreachable-comptime">At Compile-Time</h3> | 2798 | {#header_close#} |
| | 2799 | {#header_open|At Compile-Time#} |
| 2978 | <pre><code class="zig">const assert = @import("std").debug.assert; | 2800 | <pre><code class="zig">const assert = @import("std").debug.assert; |
| 2979 | | 2801 | |
| 2980 | comptime { | 2802 | comptime { |
| ... | @@ -2995,7 +2817,9 @@ test.zig:9:12: error: unreachable code | ... | @@ -2995,7 +2817,9 @@ test.zig:9:12: error: unreachable code |
| 2995 | <li><a href="#build-mode">Build Mode</a></li> | 2817 | <li><a href="#build-mode">Build Mode</a></li> |
| 2996 | <li><a href="#comptime">comptime</a></li> | 2818 | <li><a href="#comptime">comptime</a></li> |
| 2997 | </ul> | 2819 | </ul> |
| 2998 | <h2 id="noreturn">noreturn</h2> | 2820 | {#header_close#} |
| | 2821 | {#header_close#} |
| | 2822 | {#header_open|noreturn#} |
| 2999 | <p> | 2823 | <p> |
| 3000 | <code>noreturn</code> is the type of: | 2824 | <code>noreturn</code> is the type of: |
| 3001 | </p> | 2825 | </p> |
| ... | @@ -3029,7 +2853,8 @@ fn bar() -&gt; %u32 { | ... | @@ -3029,7 +2853,8 @@ fn bar() -&gt; %u32 { |
| 3029 | } | 2853 | } |
| 3030 | | 2854 | |
| 3031 | const assert = @import("std").debug.assert;</code></pre> | 2855 | const assert = @import("std").debug.assert;</code></pre> |
| 3032 | <h2 id="functions">Functions</h2> | 2856 | {#header_close#} |
| | 2857 | {#header_open|Functions#} |
| 3033 | <pre><code class="zig">const assert = @import("std").debug.assert; | 2858 | <pre><code class="zig">const assert = @import("std").debug.assert; |
| 3034 | | 2859 | |
| 3035 | // Functions are declared like this | 2860 | // Functions are declared like this |
| ... | @@ -3091,7 +2916,7 @@ comptime { | ... | @@ -3091,7 +2916,7 @@ comptime { |
| 3091 | | 2916 | |
| 3092 | fn foo() { }</code></pre> | 2917 | fn foo() { }</code></pre> |
| 3093 | <pre><code class="sh">$ zig build-obj test.zig</code></pre> | 2918 | <pre><code class="sh">$ zig build-obj test.zig</code></pre> |
| 3094 | <h3 id="functions-by-val-params">Pass-by-value Parameters</h3> | 2919 | {#header_open|Pass-by-value Parameters#} |
| 3095 | <p> | 2920 | <p> |
| 3096 | In Zig, structs, unions, and enums with payloads cannot be passed by value | 2921 | In Zig, structs, unions, and enums with payloads cannot be passed by value |
| 3097 | to a function. | 2922 | to a function. |
| ... | @@ -3127,7 +2952,9 @@ export fn entry() { | ... | @@ -3127,7 +2952,9 @@ export fn entry() { |
| 3127 | the C ABI does allow passing structs and unions by value. So functions which | 2952 | the C ABI does allow passing structs and unions by value. So functions which |
| 3128 | use the C calling convention may pass structs and unions by value. | 2953 | use the C calling convention may pass structs and unions by value. |
| 3129 | </p> | 2954 | </p> |
| 3130 | <h2 id="errors">Errors</h2> | 2955 | {#header_close#} |
| | 2956 | {#header_close#} |
| | 2957 | {#header_open|Errors#} |
| 3131 | <p> | 2958 | <p> |
| 3132 | One of the distinguishing features of Zig is its exception handling strategy. | 2959 | One of the distinguishing features of Zig is its exception handling strategy. |
| 3133 | </p> | 2960 | </p> |
| ... | @@ -3321,7 +3148,8 @@ pub fn parseU64(buf: []const u8, radix: u8) -&gt; %u64 { | ... | @@ -3321,7 +3148,8 @@ pub fn parseU64(buf: []const u8, radix: u8) -&gt; %u64 { |
| 3321 | <li><a href="#if">if</a></li> | 3148 | <li><a href="#if">if</a></li> |
| 3322 | <li><a href="#switch">switch</a></li> | 3149 | <li><a href="#switch">switch</a></li> |
| 3323 | </ul> | 3150 | </ul> |
| 3324 | <h2 id="nullables">Nullables</h2> | 3151 | {#header_close#} |
| | 3152 | {#header_open|Nullables#} |
| 3325 | <p> | 3153 | <p> |
| 3326 | One area that Zig provides safety without compromising efficiency or | 3154 | One area that Zig provides safety without compromising efficiency or |
| 3327 | readability is with the nullable type. | 3155 | readability is with the nullable type. |
| ... | @@ -3415,7 +3243,8 @@ fn doAThing() -&gt; ?&amp;Foo { | ... | @@ -3415,7 +3243,8 @@ fn doAThing() -&gt; ?&amp;Foo { |
| 3415 | The optimizer can sometimes make better decisions knowing that pointer arguments | 3243 | The optimizer can sometimes make better decisions knowing that pointer arguments |
| 3416 | cannot be null. | 3244 | cannot be null. |
| 3417 | </p> | 3245 | </p> |
| 3418 | <h2 id="casting">Casting</h2> | 3246 | {#header_close#} |
| | 3247 | {#header_open|Casting#} |
| 3419 | <p>TODO: explain implicit vs explicit casting</p> | 3248 | <p>TODO: explain implicit vs explicit casting</p> |
| 3420 | <p>TODO: resolve peer types builtin</p> | 3249 | <p>TODO: resolve peer types builtin</p> |
| 3421 | <p>TODO: truncate builtin</p> | 3250 | <p>TODO: truncate builtin</p> |
| ... | @@ -3424,24 +3253,27 @@ fn doAThing() -&gt; ?&amp;Foo { | ... | @@ -3424,24 +3253,27 @@ fn doAThing() -&gt; ?&amp;Foo { |
| 3424 | <p>TODO: ptr to int builtin</p> | 3253 | <p>TODO: ptr to int builtin</p> |
| 3425 | <p>TODO: ptrcast builtin</p> | 3254 | <p>TODO: ptrcast builtin</p> |
| 3426 | <p>TODO: explain number literals vs concrete types</p> | 3255 | <p>TODO: explain number literals vs concrete types</p> |
| 3427 | <h2 id="void">void</h2> | 3256 | {#header_close#} |
| | 3257 | {#header_open|void#} |
| 3428 | <p>TODO: assigning void has no codegen</p> | 3258 | <p>TODO: assigning void has no codegen</p> |
| 3429 | <p>TODO: hashmap with void becomes a set</p> | 3259 | <p>TODO: hashmap with void becomes a set</p> |
| 3430 | <p>TODO: difference between c_void and void</p> | 3260 | <p>TODO: difference between c_void and void</p> |
| 3431 | <p>TODO: void is the default return value of functions</p> | 3261 | <p>TODO: void is the default return value of functions</p> |
| 3432 | <p>TODO: functions require assigning the return value</p> | 3262 | <p>TODO: functions require assigning the return value</p> |
| 3433 | <h2 id="this">this</h2> | 3263 | {#header_close#} |
| | 3264 | {#header_open|this#} |
| 3434 | <p>TODO: example of this referring to Self struct</p> | 3265 | <p>TODO: example of this referring to Self struct</p> |
| 3435 | <p>TODO: example of this referring to recursion function</p> | 3266 | <p>TODO: example of this referring to recursion function</p> |
| 3436 | <p>TODO: example of this referring to basic block for @setDebugSafety</p> | 3267 | <p>TODO: example of this referring to basic block for @setDebugSafety</p> |
| 3437 | <h2 id="comptime">comptime</h2> | 3268 | {#header_close#} |
| | 3269 | {#header_open|comptime#} |
| 3438 | <p> | 3270 | <p> |
| 3439 | Zig places importance on the concept of whether an expression is known at compile-time. | 3271 | Zig places importance on the concept of whether an expression is known at compile-time. |
| 3440 | There are a few different places this concept is used, and these building blocks are used | 3272 | There are a few different places this concept is used, and these building blocks are used |
| 3441 | to keep the language small, readable, and powerful. | 3273 | to keep the language small, readable, and powerful. |
| 3442 | </p> | 3274 | </p> |
| 3443 | <h3 id="introducing-compile-time-concept">Introducing the Compile-Time Concept</h3> | 3275 | {#header_open|Introducing the Compile-Time Concept#} |
| 3444 | <h4 id="compile-time-parameters">Compile-Time Parameters</h4> | 3276 | {#header_open|Compile-Time Parameters#} |
| 3445 | <p> | 3277 | <p> |
| 3446 | Compile-time parameters is how Zig implements generics. It is compile-time duck typing. | 3278 | Compile-time parameters is how Zig implements generics. It is compile-time duck typing. |
| 3447 | </p> | 3279 | </p> |
| ... | @@ -3549,7 +3381,8 @@ fn letsTryToCompareBools(a: bool, b: bool) -&gt; bool { | ... | @@ -3549,7 +3381,8 @@ fn letsTryToCompareBools(a: bool, b: bool) -&gt; bool { |
| 3549 | This works the same way for <code>switch</code> expressions - they are implicitly inlined | 3381 | This works the same way for <code>switch</code> expressions - they are implicitly inlined |
| 3550 | when the target expression is compile-time known. | 3382 | when the target expression is compile-time known. |
| 3551 | </p> | 3383 | </p> |
| 3552 | <h4 id="compile-time-variables">Compile-Time Variables</h4> | 3384 | {#header_close#} |
| | 3385 | {#header_open|Compile-Time Variables#} |
| 3553 | <p> | 3386 | <p> |
| 3554 | In Zig, the programmer can label variables as <code>comptime</code>. This guarantees to the compiler | 3387 | In Zig, the programmer can label variables as <code>comptime</code>. This guarantees to the compiler |
| 3555 | that every load and store of the variable is performed at compile-time. Any violation of this results in a | 3388 | that every load and store of the variable is performed at compile-time. Any violation of this results in a |
| ... | @@ -3631,7 +3464,8 @@ fn performFn(start_value: i32) -&gt; i32 { | ... | @@ -3631,7 +3464,8 @@ fn performFn(start_value: i32) -&gt; i32 { |
| 3631 | later in this article, allows expressiveness that in other languages requires using macros, | 3464 | later in this article, allows expressiveness that in other languages requires using macros, |
| 3632 | generated code, or a preprocessor to accomplish. | 3465 | generated code, or a preprocessor to accomplish. |
| 3633 | </p> | 3466 | </p> |
| 3634 | <h4 id="compile-time-expressions">Compile-Time Expressions</h4> | 3467 | {#header_close#} |
| | 3468 | {#header_open|Compile-Time Expressions#} |
| 3635 | <p> | 3469 | <p> |
| 3636 | In Zig, it matters whether a given expression is known at compile-time or run-time. A programmer can | 3470 | In Zig, it matters whether a given expression is known at compile-time or run-time. A programmer can |
| 3637 | use a <code>comptime</code> expression to guarantee that the expression will be evaluated at compile-time. | 3471 | use a <code>comptime</code> expression to guarantee that the expression will be evaluated at compile-time. |
| ... | @@ -3860,7 +3694,9 @@ fn sum(numbers: []i32) -&gt; i32 { | ... | @@ -3860,7 +3694,9 @@ fn sum(numbers: []i32) -&gt; i32 { |
| 3860 | we could call the <code>sum</code> function as is with a slice of numbers whose length and values were | 3694 | we could call the <code>sum</code> function as is with a slice of numbers whose length and values were |
| 3861 | only known at run-time. | 3695 | only known at run-time. |
| 3862 | </p> | 3696 | </p> |
| 3863 | <h3 id="generic-data-structures">Generic Data Structures</h3> | 3697 | {#header_close#} |
| | 3698 | {#header_close#} |
| | 3699 | {#header_open|Generic Data Structures#} |
| 3864 | <p> | 3700 | <p> |
| 3865 | Zig uses these capabilities to implement generic data structures without introducing any | 3701 | Zig uses these capabilities to implement generic data structures without introducing any |
| 3866 | special-case syntax. If you followed along so far, you may already know how to create a | 3702 | special-case syntax. If you followed along so far, you may already know how to create a |
| ... | @@ -3895,7 +3731,8 @@ fn sum(numbers: []i32) -&gt; i32 { | ... | @@ -3895,7 +3731,8 @@ fn sum(numbers: []i32) -&gt; i32 { |
| 3895 | <code>Node</code> refers to itself as a pointer, which is not actually an infinite regression, so | 3731 | <code>Node</code> refers to itself as a pointer, which is not actually an infinite regression, so |
| 3896 | it works fine. | 3732 | it works fine. |
| 3897 | </p> | 3733 | </p> |
| 3898 | <h3 id="case-study-printf">Case Study: printf in Zig</h3> | 3734 | {#header_close#} |
| | 3735 | {#header_open|Case Study: printf in Zig#} |
| 3899 | <p> | 3736 | <p> |
| 3900 | Putting all of this together, let's seee how <code>printf</code> works in Zig. | 3737 | Putting all of this together, let's seee how <code>printf</code> works in Zig. |
| 3901 | </p> | 3738 | </p> |
| ... | @@ -4045,35 +3882,42 @@ pub fn main(args: [][]u8) -&gt; %void { | ... | @@ -4045,35 +3882,42 @@ pub fn main(args: [][]u8) -&gt; %void { |
| 4045 | a macro language or a preprocessor language. It's Zig all the way down. | 3882 | a macro language or a preprocessor language. It's Zig all the way down. |
| 4046 | </p> | 3883 | </p> |
| 4047 | <p>TODO: suggestion to not use inline unless necessary</p> | 3884 | <p>TODO: suggestion to not use inline unless necessary</p> |
| 4048 | <h2 id="inline">inline</h2> | 3885 | {#header_close#} |
| | 3886 | {#header_close#} |
| | 3887 | {#header_open|inline#} |
| 4049 | <p>TODO: inline while</p> | 3888 | <p>TODO: inline while</p> |
| 4050 | <p>TODO: inline for</p> | 3889 | <p>TODO: inline for</p> |
| 4051 | <p>TODO: suggestion to not use inline unless necessary</p> | 3890 | <p>TODO: suggestion to not use inline unless necessary</p> |
| 4052 | <h2 id="assembly">Assembly</h2> | 3891 | {#header_close#} |
| | 3892 | {#header_open|Assembly#} |
| 4053 | <p>TODO: example of inline assembly</p> | 3893 | <p>TODO: example of inline assembly</p> |
| 4054 | <p>TODO: example of module level assembly</p> | 3894 | <p>TODO: example of module level assembly</p> |
| 4055 | <p>TODO: example of using inline assembly return value</p> | 3895 | <p>TODO: example of using inline assembly return value</p> |
| 4056 | <p>TODO: example of using inline assembly assigning values to variables</p> | 3896 | <p>TODO: example of using inline assembly assigning values to variables</p> |
| 4057 | <h2 id="atomics">Atomics</h2> | 3897 | {#header_close#} |
| | 3898 | {#header_open|Atomics#} |
| 4058 | <p>TODO: @fence()</p> | 3899 | <p>TODO: @fence()</p> |
| 4059 | <p>TODO: @atomic rmw</p> | 3900 | <p>TODO: @atomic rmw</p> |
| 4060 | <p>TODO: builtin atomic memory ordering enum</p> | 3901 | <p>TODO: builtin atomic memory ordering enum</p> |
| 4061 | <h2 id="builtin-functions">Builtin Functions</h2> | 3902 | {#header_close#} |
| | 3903 | {#header_open|Builtin Functions#} |
| 4062 | <p> | 3904 | <p> |
| 4063 | Builtin functions are provided by the compiler and are prefixed with <code>@</code>. | 3905 | Builtin functions are provided by the compiler and are prefixed with <code>@</code>. |
| 4064 | The <code>comptime</code> keyword on a parameter means that the parameter must be known | 3906 | The <code>comptime</code> keyword on a parameter means that the parameter must be known |
| 4065 | at compile time. | 3907 | at compile time. |
| 4066 | </p> | 3908 | </p> |
| 4067 | <h3 id="builtin-addWithOverflow">@addWithOverflow</h3> | 3909 | {#header_open|@addWithOverflow#} |
| 4068 | <pre><code class="zig">@addWithOverflow(comptime T: type, a: T, b: T, result: &amp;T) -&gt; bool</code></pre> | 3910 | <pre><code class="zig">@addWithOverflow(comptime T: type, a: T, b: T, result: &amp;T) -&gt; bool</code></pre> |
| 4069 | <p> | 3911 | <p> |
| 4070 | Performs <code>*result = a + b</code>. If overflow or underflow occurs, | 3912 | Performs <code>*result = a + b</code>. If overflow or underflow occurs, |
| 4071 | stores the overflowed bits in <code>result</code> and returns <code>true</code>. | 3913 | stores the overflowed bits in <code>result</code> and returns <code>true</code>. |
| 4072 | If no overflow or underflow occurs, returns <code>false</code>. | 3914 | If no overflow or underflow occurs, returns <code>false</code>. |
| 4073 | </p> | 3915 | </p> |
| 4074 | <h3 id="builtin-ArgType">@ArgType</h3> | 3916 | {#header_close#} |
| | 3917 | {#header_open|@ArgType#} |
| 4075 | <p>TODO</p> | 3918 | <p>TODO</p> |
| 4076 | <h3 id="builtin-bitCast">@bitCast</h3> | 3919 | {#header_close#} |
| | 3920 | {#header_open|@bitCast#} |
| 4077 | <pre><code class="zig">@bitCast(comptime DestType: type, value: var) -&gt; DestType</code></pre> | 3921 | <pre><code class="zig">@bitCast(comptime DestType: type, value: var) -&gt; DestType</code></pre> |
| 4078 | <p> | 3922 | <p> |
| 4079 | Converts a value of one type to another type. | 3923 | Converts a value of one type to another type. |
| ... | @@ -4094,7 +3938,8 @@ pub fn main(args: [][]u8) -&gt; %void { | ... | @@ -4094,7 +3938,8 @@ pub fn main(args: [][]u8) -&gt; %void { |
| 4094 | <p> | 3938 | <p> |
| 4095 | Works at compile-time if <code>value</code> is known at compile time. It's a compile error to bitcast a struct to a scalar type of the same size since structs have undefined layout. However if the struct is packed then it works. | 3939 | Works at compile-time if <code>value</code> is known at compile time. It's a compile error to bitcast a struct to a scalar type of the same size since structs have undefined layout. However if the struct is packed then it works. |
| 4096 | </p> | 3940 | </p> |
| 4097 | <h3 id="builtin-breakpoint">@breakpoint</h3> | 3941 | {#header_close#} |
| | 3942 | {#header_open|@breakpoint#} |
| 4098 | <pre><code class="zig">@breakpoint()</code></pre> | 3943 | <pre><code class="zig">@breakpoint()</code></pre> |
| 4099 | <p> | 3944 | <p> |
| 4100 | This function inserts a platform-specific debug trap instruction which causes | 3945 | This function inserts a platform-specific debug trap instruction which causes |
| ... | @@ -4104,7 +3949,8 @@ pub fn main(args: [][]u8) -&gt; %void { | ... | @@ -4104,7 +3949,8 @@ pub fn main(args: [][]u8) -&gt; %void { |
| 4104 | This function is only valid within function scope. | 3949 | This function is only valid within function scope. |
| 4105 | </p> | 3950 | </p> |
| 4106 | | 3951 | |
| 4107 | <h3 id="builtin-alignCast">@alignCast</h3> | 3952 | {#header_close#} |
| | 3953 | {#header_open|@alignCast#} |
| 4108 | <pre><code class="zig">@alignCast(comptime alignment: u29, ptr: var) -&gt; var</code></pre> | 3954 | <pre><code class="zig">@alignCast(comptime alignment: u29, ptr: var) -&gt; var</code></pre> |
| 4109 | <p> | 3955 | <p> |
| 4110 | <code>ptr</code> can be <code>&amp;T</code>, <code>fn()</code>, <code>?&amp;T</code>, | 3956 | <code>ptr</code> can be <code>&amp;T</code>, <code>fn()</code>, <code>?&amp;T</code>, |
| ... | @@ -4114,7 +3960,8 @@ pub fn main(args: [][]u8) -&gt; %void { | ... | @@ -4114,7 +3960,8 @@ pub fn main(args: [][]u8) -&gt; %void { |
| 4114 | <p>A <a href="#undef-incorrect-pointer-alignment">pointer alignment safety check</a> is added | 3960 | <p>A <a href="#undef-incorrect-pointer-alignment">pointer alignment safety check</a> is added |
| 4115 | to the generated code to make sure the pointer is aligned as promised.</p> | 3961 | to the generated code to make sure the pointer is aligned as promised.</p> |
| 4116 | | 3962 | |
| 4117 | <h3 id="builtin-alignOf">@alignOf</h3> | 3963 | {#header_close#} |
| | 3964 | {#header_open|@alignOf#} |
| 4118 | <pre><code class="zig">@alignOf(comptime T: type) -&gt; (number literal)</code></pre> | 3965 | <pre><code class="zig">@alignOf(comptime T: type) -&gt; (number literal)</code></pre> |
| 4119 | <p> | 3966 | <p> |
| 4120 | This function returns the number of bytes that this type should be aligned to | 3967 | This function returns the number of bytes that this type should be aligned to |
| ... | @@ -4134,7 +3981,8 @@ comptime { | ... | @@ -4134,7 +3981,8 @@ comptime { |
| 4134 | <li><a href="#alignment">Alignment</a></li> | 3981 | <li><a href="#alignment">Alignment</a></li> |
| 4135 | </ul> | 3982 | </ul> |
| 4136 | | 3983 | |
| 4137 | <h3 id="builtin-cDefine">@cDefine</h3> | 3984 | {#header_close#} |
| | 3985 | {#header_open|@cDefine#} |
| 4138 | <pre><code class="zig">@cDefine(comptime name: []u8, value)</code></pre> | 3986 | <pre><code class="zig">@cDefine(comptime name: []u8, value)</code></pre> |
| 4139 | <p> | 3987 | <p> |
| 4140 | This function can only occur inside <code>@cImport</code>. | 3988 | This function can only occur inside <code>@cImport</code>. |
| ... | @@ -4159,7 +4007,8 @@ comptime { | ... | @@ -4159,7 +4007,8 @@ comptime { |
| 4159 | <li><a href="#builtin-cUndef">@cUndef</a></li> | 4007 | <li><a href="#builtin-cUndef">@cUndef</a></li> |
| 4160 | <li><a href="#void">void</a></li> | 4008 | <li><a href="#void">void</a></li> |
| 4161 | </ul> | 4009 | </ul> |
| 4162 | <h3 id="builtin-cImport">@cImport</h3> | 4010 | {#header_close#} |
| | 4011 | {#header_open|@cImport#} |
| 4163 | <pre><code class="zig">@cImport(expression) -&gt; (namespace)</code></pre> | 4012 | <pre><code class="zig">@cImport(expression) -&gt; (namespace)</code></pre> |
| 4164 | <p> | 4013 | <p> |
| 4165 | This function parses C code and imports the functions, types, variables, and | 4014 | This function parses C code and imports the functions, types, variables, and |
| ... | @@ -4177,7 +4026,8 @@ comptime { | ... | @@ -4177,7 +4026,8 @@ comptime { |
| 4177 | <li><a href="#builtin-cDefine">@cDefine</a></li> | 4026 | <li><a href="#builtin-cDefine">@cDefine</a></li> |
| 4178 | <li><a href="#builtin-cUndef">@cUndef</a></li> | 4027 | <li><a href="#builtin-cUndef">@cUndef</a></li> |
| 4179 | </ul> | 4028 | </ul> |
| 4180 | <h3 id="builtin-cInclude">@cInclude</h3> | 4029 | {#header_close#} |
| | 4030 | {#header_open|@cInclude#} |
| 4181 | <pre><code class="zig">@cInclude(comptime path: []u8)</code></pre> | 4031 | <pre><code class="zig">@cInclude(comptime path: []u8)</code></pre> |
| 4182 | <p> | 4032 | <p> |
| 4183 | This function can only occur inside <code>@cImport</code>. | 4033 | This function can only occur inside <code>@cImport</code>. |
| ... | @@ -4193,7 +4043,8 @@ comptime { | ... | @@ -4193,7 +4043,8 @@ comptime { |
| 4193 | <li><a href="#builtin-cDefine">@cDefine</a></li> | 4043 | <li><a href="#builtin-cDefine">@cDefine</a></li> |
| 4194 | <li><a href="#builtin-cUndef">@cUndef</a></li> | 4044 | <li><a href="#builtin-cUndef">@cUndef</a></li> |
| 4195 | </ul> | 4045 | </ul> |
| 4196 | <h3 id="builtin-cUndef">@cUndef</h3> | 4046 | {#header_close#} |
| | 4047 | {#header_open|@cUndef#} |
| 4197 | <pre><code class="zig">@cUndef(comptime name: []u8)</code></pre> | 4048 | <pre><code class="zig">@cUndef(comptime name: []u8)</code></pre> |
| 4198 | <p> | 4049 | <p> |
| 4199 | This function can only occur inside <code>@cImport</code>. | 4050 | This function can only occur inside <code>@cImport</code>. |
| ... | @@ -4209,12 +4060,14 @@ comptime { | ... | @@ -4209,12 +4060,14 @@ comptime { |
| 4209 | <li><a href="#builtin-cDefine">@cDefine</a></li> | 4060 | <li><a href="#builtin-cDefine">@cDefine</a></li> |
| 4210 | <li><a href="#builtin-cInclude">@cInclude</a></li> | 4061 | <li><a href="#builtin-cInclude">@cInclude</a></li> |
| 4211 | </ul> | 4062 | </ul> |
| 4212 | <h3 id="builtin-canImplicitCast">@canImplicitCast</h3> | 4063 | {#header_close#} |
| | 4064 | {#header_open|@canImplicitCast#} |
| 4213 | <pre><code class="zig">@canImplicitCast(comptime T: type, value) -&gt; bool</code></pre> | 4065 | <pre><code class="zig">@canImplicitCast(comptime T: type, value) -&gt; bool</code></pre> |
| 4214 | <p> | 4066 | <p> |
| 4215 | Returns whether a value can be implicitly casted to a given type. | 4067 | Returns whether a value can be implicitly casted to a given type. |
| 4216 | </p> | 4068 | </p> |
| 4217 | <h3 id="builtin-clz">@clz</h3> | 4069 | {#header_close#} |
| | 4070 | {#header_open|@clz#} |
| 4218 | <pre><code class="zig">@clz(x: T) -&gt; U</code></pre> | 4071 | <pre><code class="zig">@clz(x: T) -&gt; U</code></pre> |
| 4219 | <p> | 4072 | <p> |
| 4220 | This function counts the number of leading zeroes in <code>x</code> which is an integer | 4073 | This function counts the number of leading zeroes in <code>x</code> which is an integer |
| ... | @@ -4228,7 +4081,8 @@ comptime { | ... | @@ -4228,7 +4081,8 @@ comptime { |
| 4228 | If <code>x</code> is zero, <code>@clz</code> returns <code>T.bit_count</code>. | 4081 | If <code>x</code> is zero, <code>@clz</code> returns <code>T.bit_count</code>. |
| 4229 | </p> | 4082 | </p> |
| 4230 | | 4083 | |
| 4231 | <h3 id="builtin-cmpxchg">@cmpxchg</h3> | 4084 | {#header_close#} |
| | 4085 | {#header_open|@cmpxchg#} |
| 4232 | <pre><code class="zig">@cmpxchg(ptr: &amp;T, cmp: T, new: T, success_order: AtomicOrder, fail_order: AtomicOrder) -&gt; bool</code></pre> | 4086 | <pre><code class="zig">@cmpxchg(ptr: &amp;T, cmp: T, new: T, success_order: AtomicOrder, fail_order: AtomicOrder) -&gt; bool</code></pre> |
| 4233 | <p> | 4087 | <p> |
| 4234 | This function performs an atomic compare exchange operation. | 4088 | This function performs an atomic compare exchange operation. |
| ... | @@ -4242,7 +4096,8 @@ comptime { | ... | @@ -4242,7 +4096,8 @@ comptime { |
| 4242 | <li><a href="#compile-variables">Compile Variables</a></li> | 4096 | <li><a href="#compile-variables">Compile Variables</a></li> |
| 4243 | </ul> | 4097 | </ul> |
| 4244 | | 4098 | |
| 4245 | <h3 id="builtin-compileError">@compileError</h3> | 4099 | {#header_close#} |
| | 4100 | {#header_open|@compileError#} |
| 4246 | <pre><code class="zig">@compileError(comptime msg: []u8)</code></pre> | 4101 | <pre><code class="zig">@compileError(comptime msg: []u8)</code></pre> |
| 4247 | <p> | 4102 | <p> |
| 4248 | This function, when semantically analyzed, causes a compile error with the | 4103 | This function, when semantically analyzed, causes a compile error with the |
| ... | @@ -4253,7 +4108,8 @@ comptime { | ... | @@ -4253,7 +4108,8 @@ comptime { |
| 4253 | using <code>if</code> or <code>switch</code> with compile time constants, | 4108 | using <code>if</code> or <code>switch</code> with compile time constants, |
| 4254 | and <code>comptime</code> functions. | 4109 | and <code>comptime</code> functions. |
| 4255 | </p> | 4110 | </p> |
| 4256 | <h3 id="builtin-compileLog">@compileLog</h3> | 4111 | {#header_close#} |
| | 4112 | {#header_open|@compileLog#} |
| 4257 | <pre><code class="zig">@compileLog(args: ...)</code></pre> | 4113 | <pre><code class="zig">@compileLog(args: ...)</code></pre> |
| 4258 | <p> | 4114 | <p> |
| 4259 | This function prints the arguments passed to it at compile-time. | 4115 | This function prints the arguments passed to it at compile-time. |
| ... | @@ -4303,7 +4159,7 @@ test.zig:6:2: error: found compile log statement | ... | @@ -4303,7 +4159,7 @@ test.zig:6:2: error: found compile log statement |
| 4303 | program compiles successfully and the generated executable prints: | 4159 | program compiles successfully and the generated executable prints: |
| 4304 | </p> | 4160 | </p> |
| 4305 | <pre><code class="sh">Runtime in main, num1 = 100.</code></pre> | 4161 | <pre><code class="sh">Runtime in main, num1 = 100.</code></pre> |
| 4306 | <h3 id="builtin-ctz">@ctz</h3> | 4162 | {{@ctheader_open:z}} |
| 4307 | <pre><code class="zig">@ctz(x: T) -&gt; U</code></pre> | 4163 | <pre><code class="zig">@ctz(x: T) -&gt; U</code></pre> |
| 4308 | <p> | 4164 | <p> |
| 4309 | This function counts the number of trailing zeroes in <code>x</code> which is an integer | 4165 | This function counts the number of trailing zeroes in <code>x</code> which is an integer |
| ... | @@ -4316,7 +4172,8 @@ test.zig:6:2: error: found compile log statement | ... | @@ -4316,7 +4172,8 @@ test.zig:6:2: error: found compile log statement |
| 4316 | <p> | 4172 | <p> |
| 4317 | If <code>x</code> is zero, <code>@ctz</code> returns <code>T.bit_count</code>. | 4173 | If <code>x</code> is zero, <code>@ctz</code> returns <code>T.bit_count</code>. |
| 4318 | </p> | 4174 | </p> |
| 4319 | <h3 id="builtin-divExact">@divExact</h3> | 4175 | {#header_close#} |
| | 4176 | {#header_open|@divExact#} |
| 4320 | <pre><code class="zig">@divExact(numerator: T, denominator: T) -&gt; T</code></pre> | 4177 | <pre><code class="zig">@divExact(numerator: T, denominator: T) -&gt; T</code></pre> |
| 4321 | <p> | 4178 | <p> |
| 4322 | Exact division. Caller guarantees <code>denominator != 0</code> and | 4179 | Exact division. Caller guarantees <code>denominator != 0</code> and |
| ... | @@ -4332,7 +4189,8 @@ test.zig:6:2: error: found compile log statement | ... | @@ -4332,7 +4189,8 @@ test.zig:6:2: error: found compile log statement |
| 4332 | <li><a href="#builtin-divFloor">@divFloor</a></li> | 4189 | <li><a href="#builtin-divFloor">@divFloor</a></li> |
| 4333 | <li><code>@import("std").math.divExact</code></li> | 4190 | <li><code>@import("std").math.divExact</code></li> |
| 4334 | </ul> | 4191 | </ul> |
| 4335 | <h3 id="builtin-divFloor">@divFloor</h3> | 4192 | {#header_close#} |
| | 4193 | {#header_open|@divFloor#} |
| 4336 | <pre><code class="zig">@divFloor(numerator: T, denominator: T) -&gt; T</code></pre> | 4194 | <pre><code class="zig">@divFloor(numerator: T, denominator: T) -&gt; T</code></pre> |
| 4337 | <p> | 4195 | <p> |
| 4338 | Floored division. Rounds toward negative infinity. For unsigned integers it is | 4196 | Floored division. Rounds toward negative infinity. For unsigned integers it is |
| ... | @@ -4349,7 +4207,8 @@ test.zig:6:2: error: found compile log statement | ... | @@ -4349,7 +4207,8 @@ test.zig:6:2: error: found compile log statement |
| 4349 | <li><a href="#builtin-divExact">@divExact</a></li> | 4207 | <li><a href="#builtin-divExact">@divExact</a></li> |
| 4350 | <li><code>@import("std").math.divFloor</code></li> | 4208 | <li><code>@import("std").math.divFloor</code></li> |
| 4351 | </ul> | 4209 | </ul> |
| 4352 | <h3 id="builtin-divTrunc">@divTrunc</h3> | 4210 | {#header_close#} |
| | 4211 | {#header_open|@divTrunc#} |
| 4353 | <pre><code class="zig">@divTrunc(numerator: T, denominator: T) -&gt; T</code></pre> | 4212 | <pre><code class="zig">@divTrunc(numerator: T, denominator: T) -&gt; T</code></pre> |
| 4354 | <p> | 4213 | <p> |
| 4355 | Truncated division. Rounds toward zero. For unsigned integers it is | 4214 | Truncated division. Rounds toward zero. For unsigned integers it is |
| ... | @@ -4366,7 +4225,8 @@ test.zig:6:2: error: found compile log statement | ... | @@ -4366,7 +4225,8 @@ test.zig:6:2: error: found compile log statement |
| 4366 | <li><a href="#builtin-divExact">@divExact</a></li> | 4225 | <li><a href="#builtin-divExact">@divExact</a></li> |
| 4367 | <li><code>@import("std").math.divTrunc</code></li> | 4226 | <li><code>@import("std").math.divTrunc</code></li> |
| 4368 | </ul> | 4227 | </ul> |
| 4369 | <h3 id="builtin-embedFile">@embedFile</h3> | 4228 | {#header_close#} |
| | 4229 | {#header_open|@embedFile#} |
| 4370 | <pre><code class="zig">@embedFile(comptime path: []const u8) -&gt; [X]u8</code></pre> | 4230 | <pre><code class="zig">@embedFile(comptime path: []const u8) -&gt; [X]u8</code></pre> |
| 4371 | <p> | 4231 | <p> |
| 4372 | This function returns a compile time constant fixed-size array with length | 4232 | This function returns a compile time constant fixed-size array with length |
| ... | @@ -4380,17 +4240,20 @@ test.zig:6:2: error: found compile log statement | ... | @@ -4380,17 +4240,20 @@ test.zig:6:2: error: found compile log statement |
| 4380 | <ul> | 4240 | <ul> |
| 4381 | <li><a href="#builtin-import">@import</a></li> | 4241 | <li><a href="#builtin-import">@import</a></li> |
| 4382 | </ul> | 4242 | </ul> |
| 4383 | <h3 id="builtin-export">@export</h3> | 4243 | {#header_close#} |
| | 4244 | {#header_open|@export#} |
| 4384 | <pre><code class="zig">@export(comptime name: []const u8, target: var, linkage: builtin.GlobalLinkage) -&gt; []const u8</code></pre> | 4245 | <pre><code class="zig">@export(comptime name: []const u8, target: var, linkage: builtin.GlobalLinkage) -&gt; []const u8</code></pre> |
| 4385 | <p> | 4246 | <p> |
| 4386 | Creates a symbol in the output object file. | 4247 | Creates a symbol in the output object file. |
| 4387 | </p> | 4248 | </p> |
| 4388 | <h3 id="builtin-tagName">@tagName</h3> | 4249 | {#header_close#} |
| | 4250 | {#header_open|@tagName#} |
| 4389 | <pre><code class="zig">@tagName(value: var) -&gt; []const u8</code></pre> | 4251 | <pre><code class="zig">@tagName(value: var) -&gt; []const u8</code></pre> |
| 4390 | <p> | 4252 | <p> |
| 4391 | Converts an enum value or union value to a slice of bytes representing the name. | 4253 | Converts an enum value or union value to a slice of bytes representing the name. |
| 4392 | </p> | 4254 | </p> |
| 4393 | <h3 id="builtin-TagType">@TagType</h3> | 4255 | {#header_close#} |
| | 4256 | {#header_open|@TagType#} |
| 4394 | <pre><code class="zig">@TagType(T: type) -&gt; type</code></pre> | 4257 | <pre><code class="zig">@TagType(T: type) -&gt; type</code></pre> |
| 4395 | <p> | 4258 | <p> |
| 4396 | For an enum, returns the integer type that is used to store the enumeration value. | 4259 | For an enum, returns the integer type that is used to store the enumeration value. |
| ... | @@ -4398,7 +4261,8 @@ test.zig:6:2: error: found compile log statement | ... | @@ -4398,7 +4261,8 @@ test.zig:6:2: error: found compile log statement |
| 4398 | <p> | 4261 | <p> |
| 4399 | For a union, returns the enum type that is used to store the tag value. | 4262 | For a union, returns the enum type that is used to store the tag value. |
| 4400 | </p> | 4263 | </p> |
| 4401 | <h3 id="builtin-errorName">@errorName</h3> | 4264 | {#header_close#} |
| | 4265 | {#header_open|@errorName#} |
| 4402 | <pre><code class="zig">@errorName(err: error) -&gt; []u8</code></pre> | 4266 | <pre><code class="zig">@errorName(err: error) -&gt; []u8</code></pre> |
| 4403 | <p> | 4267 | <p> |
| 4404 | This function returns the string representation of an error. If an error | 4268 | This function returns the string representation of an error. If an error |
| ... | @@ -4413,14 +4277,16 @@ test.zig:6:2: error: found compile log statement | ... | @@ -4413,14 +4277,16 @@ test.zig:6:2: error: found compile log statement |
| 4413 | or all calls have a compile-time known value for <code>err</code>, then no | 4277 | or all calls have a compile-time known value for <code>err</code>, then no |
| 4414 | error name table will be generated. | 4278 | error name table will be generated. |
| 4415 | </p> | 4279 | </p> |
| 4416 | <h3 id="builtin-errorReturnTrace">@errorReturnTrace</h3> | 4280 | {#header_close#} |
| | 4281 | {#header_open|@errorReturnTrace#} |
| 4417 | <pre><code class="zig">@errorReturnTrace() -&gt; ?&amp;builtin.StackTrace</code></pre> | 4282 | <pre><code class="zig">@errorReturnTrace() -&gt; ?&amp;builtin.StackTrace</code></pre> |
| 4418 | <p> | 4283 | <p> |
| 4419 | If the binary is built with error return tracing, and this function is invoked in a | 4284 | If the binary is built with error return tracing, and this function is invoked in a |
| 4420 | function that calls a function with an error or error union return type, returns a | 4285 | function that calls a function with an error or error union return type, returns a |
| 4421 | stack trace object. Otherwise returns `null`. | 4286 | stack trace object. Otherwise returns `null`. |
| 4422 | </p> | 4287 | </p> |
| 4423 | <h3 id="builtin-fence">@fence</h3> | 4288 | {#header_close#} |
| | 4289 | {#header_open|@fence#} |
| 4424 | <pre><code class="zig">@fence(order: AtomicOrder)</code></pre> | 4290 | <pre><code class="zig">@fence(order: AtomicOrder)</code></pre> |
| 4425 | <p> | 4291 | <p> |
| 4426 | The <code>fence</code> function is used to introduce happens-before edges between operations. | 4292 | The <code>fence</code> function is used to introduce happens-before edges between operations. |
| ... | @@ -4432,13 +4298,15 @@ test.zig:6:2: error: found compile log statement | ... | @@ -4432,13 +4298,15 @@ test.zig:6:2: error: found compile log statement |
| 4432 | <ul> | 4298 | <ul> |
| 4433 | <li><a href="#compile-variables">Compile Variables</a></li> | 4299 | <li><a href="#compile-variables">Compile Variables</a></li> |
| 4434 | </ul> | 4300 | </ul> |
| 4435 | <h3 id="builtin-fieldParentPtr">@fieldParentPtr</h3> | 4301 | {#header_close#} |
| | 4302 | {#header_open|@fieldParentPtr#} |
| 4436 | <pre><code class="zig">@fieldParentPtr(comptime ParentType: type, comptime field_name: []const u8, | 4303 | <pre><code class="zig">@fieldParentPtr(comptime ParentType: type, comptime field_name: []const u8, |
| 4437 | field_ptr: &amp;T) -&gt; &amp;ParentType</code></pre> | 4304 | field_ptr: &amp;T) -&gt; &amp;ParentType</code></pre> |
| 4438 | <p> | 4305 | <p> |
| 4439 | Given a pointer to a field, returns the base pointer of a struct. | 4306 | Given a pointer to a field, returns the base pointer of a struct. |
| 4440 | </p> | 4307 | </p> |
| 4441 | <h3 id="builtin-frameAddress">@frameAddress</h3> | 4308 | {#header_close#} |
| | 4309 | {#header_open|@frameAddress#} |
| 4442 | <pre><code class="zig">@frameAddress()</code></pre> | 4310 | <pre><code class="zig">@frameAddress()</code></pre> |
| 4443 | <p> | 4311 | <p> |
| 4444 | This function returns the base pointer of the current stack frame. | 4312 | This function returns the base pointer of the current stack frame. |
| ... | @@ -4451,7 +4319,8 @@ test.zig:6:2: error: found compile log statement | ... | @@ -4451,7 +4319,8 @@ test.zig:6:2: error: found compile log statement |
| 4451 | <p> | 4319 | <p> |
| 4452 | This function is only valid within function scope. | 4320 | This function is only valid within function scope. |
| 4453 | </p> | 4321 | </p> |
| 4454 | <h3 id="builtin-import">@import</h3> | 4322 | {#header_close#} |
| | 4323 | {#header_open|@import#} |
| 4455 | <pre><code class="zig">@import(comptime path: []u8) -&gt; (namespace)</code></pre> | 4324 | <pre><code class="zig">@import(comptime path: []u8) -&gt; (namespace)</code></pre> |
| 4456 | <p> | 4325 | <p> |
| 4457 | This function finds a zig file corresponding to <code>path</code> and imports all the | 4326 | This function finds a zig file corresponding to <code>path</code> and imports all the |
| ... | @@ -4474,7 +4343,8 @@ test.zig:6:2: error: found compile log statement | ... | @@ -4474,7 +4343,8 @@ test.zig:6:2: error: found compile log statement |
| 4474 | <li><a href="#compile-variables">Compile Variables</a></li> | 4343 | <li><a href="#compile-variables">Compile Variables</a></li> |
| 4475 | <li><a href="#builtin-embedFile">@embedFile</a></li> | 4344 | <li><a href="#builtin-embedFile">@embedFile</a></li> |
| 4476 | </ul> | 4345 | </ul> |
| 4477 | <h3 id="builtin-inlineCall">@inlineCall</h3> | 4346 | {#header_close#} |
| | 4347 | {#header_open|@inlineCall#} |
| 4478 | <pre><code class="zig">@inlineCall(function: X, args: ...) -&gt; Y</code></pre> | 4348 | <pre><code class="zig">@inlineCall(function: X, args: ...) -&gt; Y</code></pre> |
| 4479 | <p> | 4349 | <p> |
| 4480 | This calls a function, in the same way that invoking an expression with parentheses does: | 4350 | This calls a function, in the same way that invoking an expression with parentheses does: |
| ... | @@ -4493,17 +4363,20 @@ fn add(a: i32, b: i32) -&gt; i32 { a + b }</code></pre> | ... | @@ -4493,17 +4363,20 @@ fn add(a: i32, b: i32) -&gt; i32 { a + b }</code></pre> |
| 4493 | <ul> | 4363 | <ul> |
| 4494 | <li><a href="#builtin-noInlineCall">@noInlineCall</a></li> | 4364 | <li><a href="#builtin-noInlineCall">@noInlineCall</a></li> |
| 4495 | </ul> | 4365 | </ul> |
| 4496 | <h3 id="builtin-intToPtr">@intToPtr</h3> | 4366 | {#header_close#} |
| | 4367 | {#header_open|@intToPtr#} |
| 4497 | <pre><code class="zig">@intToPtr(comptime DestType: type, int: usize) -&gt; DestType</code></pre> | 4368 | <pre><code class="zig">@intToPtr(comptime DestType: type, int: usize) -&gt; DestType</code></pre> |
| 4498 | <p> | 4369 | <p> |
| 4499 | Converts an integer to a pointer. To convert the other way, use <a href="#builtin-ptrToInt">@ptrToInt</a>. | 4370 | Converts an integer to a pointer. To convert the other way, use <a href="#builtin-ptrToInt">@ptrToInt</a>. |
| 4500 | </p> | 4371 | </p> |
| 4501 | <h3 id="builtin-IntType">@IntType</h3> | 4372 | {#header_close#} |
| | 4373 | {#header_open|@IntType#} |
| 4502 | <pre><code class="zig">@IntType(comptime is_signed: bool, comptime bit_count: u8) -&gt; type</code></pre> | 4374 | <pre><code class="zig">@IntType(comptime is_signed: bool, comptime bit_count: u8) -&gt; type</code></pre> |
| 4503 | <p> | 4375 | <p> |
| 4504 | This function returns an integer type with the given signness and bit count. | 4376 | This function returns an integer type with the given signness and bit count. |
| 4505 | </p> | 4377 | </p> |
| 4506 | <h3 id="builtin-maxValue">@maxValue</h3> | 4378 | {#header_close#} |
| | 4379 | {#header_open|@maxValue#} |
| 4507 | <pre><code class="zig">@maxValue(comptime T: type) -&gt; (number literal)</code></pre> | 4380 | <pre><code class="zig">@maxValue(comptime T: type) -&gt; (number literal)</code></pre> |
| 4508 | <p> | 4381 | <p> |
| 4509 | This function returns the maximum value of the integer type <code>T</code>. | 4382 | This function returns the maximum value of the integer type <code>T</code>. |
| ... | @@ -4511,7 +4384,8 @@ fn add(a: i32, b: i32) -&gt; i32 { a + b }</code></pre> | ... | @@ -4511,7 +4384,8 @@ fn add(a: i32, b: i32) -&gt; i32 { a + b }</code></pre> |
| 4511 | <p> | 4384 | <p> |
| 4512 | The result is a compile time constant. | 4385 | The result is a compile time constant. |
| 4513 | </p> | 4386 | </p> |
| 4514 | <h3 id="builtin-memberCount">@memberCount</h3> | 4387 | {#header_close#} |
| | 4388 | {#header_open|@memberCount#} |
| 4515 | <pre><code class="zig">@memberCount(comptime T: type) -&gt; (number literal)</code></pre> | 4389 | <pre><code class="zig">@memberCount(comptime T: type) -&gt; (number literal)</code></pre> |
| 4516 | <p> | 4390 | <p> |
| 4517 | This function returns the number of enum values in an enum type. | 4391 | This function returns the number of enum values in an enum type. |
| ... | @@ -4519,11 +4393,14 @@ fn add(a: i32, b: i32) -&gt; i32 { a + b }</code></pre> | ... | @@ -4519,11 +4393,14 @@ fn add(a: i32, b: i32) -&gt; i32 { a + b }</code></pre> |
| 4519 | <p> | 4393 | <p> |
| 4520 | The result is a compile time constant. | 4394 | The result is a compile time constant. |
| 4521 | </p> | 4395 | </p> |
| 4522 | <h3 id="builtin-memberName">@memberName</h3> | 4396 | {#header_close#} |
| | 4397 | {#header_open|@memberName#} |
| 4523 | <p>TODO</p> | 4398 | <p>TODO</p> |
| 4524 | <h3 id="builtin-memberType">@memberType</h3> | 4399 | {#header_close#} |
| | 4400 | {#header_open|@memberType#} |
| 4525 | <p>TODO</p> | 4401 | <p>TODO</p> |
| 4526 | <h3 id="builtin-memcpy">@memcpy</h3> | 4402 | {#header_close#} |
| | 4403 | {#header_open|@memcpy#} |
| 4527 | <pre><code class="zig">@memcpy(noalias dest: &amp;u8, noalias source: &amp;const u8, byte_count: usize)</code></pre> | 4404 | <pre><code class="zig">@memcpy(noalias dest: &amp;u8, noalias source: &amp;const u8, byte_count: usize)</code></pre> |
| 4528 | <p> | 4405 | <p> |
| 4529 | This function copies bytes from one region of memory to another. <code>dest</code> and | 4406 | This function copies bytes from one region of memory to another. <code>dest</code> and |
| ... | @@ -4540,7 +4417,8 @@ fn add(a: i32, b: i32) -&gt; i32 { a + b }</code></pre> | ... | @@ -4540,7 +4417,8 @@ fn add(a: i32, b: i32) -&gt; i32 { a + b }</code></pre> |
| 4540 | <p>There is also a standard library function for this:</p> | 4417 | <p>There is also a standard library function for this:</p> |
| 4541 | <pre><code class="zig">const mem = @import("std").mem; | 4418 | <pre><code class="zig">const mem = @import("std").mem; |
| 4542 | mem.copy(u8, dest[0...byte_count], source[0...byte_count]);</code></pre> | 4419 | mem.copy(u8, dest[0...byte_count], source[0...byte_count]);</code></pre> |
| 4543 | <h3 id="builtin-memset">@memset</h3> | 4420 | {#header_close#} |
| | 4421 | {#header_open|@memset#} |
| 4544 | <pre><code class="zig">@memset(dest: &amp;u8, c: u8, byte_count: usize)</code></pre> | 4422 | <pre><code class="zig">@memset(dest: &amp;u8, c: u8, byte_count: usize)</code></pre> |
| 4545 | <p> | 4423 | <p> |
| 4546 | This function sets a region of memory to <code>c</code>. <code>dest</code> is a pointer. | 4424 | This function sets a region of memory to <code>c</code>. <code>dest</code> is a pointer. |
| ... | @@ -4556,7 +4434,8 @@ mem.copy(u8, dest[0...byte_count], source[0...byte_count]);</code></pre> | ... | @@ -4556,7 +4434,8 @@ mem.copy(u8, dest[0...byte_count], source[0...byte_count]);</code></pre> |
| 4556 | <p>There is also a standard library function for this:</p> | 4434 | <p>There is also a standard library function for this:</p> |
| 4557 | <pre><code>const mem = @import("std").mem; | 4435 | <pre><code>const mem = @import("std").mem; |
| 4558 | mem.set(u8, dest, c);</code></pre> | 4436 | mem.set(u8, dest, c);</code></pre> |
| 4559 | <h3 id="builtin-minValue">@minValue</h3> | 4437 | {#header_close#} |
| | 4438 | {#header_open|@minValue#} |
| 4560 | <pre><code class="zig">@minValue(comptime T: type) -&gt; (number literal)</code></pre> | 4439 | <pre><code class="zig">@minValue(comptime T: type) -&gt; (number literal)</code></pre> |
| 4561 | <p> | 4440 | <p> |
| 4562 | This function returns the minimum value of the integer type T. | 4441 | This function returns the minimum value of the integer type T. |
| ... | @@ -4564,7 +4443,8 @@ mem.set(u8, dest, c);</code></pre> | ... | @@ -4564,7 +4443,8 @@ mem.set(u8, dest, c);</code></pre> |
| 4564 | <p> | 4443 | <p> |
| 4565 | The result is a compile time constant. | 4444 | The result is a compile time constant. |
| 4566 | </p> | 4445 | </p> |
| 4567 | <h3 id="builtin-mod">@mod</h3> | 4446 | {#header_close#} |
| | 4447 | {#header_open|@mod#} |
| 4568 | <pre><code class="zig">@mod(numerator: T, denominator: T) -&gt; T</code></pre> | 4448 | <pre><code class="zig">@mod(numerator: T, denominator: T) -&gt; T</code></pre> |
| 4569 | <p> | 4449 | <p> |
| 4570 | Modulus division. For unsigned integers this is the same as | 4450 | Modulus division. For unsigned integers this is the same as |
| ... | @@ -4579,14 +4459,16 @@ mem.set(u8, dest, c);</code></pre> | ... | @@ -4579,14 +4459,16 @@ mem.set(u8, dest, c);</code></pre> |
| 4579 | <li><a href="#builtin-rem">@rem</a></li> | 4459 | <li><a href="#builtin-rem">@rem</a></li> |
| 4580 | <li><code>@import("std").math.mod</code></li> | 4460 | <li><code>@import("std").math.mod</code></li> |
| 4581 | </ul> | 4461 | </ul> |
| 4582 | <h3 id="builtin-mulWithOverflow">@mulWithOverflow</h3> | 4462 | {#header_close#} |
| | 4463 | {#header_open|@mulWithOverflow#} |
| 4583 | <pre><code class="zig">@mulWithOverflow(comptime T: type, a: T, b: T, result: &amp;T) -&gt; bool</code></pre> | 4464 | <pre><code class="zig">@mulWithOverflow(comptime T: type, a: T, b: T, result: &amp;T) -&gt; bool</code></pre> |
| 4584 | <p> | 4465 | <p> |
| 4585 | Performs <code>*result = a * b</code>. If overflow or underflow occurs, | 4466 | Performs <code>*result = a * b</code>. If overflow or underflow occurs, |
| 4586 | stores the overflowed bits in <code>result</code> and returns <code>true</code>. | 4467 | stores the overflowed bits in <code>result</code> and returns <code>true</code>. |
| 4587 | If no overflow or underflow occurs, returns <code>false</code>. | 4468 | If no overflow or underflow occurs, returns <code>false</code>. |
| 4588 | </p> | 4469 | </p> |
| 4589 | <h3 id="builtin-noInlineCall">@noInlineCall</h3> | 4470 | {#header_close#} |
| | 4471 | {#header_open|@noInlineCall#} |
| 4590 | <pre><code class="zig">@noInlineCall(function: var, args: ...) -&gt; var</code></pre> | 4472 | <pre><code class="zig">@noInlineCall(function: var, args: ...) -&gt; var</code></pre> |
| 4591 | <p> | 4473 | <p> |
| 4592 | This calls a function, in the same way that invoking an expression with parentheses does: | 4474 | This calls a function, in the same way that invoking an expression with parentheses does: |
| ... | @@ -4605,12 +4487,14 @@ fn add(a: i32, b: i32) -&gt; i32 { a + b }</code></pre> | ... | @@ -4605,12 +4487,14 @@ fn add(a: i32, b: i32) -&gt; i32 { a + b }</code></pre> |
| 4605 | <ul> | 4487 | <ul> |
| 4606 | <li><a href="#builtin-inlineCall">@inlineCall</a></li> | 4488 | <li><a href="#builtin-inlineCall">@inlineCall</a></li> |
| 4607 | </ul> | 4489 | </ul> |
| 4608 | <h3 id="builtin-offsetOf">@offsetOf</h3> | 4490 | {#header_close#} |
| | 4491 | {#header_open|@offsetOf#} |
| 4609 | <pre><code class="zig">@offsetOf(comptime T: type, comptime field_name: [] const u8) -&gt; (number literal)</code></pre> | 4492 | <pre><code class="zig">@offsetOf(comptime T: type, comptime field_name: [] const u8) -&gt; (number literal)</code></pre> |
| 4610 | <p> | 4493 | <p> |
| 4611 | This function returns the byte offset of a field relative to its containing struct. | 4494 | This function returns the byte offset of a field relative to its containing struct. |
| 4612 | </p> | 4495 | </p> |
| 4613 | <h3 id="builtin-OpaqueType">@OpaqueType</h3> | 4496 | {#header_close#} |
| | 4497 | {#header_open|@OpaqueType#} |
| 4614 | <pre><code class="zig">@OpaqueType() -&gt; type</code></pre> | 4498 | <pre><code class="zig">@OpaqueType() -&gt; type</code></pre> |
| 4615 | <p> | 4499 | <p> |
| 4616 | Creates a new type with an unknown size and alignment. | 4500 | Creates a new type with an unknown size and alignment. |
| ... | @@ -4630,7 +4514,8 @@ export fn foo(w: &amp;Wat) { | ... | @@ -4630,7 +4514,8 @@ export fn foo(w: &amp;Wat) { |
| 4630 | test.zig:5:9: error: expected type '&amp;Derp', found '&amp;Wat' | 4514 | test.zig:5:9: error: expected type '&amp;Derp', found '&amp;Wat' |
| 4631 | bar(w); | 4515 | bar(w); |
| 4632 | ^</code></pre> | 4516 | ^</code></pre> |
| 4633 | <h3 id="builtin-panic">@panic</h3> | 4517 | {#header_close#} |
| | 4518 | {#header_open|@panic#} |
| 4634 | <pre><code class="zig">@panic(message: []const u8) -&gt; noreturn</code></pre> | 4519 | <pre><code class="zig">@panic(message: []const u8) -&gt; noreturn</code></pre> |
| 4635 | <p> | 4520 | <p> |
| 4636 | Invokes the panic handler function. By default the panic handler function | 4521 | Invokes the panic handler function. By default the panic handler function |
| ... | @@ -4649,12 +4534,14 @@ test.zig:5:9: error: expected type '&amp;Derp', found '&amp;Wat' | ... | @@ -4649,12 +4534,14 @@ test.zig:5:9: error: expected type '&amp;Derp', found '&amp;Wat' |
| 4649 | <li><a href="#root-source-file">Root Source File</a></li> | 4534 | <li><a href="#root-source-file">Root Source File</a></li> |
| 4650 | </ul> | 4535 | </ul> |
| 4651 | | 4536 | |
| 4652 | <h3 id="builtin-ptrCast">@ptrCast</h3> | 4537 | {#header_close#} |
| | 4538 | {#header_open|@ptrCast#} |
| 4653 | <pre><code class="zig">@ptrCast(comptime DestType: type, value: var) -&gt; DestType</code></pre> | 4539 | <pre><code class="zig">@ptrCast(comptime DestType: type, value: var) -&gt; DestType</code></pre> |
| 4654 | <p> | 4540 | <p> |
| 4655 | Converts a pointer of one type to a pointer of another type. | 4541 | Converts a pointer of one type to a pointer of another type. |
| 4656 | </p> | 4542 | </p> |
| 4657 | <h3 id="builtin-ptrToInt">@ptrToInt</h3> | 4543 | {#header_close#} |
| | 4544 | {#header_open|@ptrToInt#} |
| 4658 | <pre><code class="zig">@ptrToInt(value: var) -&gt; usize</code></pre> | 4545 | <pre><code class="zig">@ptrToInt(value: var) -&gt; usize</code></pre> |
| 4659 | <p> | 4546 | <p> |
| 4660 | Converts <code>value</code> to a <code>usize</code> which is the address of the pointer. <code>value</code> can be one of these types: | 4547 | Converts <code>value</code> to a <code>usize</code> which is the address of the pointer. <code>value</code> can be one of these types: |
| ... | @@ -4667,7 +4554,8 @@ test.zig:5:9: error: expected type '&amp;Derp', found '&amp;Wat' | ... | @@ -4667,7 +4554,8 @@ test.zig:5:9: error: expected type '&amp;Derp', found '&amp;Wat' |
| 4667 | </ul> | 4554 | </ul> |
| 4668 | <p>To convert the other way, use <a href="#builtin-intToPtr">@intToPtr</a></p> | 4555 | <p>To convert the other way, use <a href="#builtin-intToPtr">@intToPtr</a></p> |
| 4669 | | 4556 | |
| 4670 | <h3 id="builtin-rem">@rem</h3> | 4557 | {#header_close#} |
| | 4558 | {#header_open|@rem#} |
| 4671 | <pre><code class="zig">@rem(numerator: T, denominator: T) -&gt; T</code></pre> | 4559 | <pre><code class="zig">@rem(numerator: T, denominator: T) -&gt; T</code></pre> |
| 4672 | <p> | 4560 | <p> |
| 4673 | Remainder division. For unsigned integers this is the same as | 4561 | Remainder division. For unsigned integers this is the same as |
| ... | @@ -4682,7 +4570,8 @@ test.zig:5:9: error: expected type '&amp;Derp', found '&amp;Wat' | ... | @@ -4682,7 +4570,8 @@ test.zig:5:9: error: expected type '&amp;Derp', found '&amp;Wat' |
| 4682 | <li><a href="#builtin-mod">@mod</a></li> | 4570 | <li><a href="#builtin-mod">@mod</a></li> |
| 4683 | <li><code>@import("std").math.rem</code></li> | 4571 | <li><code>@import("std").math.rem</code></li> |
| 4684 | </ul> | 4572 | </ul> |
| 4685 | <h3 id="builtin-returnAddress">@returnAddress</h3> | 4573 | {#header_close#} |
| | 4574 | {#header_open|@returnAddress#} |
| 4686 | <pre><code class="zig">@returnAddress()</code></pre> | 4575 | <pre><code class="zig">@returnAddress()</code></pre> |
| 4687 | <p> | 4576 | <p> |
| 4688 | This function returns a pointer to the return address of the current stack | 4577 | This function returns a pointer to the return address of the current stack |
| ... | @@ -4696,13 +4585,15 @@ test.zig:5:9: error: expected type '&amp;Derp', found '&amp;Wat' | ... | @@ -4696,13 +4585,15 @@ test.zig:5:9: error: expected type '&amp;Derp', found '&amp;Wat' |
| 4696 | This function is only valid within function scope. | 4585 | This function is only valid within function scope. |
| 4697 | </p> | 4586 | </p> |
| 4698 | | 4587 | |
| 4699 | <h3 id="builtin-setDebugSafety">@setDebugSafety</h3> | 4588 | {#header_close#} |
| | 4589 | {#header_open|@setDebugSafety#} |
| 4700 | <pre><code class="zig">@setDebugSafety(scope, safety_on: bool)</code></pre> | 4590 | <pre><code class="zig">@setDebugSafety(scope, safety_on: bool)</code></pre> |
| 4701 | <p> | 4591 | <p> |
| 4702 | Sets whether debug safety checks are on for a given scope. | 4592 | Sets whether debug safety checks are on for a given scope. |
| 4703 | </p> | 4593 | </p> |
| 4704 | | 4594 | |
| 4705 | <h3 id="builtin-setEvalBranchQuota">@setEvalBranchQuota</h3> | 4595 | {#header_close#} |
| | 4596 | {#header_open|@setEvalBranchQuota#} |
| 4706 | <pre><code class="zig">@setEvalBranchQuota(new_quota: usize)</code></pre> | 4597 | <pre><code class="zig">@setEvalBranchQuota(new_quota: usize)</code></pre> |
| 4707 | <p> | 4598 | <p> |
| 4708 | Changes the maximum number of backwards branches that compile-time code | 4599 | Changes the maximum number of backwards branches that compile-time code |
| ... | @@ -4737,7 +4628,8 @@ test.zig:5:9: error: expected type '&amp;Derp', found '&amp;Wat' | ... | @@ -4737,7 +4628,8 @@ test.zig:5:9: error: expected type '&amp;Derp', found '&amp;Wat' |
| 4737 | <li><a href="#comptime">comptime</a></li> | 4628 | <li><a href="#comptime">comptime</a></li> |
| 4738 | </ul> | 4629 | </ul> |
| 4739 | | 4630 | |
| 4740 | <h3 id="builtin-setFloatMode">@setFloatMode</h3> | 4631 | {#header_close#} |
| | 4632 | {#header_open|@setFloatMode#} |
| 4741 | <pre><code class="zig">@setFloatMode(scope, mode: @import("builtin").FloatMode)</code></pre> | 4633 | <pre><code class="zig">@setFloatMode(scope, mode: @import("builtin").FloatMode)</code></pre> |
| 4742 | <p> | 4634 | <p> |
| 4743 | Sets the floating point mode for a given scope. Possible values are: | 4635 | Sets the floating point mode for a given scope. Possible values are: |
| ... | @@ -4768,7 +4660,8 @@ test.zig:5:9: error: expected type '&amp;Derp', found '&amp;Wat' | ... | @@ -4768,7 +4660,8 @@ test.zig:5:9: error: expected type '&amp;Derp', found '&amp;Wat' |
| 4768 | <li><a href="#float-operations">Floating Point Operations</a></li> | 4660 | <li><a href="#float-operations">Floating Point Operations</a></li> |
| 4769 | </ul> | 4661 | </ul> |
| 4770 | | 4662 | |
| 4771 | <h3 id="builtin-setGlobalLinkage">@setGlobalLinkage</h3> | 4663 | {#header_close#} |
| | 4664 | {#header_open|@setGlobalLinkage#} |
| 4772 | <pre><code class="zig">@setGlobalLinkage(global_variable_name, comptime linkage: GlobalLinkage)</code></pre> | 4665 | <pre><code class="zig">@setGlobalLinkage(global_variable_name, comptime linkage: GlobalLinkage)</code></pre> |
| 4773 | <p> | 4666 | <p> |
| 4774 | <code>GlobalLinkage</code> can be found with <code>@import("builtin").GlobalLinkage</code>. | 4667 | <code>GlobalLinkage</code> can be found with <code>@import("builtin").GlobalLinkage</code>. |
| ... | @@ -4777,12 +4670,14 @@ test.zig:5:9: error: expected type '&amp;Derp', found '&amp;Wat' | ... | @@ -4777,12 +4670,14 @@ test.zig:5:9: error: expected type '&amp;Derp', found '&amp;Wat' |
| 4777 | <ul> | 4670 | <ul> |
| 4778 | <li><a href="#compile-variables">Compile Variables</a></li> | 4671 | <li><a href="#compile-variables">Compile Variables</a></li> |
| 4779 | </ul> | 4672 | </ul> |
| 4780 | <h3 id="builtin-setGlobalSection">@setGlobalSection</h3> | 4673 | {#header_close#} |
| | 4674 | {#header_open|@setGlobalSection#} |
| 4781 | <pre><code class="zig">@setGlobalSection(global_variable_name, comptime section_name: []const u8) -&gt; bool</code></pre> | 4675 | <pre><code class="zig">@setGlobalSection(global_variable_name, comptime section_name: []const u8) -&gt; bool</code></pre> |
| 4782 | <p> | 4676 | <p> |
| 4783 | Puts the global variable in the specified section. | 4677 | Puts the global variable in the specified section. |
| 4784 | </p> | 4678 | </p> |
| 4785 | <h3 id="builtin-shlExact">@shlExact</h3> | 4679 | {#header_close#} |
| | 4680 | {#header_open|@shlExact#} |
| 4786 | <pre><code class="zig">@shlExact(value: T, shift_amt: Log2T) -&gt; T</code></pre> | 4681 | <pre><code class="zig">@shlExact(value: T, shift_amt: Log2T) -&gt; T</code></pre> |
| 4787 | <p> | 4682 | <p> |
| 4788 | Performs the left shift operation (<code>&lt;&lt;</code>). Caller guarantees | 4683 | Performs the left shift operation (<code>&lt;&lt;</code>). Caller guarantees |
| ... | @@ -4797,7 +4692,8 @@ test.zig:5:9: error: expected type '&amp;Derp', found '&amp;Wat' | ... | @@ -4797,7 +4692,8 @@ test.zig:5:9: error: expected type '&amp;Derp', found '&amp;Wat' |
| 4797 | <li><a href="#builtin-shrExact">@shrExact</a></li> | 4692 | <li><a href="#builtin-shrExact">@shrExact</a></li> |
| 4798 | <li><a href="#builtin-shlWithOverflow">@shlWithOverflow</a></li> | 4693 | <li><a href="#builtin-shlWithOverflow">@shlWithOverflow</a></li> |
| 4799 | </ul> | 4694 | </ul> |
| 4800 | <h3 id="builtin-shlWithOverflow">@shlWithOverflow</h3> | 4695 | {#header_close#} |
| | 4696 | {#header_open|@shlWithOverflow#} |
| 4801 | <pre><code class="zig">@shlWithOverflow(comptime T: type, a: T, shift_amt: Log2T, result: &amp;T) -&gt; bool</code></pre> | 4697 | <pre><code class="zig">@shlWithOverflow(comptime T: type, a: T, shift_amt: Log2T, result: &amp;T) -&gt; bool</code></pre> |
| 4802 | <p> | 4698 | <p> |
| 4803 | Performs <code>*result = a &lt;&lt; b</code>. If overflow or underflow occurs, | 4699 | Performs <code>*result = a &lt;&lt; b</code>. If overflow or underflow occurs, |
| ... | @@ -4813,7 +4709,8 @@ test.zig:5:9: error: expected type '&amp;Derp', found '&amp;Wat' | ... | @@ -4813,7 +4709,8 @@ test.zig:5:9: error: expected type '&amp;Derp', found '&amp;Wat' |
| 4813 | <li><a href="#builtin-shlExact">@shlExact</a></li> | 4709 | <li><a href="#builtin-shlExact">@shlExact</a></li> |
| 4814 | <li><a href="#builtin-shrExact">@shrExact</a></li> | 4710 | <li><a href="#builtin-shrExact">@shrExact</a></li> |
| 4815 | </ul> | 4711 | </ul> |
| 4816 | <h3 id="builtin-shrExact">@shrExact</h3> | 4712 | {#header_close#} |
| | 4713 | {#header_open|@shrExact#} |
| 4817 | <pre><code class="zig">@shrExact(value: T, shift_amt: Log2T) -&gt; T</code></pre> | 4714 | <pre><code class="zig">@shrExact(value: T, shift_amt: Log2T) -&gt; T</code></pre> |
| 4818 | <p> | 4715 | <p> |
| 4819 | Performs the right shift operation (<code>&gt;&gt;</code>). Caller guarantees | 4716 | Performs the right shift operation (<code>&gt;&gt;</code>). Caller guarantees |
| ... | @@ -4827,7 +4724,8 @@ test.zig:5:9: error: expected type '&amp;Derp', found '&amp;Wat' | ... | @@ -4827,7 +4724,8 @@ test.zig:5:9: error: expected type '&amp;Derp', found '&amp;Wat' |
| 4827 | <ul> | 4724 | <ul> |
| 4828 | <li><a href="#builtin-shlExact">@shlExact</a></li> | 4725 | <li><a href="#builtin-shlExact">@shlExact</a></li> |
| 4829 | </ul> | 4726 | </ul> |
| 4830 | <h3 id="builtin-sizeOf">@sizeOf</h3> | 4727 | {#header_close#} |
| | 4728 | {#header_open|@sizeOf#} |
| 4831 | <pre><code class="zig">@sizeOf(comptime T: type) -&gt; (number literal)</code></pre> | 4729 | <pre><code class="zig">@sizeOf(comptime T: type) -&gt; (number literal)</code></pre> |
| 4832 | <p> | 4730 | <p> |
| 4833 | This function returns the number of bytes it takes to store <code>T</code> in memory. | 4731 | This function returns the number of bytes it takes to store <code>T</code> in memory. |
| ... | @@ -4835,14 +4733,16 @@ test.zig:5:9: error: expected type '&amp;Derp', found '&amp;Wat' | ... | @@ -4835,14 +4733,16 @@ test.zig:5:9: error: expected type '&amp;Derp', found '&amp;Wat' |
| 4835 | <p> | 4733 | <p> |
| 4836 | The result is a target-specific compile time constant. | 4734 | The result is a target-specific compile time constant. |
| 4837 | </p> | 4735 | </p> |
| 4838 | <h3 id="builtin-subWithOverflow">@subWithOverflow</h3> | 4736 | {#header_close#} |
| | 4737 | {#header_open|@subWithOverflow#} |
| 4839 | <pre><code class="zig">@subWithOverflow(comptime T: type, a: T, b: T, result: &amp;T) -&gt; bool</code></pre> | 4738 | <pre><code class="zig">@subWithOverflow(comptime T: type, a: T, b: T, result: &amp;T) -&gt; bool</code></pre> |
| 4840 | <p> | 4739 | <p> |
| 4841 | Performs <code>*result = a - b</code>. If overflow or underflow occurs, | 4740 | Performs <code>*result = a - b</code>. If overflow or underflow occurs, |
| 4842 | stores the overflowed bits in <code>result</code> and returns <code>true</code>. | 4741 | stores the overflowed bits in <code>result</code> and returns <code>true</code>. |
| 4843 | If no overflow or underflow occurs, returns <code>false</code>. | 4742 | If no overflow or underflow occurs, returns <code>false</code>. |
| 4844 | </p> | 4743 | </p> |
| 4845 | <h3 id="builtin-truncate">@truncate</h3> | 4744 | {#header_close#} |
| | 4745 | {#header_open|@truncate#} |
| 4846 | <pre><code class="zig">@truncate(comptime T: type, integer) -&gt; T</code></pre> | 4746 | <pre><code class="zig">@truncate(comptime T: type, integer) -&gt; T</code></pre> |
| 4847 | <p> | 4747 | <p> |
| 4848 | This function truncates bits from an integer type, resulting in a smaller | 4748 | This function truncates bits from an integer type, resulting in a smaller |
| ... | @@ -4865,7 +4765,8 @@ const b: u8 = @truncate(u8, a); | ... | @@ -4865,7 +4765,8 @@ const b: u8 = @truncate(u8, a); |
| 4865 | of endianness on the target platform. | 4765 | of endianness on the target platform. |
| 4866 | </p> | 4766 | </p> |
| 4867 | | 4767 | |
| 4868 | <h3 id="builtin-typeId">@typeId</h3> | 4768 | {#header_close#} |
| | 4769 | {#header_open|@typeId#} |
| 4869 | <pre><code class="zig">@typeId(comptime T: type) -&gt; @import("builtin").TypeId</code></pre> | 4770 | <pre><code class="zig">@typeId(comptime T: type) -&gt; @import("builtin").TypeId</code></pre> |
| 4870 | <p> | 4771 | <p> |
| 4871 | Returns which kind of type something is. Possible values: | 4772 | Returns which kind of type something is. Possible values: |
| ... | @@ -4898,20 +4799,24 @@ const b: u8 = @truncate(u8, a); | ... | @@ -4898,20 +4799,24 @@ const b: u8 = @truncate(u8, a); |
| 4898 | Opaque, | 4799 | Opaque, |
| 4899 | };</code></pre> | 4800 | };</code></pre> |
| 4900 | | 4801 | |
| 4901 | <h3 id="builtin-typeName">@typeName</h3> | 4802 | {#header_close#} |
| | 4803 | {#header_open|@typeName#} |
| 4902 | <pre><code class="zig">@typeName(T: type) -&gt; []u8</code></pre> | 4804 | <pre><code class="zig">@typeName(T: type) -&gt; []u8</code></pre> |
| 4903 | <p> | 4805 | <p> |
| 4904 | This function returns the string representation of a type. | 4806 | This function returns the string representation of a type. |
| 4905 | </p> | 4807 | </p> |
| 4906 | | 4808 | |
| 4907 | <h3 id="builtin-typeOf">@typeOf</h3> | 4809 | {#header_close#} |
| | 4810 | {#header_open|@typeOf#} |
| 4908 | <pre><code class="zig">@typeOf(expression) -&gt; type</code></pre> | 4811 | <pre><code class="zig">@typeOf(expression) -&gt; type</code></pre> |
| 4909 | <p> | 4812 | <p> |
| 4910 | This function returns a compile-time constant, which is the type of the | 4813 | This function returns a compile-time constant, which is the type of the |
| 4911 | expression passed as an argument. The expression is evaluated. | 4814 | expression passed as an argument. The expression is evaluated. |
| 4912 | </p> | 4815 | </p> |
| 4913 | | 4816 | |
| 4914 | <h2 id="build-mode">Build Mode</h2> | 4817 | {#header_close#} |
| | 4818 | {#header_close#} |
| | 4819 | {#header_open|Build Mode#} |
| 4915 | <p> | 4820 | <p> |
| 4916 | Zig has three build modes: | 4821 | Zig has three build modes: |
| 4917 | </p> | 4822 | </p> |
| ... | @@ -4935,21 +4840,23 @@ pub fn build(b: &amp;Builder) { | ... | @@ -4935,21 +4840,23 @@ pub fn build(b: &amp;Builder) { |
| 4935 | </p> | 4840 | </p> |
| 4936 | <pre><code class="sh"> -Drelease-safe=(bool) optimizations on and safety on | 4841 | <pre><code class="sh"> -Drelease-safe=(bool) optimizations on and safety on |
| 4937 | -Drelease-fast=(bool) optimizations on and safety off</code></pre> | 4842 | -Drelease-fast=(bool) optimizations on and safety off</code></pre> |
| 4938 | <h3 id="build-mode-debug">Debug</h2> | 4843 | {#header_open|Debug#} |
| 4939 | <pre><code class="sh">$ zig build-exe example.zig</code></pre> | 4844 | <pre><code class="sh">$ zig build-exe example.zig</code></pre> |
| 4940 | <ul> | 4845 | <ul> |
| 4941 | <li>Fast compilation speed</li> | 4846 | <li>Fast compilation speed</li> |
| 4942 | <li>Safety checks enabled</li> | 4847 | <li>Safety checks enabled</li> |
| 4943 | <li>Slow runtime performance</li> | 4848 | <li>Slow runtime performance</li> |
| 4944 | </ul> | 4849 | </ul> |
| 4945 | <h3 id="build-mode-release-fast">ReleaseFast</h2> | 4850 | {#header_close#} |
| | 4851 | {#header_open|ReleaseFast#} |
| 4946 | <pre><code class="sh">$ zig build-exe example.zig --release-fast</code></pre> | 4852 | <pre><code class="sh">$ zig build-exe example.zig --release-fast</code></pre> |
| 4947 | <ul> | 4853 | <ul> |
| 4948 | <li>Fast runtime performance</li> | 4854 | <li>Fast runtime performance</li> |
| 4949 | <li>Safety checks disabled</li> | 4855 | <li>Safety checks disabled</li> |
| 4950 | <li>Slow compilation speed</li> | 4856 | <li>Slow compilation speed</li> |
| 4951 | </ul> | 4857 | </ul> |
| 4952 | <h3 id="build-mode-release-safe">ReleaseSafe</h2> | 4858 | {#header_close#} |
| | 4859 | {#header_open|ReleaseSafe#} |
| 4953 | <pre><code class="sh">$ zig build-exe example.zig --release-safe</code></pre> | 4860 | <pre><code class="sh">$ zig build-exe example.zig --release-safe</code></pre> |
| 4954 | <ul> | 4861 | <ul> |
| 4955 | <li>Medium runtime performance</li> | 4862 | <li>Medium runtime performance</li> |
| ... | @@ -4962,7 +4869,9 @@ pub fn build(b: &amp;Builder) { | ... | @@ -4962,7 +4869,9 @@ pub fn build(b: &amp;Builder) { |
| 4962 | <li><a href="#zig-build-system">Zig Build System</a></li> | 4869 | <li><a href="#zig-build-system">Zig Build System</a></li> |
| 4963 | <li><a href="#undefined-behavior">Undefined Behavior</a></li> | 4870 | <li><a href="#undefined-behavior">Undefined Behavior</a></li> |
| 4964 | </ul> | 4871 | </ul> |
| 4965 | <h2 id="undefined-behavior">Undefined Behavior</h2> | 4872 | {#header_close#} |
| | 4873 | {#header_close#} |
| | 4874 | {#header_open|Undefined Behavior#} |
| 4966 | <p> | 4875 | <p> |
| 4967 | Zig has many instances of undefined behavior. If undefined behavior is | 4876 | Zig has many instances of undefined behavior. If undefined behavior is |
| 4968 | detected at compile-time, Zig emits an error. Most undefined behavior that | 4877 | detected at compile-time, Zig emits an error. Most undefined behavior that |
| ... | @@ -5000,7 +4909,7 @@ Test 1/1 safety check...reached unreachable code | ... | @@ -5000,7 +4909,7 @@ Test 1/1 safety check...reached unreachable code |
| 5000 | | 4909 | |
| 5001 | Tests failed. Use the following command to reproduce the failure: | 4910 | Tests failed. Use the following command to reproduce the failure: |
| 5002 | ./test</code></pre> | 4911 | ./test</code></pre> |
| 5003 | <h3 id="undef-unreachable">Reaching Unreachable Code</h3> | 4912 | {#header_open|Reaching Unreachable Code#} |
| 5004 | <p>At compile-time:</p> | 4913 | <p>At compile-time:</p> |
| 5005 | <pre><code class="zig">comptime { | 4914 | <pre><code class="zig">comptime { |
| 5006 | assert(false); | 4915 | assert(false); |
| ... | @@ -5019,7 +4928,8 @@ fn assert(ok: bool) { | ... | @@ -5019,7 +4928,8 @@ fn assert(ok: bool) { |
| 5019 | comptime { | 4928 | comptime { |
| 5020 | ^</code></pre> | 4929 | ^</code></pre> |
| 5021 | <p>At runtime crashes with the message <code>reached unreachable code</code> and a stack trace.</p> | 4930 | <p>At runtime crashes with the message <code>reached unreachable code</code> and a stack trace.</p> |
| 5022 | <h3 id="undef-index-out-of-bounds">Index out of Bounds</h3> | 4931 | {#header_close#} |
| | 4932 | {#header_open|Index out of Bounds#} |
| 5023 | <p>At compile-time:</p> | 4933 | <p>At compile-time:</p> |
| 5024 | <pre><code class="zig">comptime { | 4934 | <pre><code class="zig">comptime { |
| 5025 | const array = "hello"; | 4935 | const array = "hello"; |
| ... | @@ -5030,7 +4940,8 @@ comptime { | ... | @@ -5030,7 +4940,8 @@ comptime { |
| 5030 | const garbage = array[5]; | 4940 | const garbage = array[5]; |
| 5031 | ^</code></pre> | 4941 | ^</code></pre> |
| 5032 | <p>At runtime crashes with the message <code>index out of bounds</code> and a stack trace.</p> | 4942 | <p>At runtime crashes with the message <code>index out of bounds</code> and a stack trace.</p> |
| 5033 | <h3 id="undef-cast-negative-unsigned">Cast Negative Number to Unsigned Integer</h3> | 4943 | {#header_close#} |
| | 4944 | {#header_open|Cast Negative Number to Unsigned Integer#} |
| 5034 | <p>At compile-time:</p> | 4945 | <p>At compile-time:</p> |
| 5035 | <pre><code class="zig">comptime { | 4946 | <pre><code class="zig">comptime { |
| 5036 | const value: i32 = -1; | 4947 | const value: i32 = -1; |
| ... | @@ -5044,7 +4955,8 @@ comptime { | ... | @@ -5044,7 +4955,8 @@ comptime { |
| 5044 | If you are trying to obtain the maximum value of an unsigned integer, use <code>@maxValue(T)</code>, | 4955 | If you are trying to obtain the maximum value of an unsigned integer, use <code>@maxValue(T)</code>, |
| 5045 | where <code>T</code> is the integer type, such as <code>u32</code>. | 4956 | where <code>T</code> is the integer type, such as <code>u32</code>. |
| 5046 | </p> | 4957 | </p> |
| 5047 | <h3 id="undef-cast-truncates-data">Cast Truncates Data</h3> | 4958 | {#header_close#} |
| | 4959 | {#header_open|Cast Truncates Data#} |
| 5048 | <p>At compile-time:</p> | 4960 | <p>At compile-time:</p> |
| 5049 | <pre><code class="zig">comptime { | 4961 | <pre><code class="zig">comptime { |
| 5050 | const spartan_count: u16 = 300; | 4962 | const spartan_count: u16 = 300; |
| ... | @@ -5060,8 +4972,9 @@ test.zig:3:20: error: cast from 'u16' to 'u8' truncates bits | ... | @@ -5060,8 +4972,9 @@ test.zig:3:20: error: cast from 'u16' to 'u8' truncates bits |
| 5060 | where <code>T</code> is the integer type, such as <code>u32</code>, and <code>value</code> | 4972 | where <code>T</code> is the integer type, such as <code>u32</code>, and <code>value</code> |
| 5061 | is the value you want to truncate. | 4973 | is the value you want to truncate. |
| 5062 | </p> | 4974 | </p> |
| 5063 | <h3 id="undef-integer-overflow">Integer Overflow</h3> | 4975 | {#header_close#} |
| 5064 | <h4 id="undef-int-overflow-default">Default Operations</h4> | 4976 | {#header_open|Integer Overflow#} |
| | 4977 | {#header_open|Default Operations#} |
| 5065 | <p>The following operators can cause integer overflow:</p> | 4978 | <p>The following operators can cause integer overflow:</p> |
| 5066 | <ul> | 4979 | <ul> |
| 5067 | <li><code>+</code> (addition)</li> | 4980 | <li><code>+</code> (addition)</li> |
| ... | @@ -5083,7 +4996,8 @@ test.zig:3:20: error: cast from 'u16' to 'u8' truncates bits | ... | @@ -5083,7 +4996,8 @@ test.zig:3:20: error: cast from 'u16' to 'u8' truncates bits |
| 5083 | byte += 1; | 4996 | byte += 1; |
| 5084 | ^</code></pre> | 4997 | ^</code></pre> |
| 5085 | <p>At runtime crashes with the message <code>integer overflow</code> and a stack trace.</p> | 4998 | <p>At runtime crashes with the message <code>integer overflow</code> and a stack trace.</p> |
| 5086 | <h4 id="undef-int-overflow-std">Standard Library Math Functions</h4> | 4999 | {#header_close#} |
| | 5000 | {#header_open|Standard Library Math Functions#} |
| 5087 | <p>These functions provided by the standard library return possible errors.</p> | 5001 | <p>These functions provided by the standard library return possible errors.</p> |
| 5088 | <ul> | 5002 | <ul> |
| 5089 | <li><code>@import("std").math.add</code></li> | 5003 | <li><code>@import("std").math.add</code></li> |
| ... | @@ -5112,7 +5026,8 @@ pub fn main() -&gt; %void { | ... | @@ -5112,7 +5026,8 @@ pub fn main() -&gt; %void { |
| 5112 | <pre><code class="sh">$ zig build-exe test.zig | 5026 | <pre><code class="sh">$ zig build-exe test.zig |
| 5113 | $ ./test | 5027 | $ ./test |
| 5114 | unable to add one: Overflow</code></pre> | 5028 | unable to add one: Overflow</code></pre> |
| 5115 | <h4 id="undef-int-overflow-builtin">Builtin Overflow Functions</h4> | 5029 | {#header_close#} |
| | 5030 | {#header_open|Builtin Overflow Functions#} |
| 5116 | <p> | 5031 | <p> |
| 5117 | These builtins return a <code>bool</code> of whether or not overflow | 5032 | These builtins return a <code>bool</code> of whether or not overflow |
| 5118 | occurred, as well as returning the overflowed bits: | 5033 | occurred, as well as returning the overflowed bits: |
| ... | @@ -5140,7 +5055,8 @@ pub fn main() -&gt; %void { | ... | @@ -5140,7 +5055,8 @@ pub fn main() -&gt; %void { |
| 5140 | <pre><code class="sh">$ zig build-exe test.zig | 5055 | <pre><code class="sh">$ zig build-exe test.zig |
| 5141 | $ ./test | 5056 | $ ./test |
| 5142 | overflowed result: 9</code></pre> | 5057 | overflowed result: 9</code></pre> |
| 5143 | <h4 id="undef-int-overflow-wrap">Wrapping Operations</h4> | 5058 | {#header_close#} |
| | 5059 | {#header_open|Wrapping Operations#} |
| 5144 | <p> | 5060 | <p> |
| 5145 | These operations have guaranteed wraparound semantics. | 5061 | These operations have guaranteed wraparound semantics. |
| 5146 | </p> | 5062 | </p> |
| ... | @@ -5159,7 +5075,9 @@ test "wraparound addition and subtraction" { | ... | @@ -5159,7 +5075,9 @@ test "wraparound addition and subtraction" { |
| 5159 | const max_val = min_val -% 1; | 5075 | const max_val = min_val -% 1; |
| 5160 | assert(max_val == @maxValue(i32)); | 5076 | assert(max_val == @maxValue(i32)); |
| 5161 | }</code></pre> | 5077 | }</code></pre> |
| 5162 | <h3 id="undef-shl-overflow">Exact Left Shift Overflow</h3> | 5078 | {#header_close#} |
| | 5079 | {#header_close#} |
| | 5080 | {#header_open|Exact Left Shift Overflow#} |
| 5163 | <p>At compile-time:</p> | 5081 | <p>At compile-time:</p> |
| 5164 | <pre><code class="zig">comptime { | 5082 | <pre><code class="zig">comptime { |
| 5165 | const x = @shlExact(u8(0b01010101), 2); | 5083 | const x = @shlExact(u8(0b01010101), 2); |
| ... | @@ -5169,7 +5087,8 @@ test "wraparound addition and subtraction" { | ... | @@ -5169,7 +5087,8 @@ test "wraparound addition and subtraction" { |
| 5169 | const x = @shlExact(u8(0b01010101), 2); | 5087 | const x = @shlExact(u8(0b01010101), 2); |
| 5170 | ^</code></pre> | 5088 | ^</code></pre> |
| 5171 | <p>At runtime crashes with the message <code>left shift overflowed bits</code> and a stack trace.</p> | 5089 | <p>At runtime crashes with the message <code>left shift overflowed bits</code> and a stack trace.</p> |
| 5172 | <h3 id="undef-shr-overflow">Exact Right Shift Overflow</h3> | 5090 | {#header_close#} |
| | 5091 | {#header_open|Exact Right Shift Overflow#} |
| 5173 | <p>At compile-time:</p> | 5092 | <p>At compile-time:</p> |
| 5174 | <pre><code class="zig">comptime { | 5093 | <pre><code class="zig">comptime { |
| 5175 | const x = @shrExact(u8(0b10101010), 2); | 5094 | const x = @shrExact(u8(0b10101010), 2); |
| ... | @@ -5179,7 +5098,8 @@ test "wraparound addition and subtraction" { | ... | @@ -5179,7 +5098,8 @@ test "wraparound addition and subtraction" { |
| 5179 | const x = @shrExact(u8(0b10101010), 2); | 5098 | const x = @shrExact(u8(0b10101010), 2); |
| 5180 | ^</code></pre> | 5099 | ^</code></pre> |
| 5181 | <p>At runtime crashes with the message <code>right shift overflowed bits</code> and a stack trace.</p> | 5100 | <p>At runtime crashes with the message <code>right shift overflowed bits</code> and a stack trace.</p> |
| 5182 | <h3 id="undef-division-by-zero">Division by Zero</h3> | 5101 | {#header_close#} |
| | 5102 | {#header_open|Division by Zero#} |
| 5183 | <p>At compile-time:</p> | 5103 | <p>At compile-time:</p> |
| 5184 | <pre><code class="zig">comptime { | 5104 | <pre><code class="zig">comptime { |
| 5185 | const a: i32 = 1; | 5105 | const a: i32 = 1; |
| ... | @@ -5192,7 +5112,8 @@ test "wraparound addition and subtraction" { | ... | @@ -5192,7 +5112,8 @@ test "wraparound addition and subtraction" { |
| 5192 | ^</code></pre> | 5112 | ^</code></pre> |
| 5193 | <p>At runtime crashes with the message <code>division by zero</code> and a stack trace.</p> | 5113 | <p>At runtime crashes with the message <code>division by zero</code> and a stack trace.</p> |
| 5194 | | 5114 | |
| 5195 | <h3 id="undef-remainder-division-by-zero">Remainder Division by Zero</h3> | 5115 | {#header_close#} |
| | 5116 | {#header_open|Remainder Division by Zero#} |
| 5196 | <p>At compile-time:</p> | 5117 | <p>At compile-time:</p> |
| 5197 | <pre><code class="zig">comptime { | 5118 | <pre><code class="zig">comptime { |
| 5198 | const a: i32 = 10; | 5119 | const a: i32 = 10; |
| ... | @@ -5205,11 +5126,14 @@ test "wraparound addition and subtraction" { | ... | @@ -5205,11 +5126,14 @@ test "wraparound addition and subtraction" { |
| 5205 | ^</code></pre> | 5126 | ^</code></pre> |
| 5206 | <p>At runtime crashes with the message <code>remainder division by zero</code> and a stack trace.</p> | 5127 | <p>At runtime crashes with the message <code>remainder division by zero</code> and a stack trace.</p> |
| 5207 | | 5128 | |
| 5208 | <h3 id="undef-exact-division-remainder">Exact Division Remainder</h3> | 5129 | {#header_close#} |
| | 5130 | {#header_open|Exact Division Remainder#} |
| 5209 | <p>TODO</p> | 5131 | <p>TODO</p> |
| 5210 | <h3 id="undef-slice-widen-remainder">Slice Widen Remainder</h3> | 5132 | {#header_close#} |
| | 5133 | {#header_open|Slice Widen Remainder#} |
| 5211 | <p>TODO</p> | 5134 | <p>TODO</p> |
| 5212 | <h3 id="undef-attempt-unwrap-null">Attempt to Unwrap Null</h3> | 5135 | {#header_close#} |
| | 5136 | {#header_open|Attempt to Unwrap Null#} |
| 5213 | <p>At compile-time:</p> | 5137 | <p>At compile-time:</p> |
| 5214 | <pre><code class="zig">comptime { | 5138 | <pre><code class="zig">comptime { |
| 5215 | const nullable_number: ?i32 = null; | 5139 | const nullable_number: ?i32 = null; |
| ... | @@ -5235,7 +5159,8 @@ pub fn main() -&gt; %void { | ... | @@ -5235,7 +5159,8 @@ pub fn main() -&gt; %void { |
| 5235 | <pre><code class="sh">% zig build-exe test.zig | 5159 | <pre><code class="sh">% zig build-exe test.zig |
| 5236 | $ ./test | 5160 | $ ./test |
| 5237 | it's null</code></pre> | 5161 | it's null</code></pre> |
| 5238 | <h3 id="undef-attempt-unwrap-error">Attempt to Unwrap Error</h3> | 5162 | {#header_close#} |
| | 5163 | {#header_open|Attempt to Unwrap Error#} |
| 5239 | <p>At compile-time:</p> | 5164 | <p>At compile-time:</p> |
| 5240 | <pre><code class="zig">comptime { | 5165 | <pre><code class="zig">comptime { |
| 5241 | const number = %%getNumberOrFail(); | 5166 | const number = %%getNumberOrFail(); |
| ... | @@ -5274,7 +5199,8 @@ fn getNumberOrFail() -&gt; %i32 { | ... | @@ -5274,7 +5199,8 @@ fn getNumberOrFail() -&gt; %i32 { |
| 5274 | $ ./test | 5199 | $ ./test |
| 5275 | got error: UnableToReturnNumber</code></pre> | 5200 | got error: UnableToReturnNumber</code></pre> |
| 5276 | | 5201 | |
| 5277 | <h3 id="undef-invalid-error-code">Invalid Error Code</h3> | 5202 | {#header_close#} |
| | 5203 | {#header_open|Invalid Error Code#} |
| 5278 | <p>At compile-time:</p> | 5204 | <p>At compile-time:</p> |
| 5279 | <pre><code class="zig">error AnError; | 5205 | <pre><code class="zig">error AnError; |
| 5280 | comptime { | 5206 | comptime { |
| ... | @@ -5287,16 +5213,21 @@ comptime { | ... | @@ -5287,16 +5213,21 @@ comptime { |
| 5287 | const invalid_err = error(number); | 5213 | const invalid_err = error(number); |
| 5288 | ^</code></pre> | 5214 | ^</code></pre> |
| 5289 | <p>At runtime crashes with the message <code>invalid error code</code> and a stack trace.</p> | 5215 | <p>At runtime crashes with the message <code>invalid error code</code> and a stack trace.</p> |
| 5290 | <h3 id="undef-invalid-enum-cast">Invalid Enum Cast</h3> | 5216 | {#header_close#} |
| | 5217 | {#header_open|Invalid Enum Cast#} |
| 5291 | <p>TODO</p> | 5218 | <p>TODO</p> |
| 5292 | | 5219 | |
| 5293 | <h3 id="undef-incorrect-pointer-alignment">Incorrect Pointer Alignment</h3> | 5220 | {#header_close#} |
| | 5221 | {#header_open|Incorrect Pointer Alignment#} |
| 5294 | <p>TODO</p> | 5222 | <p>TODO</p> |
| 5295 | | 5223 | |
| 5296 | <h3 id="undef-bad-union-field">Wrong Union Field Access</h3> | 5224 | {#header_close#} |
| | 5225 | {#header_open|Wrong Union Field Access#} |
| 5297 | <p>TODO</p> | 5226 | <p>TODO</p> |
| 5298 | | 5227 | |
| 5299 | <h2 id="memory">Memory</h2> | 5228 | {#header_close#} |
| | 5229 | {#header_close#} |
| | 5230 | {#header_open|Memory#} |
| 5300 | <p>TODO: explain no default allocator in zig</p> | 5231 | <p>TODO: explain no default allocator in zig</p> |
| 5301 | <p>TODO: show how to use the allocator interface</p> | 5232 | <p>TODO: show how to use the allocator interface</p> |
| 5302 | <p>TODO: mention debug allocator</p> | 5233 | <p>TODO: mention debug allocator</p> |
| ... | @@ -5308,7 +5239,8 @@ comptime { | ... | @@ -5308,7 +5239,8 @@ comptime { |
| 5308 | <li><a href="#pointers">Pointers</a></li> | 5239 | <li><a href="#pointers">Pointers</a></li> |
| 5309 | </ul> | 5240 | </ul> |
| 5310 | | 5241 | |
| 5311 | <h2 id="compile-variables">Compile Variables</h2> | 5242 | {#header_close#} |
| | 5243 | {#header_open|Compile Variables#} |
| 5312 | <p> | 5244 | <p> |
| 5313 | Compile variables are accessible by importing the <code>"builtin"</code> package, | 5245 | Compile variables are accessible by importing the <code>"builtin"</code> package, |
| 5314 | which the compiler makes available to every Zig source file. It contains | 5246 | which the compiler makes available to every Zig source file. It contains |
| ... | @@ -5478,7 +5410,8 @@ pub const link_libs = [][]const u8 { | ... | @@ -5478,7 +5410,8 @@ pub const link_libs = [][]const u8 { |
| 5478 | <ul> | 5410 | <ul> |
| 5479 | <li><a href="#build-mode">Build Mode</a></li> | 5411 | <li><a href="#build-mode">Build Mode</a></li> |
| 5480 | </ul> | 5412 | </ul> |
| 5481 | <h2 id="root-source-file">Root Source File</h2> | 5413 | {#header_close#} |
| | 5414 | {#header_open|Root Source File#} |
| 5482 | <p>TODO: explain how root source file finds other files</p> | 5415 | <p>TODO: explain how root source file finds other files</p> |
| 5483 | <p>TODO: pub fn main</p> | 5416 | <p>TODO: pub fn main</p> |
| 5484 | <p>TODO: pub fn panic</p> | 5417 | <p>TODO: pub fn panic</p> |
| ... | @@ -5486,17 +5419,20 @@ pub const link_libs = [][]const u8 { | ... | @@ -5486,17 +5419,20 @@ pub const link_libs = [][]const u8 { |
| 5486 | <p>TODO: order independent top level declarations</p> | 5419 | <p>TODO: order independent top level declarations</p> |
| 5487 | <p>TODO: lazy analysis</p> | 5420 | <p>TODO: lazy analysis</p> |
| 5488 | <p>TODO: using comptime { _ = @import() }</p> | 5421 | <p>TODO: using comptime { _ = @import() }</p> |
| 5489 | <h2 id="zig-test">Zig Test</h2> | 5422 | {#header_close#} |
| | 5423 | {#header_open|Zig Test#} |
| 5490 | <p>TODO: basic usage</p> | 5424 | <p>TODO: basic usage</p> |
| 5491 | <p>TODO: lazy analysis</p> | 5425 | <p>TODO: lazy analysis</p> |
| 5492 | <p>TODO: --test-filter</p> | 5426 | <p>TODO: --test-filter</p> |
| 5493 | <p>TODO: --test-name-prefix</p> | 5427 | <p>TODO: --test-name-prefix</p> |
| 5494 | <p>TODO: testing in releasefast and releasesafe mode. assert still works</p> | 5428 | <p>TODO: testing in releasefast and releasesafe mode. assert still works</p> |
| 5495 | <h2 id="zig-build-system">Zig Build System</h2> | 5429 | {#header_close#} |
| | 5430 | {#header_open|Zig Build System#} |
| 5496 | <p>TODO: explain purpose, it's supposed to replace make/cmake</p> | 5431 | <p>TODO: explain purpose, it's supposed to replace make/cmake</p> |
| 5497 | <p>TODO: example of building a zig executable</p> | 5432 | <p>TODO: example of building a zig executable</p> |
| 5498 | <p>TODO: example of building a C library</p> | 5433 | <p>TODO: example of building a C library</p> |
| 5499 | <h2 id="c">C</h2> | 5434 | {#header_close#} |
| | 5435 | {#header_open|C#} |
| 5500 | <p> | 5436 | <p> |
| 5501 | Although Zig is independent of C, and, unlike most other languages, does not depend on libc, | 5437 | Although Zig is independent of C, and, unlike most other languages, does not depend on libc, |
| 5502 | Zig acknowledges the importance of interacting with existing C code. | 5438 | Zig acknowledges the importance of interacting with existing C code. |
| ... | @@ -5504,7 +5440,7 @@ pub const link_libs = [][]const u8 { | ... | @@ -5504,7 +5440,7 @@ pub const link_libs = [][]const u8 { |
| 5504 | <p> | 5440 | <p> |
| 5505 | There are a few ways that Zig facilitates C interop. | 5441 | There are a few ways that Zig facilitates C interop. |
| 5506 | </p> | 5442 | </p> |
| 5507 | <h3 id="c-type-primitives">C Type Primitives</h3> | 5443 | {#header_open|C Type Primitives#} |
| 5508 | <p> | 5444 | <p> |
| 5509 | These have guaranteed C ABI compatibility and can be used like any other type. | 5445 | These have guaranteed C ABI compatibility and can be used like any other type. |
| 5510 | </p> | 5446 | </p> |
| ... | @@ -5524,7 +5460,8 @@ pub const link_libs = [][]const u8 { | ... | @@ -5524,7 +5460,8 @@ pub const link_libs = [][]const u8 { |
| 5524 | <ul> | 5460 | <ul> |
| 5525 | <li><a href="#primitive-types">Primitive Types</a></li> | 5461 | <li><a href="#primitive-types">Primitive Types</a></li> |
| 5526 | </ul> | 5462 | </ul> |
| 5527 | <h3 id="c-string-literals">C String Literals</h3> | 5463 | {#header_close#} |
| | 5464 | {#header_open|C String Literals#} |
| 5528 | <pre><code class="zig">extern fn puts(&amp;const u8); | 5465 | <pre><code class="zig">extern fn puts(&amp;const u8); |
| 5529 | | 5466 | |
| 5530 | pub fn main() -&gt; %void { | 5467 | pub fn main() -&gt; %void { |
| ... | @@ -5539,7 +5476,8 @@ pub fn main() -&gt; %void { | ... | @@ -5539,7 +5476,8 @@ pub fn main() -&gt; %void { |
| 5539 | <ul> | 5476 | <ul> |
| 5540 | <li><a href="#string-literals">String Literals</a></li> | 5477 | <li><a href="#string-literals">String Literals</a></li> |
| 5541 | </ul> | 5478 | </ul> |
| 5542 | <h3 id="c-import">Import from C Header File</h3> | 5479 | {#header_close#} |
| | 5480 | {#header_open|Import from C Header File#} |
| 5543 | <p> | 5481 | <p> |
| 5544 | The <code>@cImport</code> builtin function can be used | 5482 | The <code>@cImport</code> builtin function can be used |
| 5545 | to directly import symbols from .h files: | 5483 | to directly import symbols from .h files: |
| ... | @@ -5574,11 +5512,13 @@ const c = @cImport({ | ... | @@ -5574,11 +5512,13 @@ const c = @cImport({ |
| 5574 | <li><a href="#builtin-cUndef">@cUndef</a></li> | 5512 | <li><a href="#builtin-cUndef">@cUndef</a></li> |
| 5575 | <li><a href="#builtin-import">@import</a></li> | 5513 | <li><a href="#builtin-import">@import</a></li> |
| 5576 | </ul> | 5514 | </ul> |
| 5577 | <h3 id="mixing-object-files">Mixing Object Files</h3> | 5515 | {#header_close#} |
| | 5516 | {#header_open|Mixing Object Files#} |
| 5578 | <p> | 5517 | <p> |
| 5579 | You can mix Zig object files with any other object files that respect the C ABI. Example: | 5518 | You can mix Zig object files with any other object files that respect the C ABI. Example: |
| 5580 | </p> | 5519 | </p> |
| 5581 | <h4>base64.zig</h4> | 5520 | {#header_close#} |
| | 5521 | {#header_open|base64.zig#} |
| 5582 | <pre><code class="zig">const base64 = @import("std").base64; | 5522 | <pre><code class="zig">const base64 = @import("std").base64; |
| 5583 | | 5523 | |
| 5584 | export fn decode_base_64(dest_ptr: &amp;u8, dest_len: usize, | 5524 | export fn decode_base_64(dest_ptr: &amp;u8, dest_len: usize, |
| ... | @@ -5592,7 +5532,7 @@ export fn decode_base_64(dest_ptr: &amp;u8, dest_len: usize, | ... | @@ -5592,7 +5532,7 @@ export fn decode_base_64(dest_ptr: &amp;u8, dest_len: usize, |
| 5592 | return decoded_size; | 5532 | return decoded_size; |
| 5593 | } | 5533 | } |
| 5594 | </code></pre> | 5534 | </code></pre> |
| 5595 | <h4>test.c</h4> | 5535 | {{teheader_open:st.c}} |
| 5596 | <pre><code class="c">// This header is generated by zig from base64.zig | 5536 | <pre><code class="c">// This header is generated by zig from base64.zig |
| 5597 | #include "base64.h" | 5537 | #include "base64.h" |
| 5598 | | 5538 | |
| ... | @@ -5609,7 +5549,8 @@ int main(int argc, char **argv) { | ... | @@ -5609,7 +5549,8 @@ int main(int argc, char **argv) { |
| 5609 | | 5549 | |
| 5610 | return 0; | 5550 | return 0; |
| 5611 | }</code></pre> | 5551 | }</code></pre> |
| 5612 | <h4>build.zig</h4> | 5552 | {#header_close#} |
| | 5553 | {#header_open|build.zig#} |
| 5613 | <pre><code class="zig">const Builder = @import("std").build.Builder; | 5554 | <pre><code class="zig">const Builder = @import("std").build.Builder; |
| 5614 | | 5555 | |
| 5615 | pub fn build(b: &amp;Builder) { | 5556 | pub fn build(b: &amp;Builder) { |
| ... | @@ -5625,7 +5566,8 @@ pub fn build(b: &amp;Builder) { | ... | @@ -5625,7 +5566,8 @@ pub fn build(b: &amp;Builder) { |
| 5625 | | 5566 | |
| 5626 | b.default_step.dependOn(&amp;exe.step); | 5567 | b.default_step.dependOn(&amp;exe.step); |
| 5627 | }</code></pre> | 5568 | }</code></pre> |
| 5628 | <h4>Terminal</h4> | 5569 | {#header_close#} |
| | 5570 | {#header_open|Terminal#} |
| 5629 | <pre><code class="sh">$ zig build | 5571 | <pre><code class="sh">$ zig build |
| 5630 | $ ./test | 5572 | $ ./test |
| 5631 | all your base are belong to us</code></pre> | 5573 | all your base are belong to us</code></pre> |
| ... | @@ -5634,7 +5576,9 @@ all your base are belong to us</code></pre> | ... | @@ -5634,7 +5576,9 @@ all your base are belong to us</code></pre> |
| 5634 | <li><a href="#targets">Targets</a></li> | 5576 | <li><a href="#targets">Targets</a></li> |
| 5635 | <li><a href="#zig-build-system">Zig Build System</a></li> | 5577 | <li><a href="#zig-build-system">Zig Build System</a></li> |
| 5636 | </ul> | 5578 | </ul> |
| 5637 | <h2 id="targets">Targets</h2> | 5579 | {#header_close#} |
| | 5580 | {#header_close#} |
| | 5581 | {#header_open|Targets#} |
| 5638 | <p> | 5582 | <p> |
| 5639 | Zig supports generating code for all targets that LLVM supports. Here is | 5583 | Zig supports generating code for all targets that LLVM supports. Here is |
| 5640 | what it looks like to execute <code>zig targets</code> on a Linux x86_64 | 5584 | what it looks like to execute <code>zig targets</code> on a Linux x86_64 |
| ... | @@ -5760,14 +5704,15 @@ Environments: | ... | @@ -5760,14 +5704,15 @@ Environments: |
| 5760 | Linux x86_64. Not all standard library code requires operating system abstractions, however, | 5704 | Linux x86_64. Not all standard library code requires operating system abstractions, however, |
| 5761 | so things such as generic data structures work an all above platforms. | 5705 | so things such as generic data structures work an all above platforms. |
| 5762 | </p> | 5706 | </p> |
| 5763 | <h2 id="style-guide">Style Guide</h2> | 5707 | {#header_close#} |
| | 5708 | {#header_open|Style Guide#} |
| 5764 | <p> | 5709 | <p> |
| 5765 | These coding conventions are not enforced by the compiler, but they are shipped in | 5710 | These coding conventions are not enforced by the compiler, but they are shipped in |
| 5766 | this documentation along with the compiler in order to provide a point of | 5711 | this documentation along with the compiler in order to provide a point of |
| 5767 | reference, should anyone wish to point to an authority on agreed upon Zig | 5712 | reference, should anyone wish to point to an authority on agreed upon Zig |
| 5768 | coding style. | 5713 | coding style. |
| 5769 | </p> | 5714 | </p> |
| 5770 | <h3 id="style-guide-whitespace">Whitespace</h3> | 5715 | {#header_open|Whitespace#} |
| 5771 | <ul> | 5716 | <ul> |
| 5772 | <li> | 5717 | <li> |
| 5773 | 4 space indentation | 5718 | 4 space indentation |
| ... | @@ -5782,7 +5727,8 @@ coding style. | ... | @@ -5782,7 +5727,8 @@ coding style. |
| 5782 | Line length: aim for 100; use common sense. | 5727 | Line length: aim for 100; use common sense. |
| 5783 | </li> | 5728 | </li> |
| 5784 | </ul> | 5729 | </ul> |
| 5785 | <h3 id="style-guide-names">Names</h3> | 5730 | {#header_close#} |
| | 5731 | {#header_open|Names#} |
| 5786 | <p> | 5732 | <p> |
| 5787 | Roughly speaking: <code>camelCaseFunctionName</code>, <code>TitleCaseTypeName</code>, | 5733 | Roughly speaking: <code>camelCaseFunctionName</code>, <code>TitleCaseTypeName</code>, |
| 5788 | <code>snake_case_variable_name</code>. More precisely: | 5734 | <code>snake_case_variable_name</code>. More precisely: |
| ... | @@ -5816,7 +5762,8 @@ coding style. | ... | @@ -5816,7 +5762,8 @@ coding style. |
| 5816 | do what makes sense. For example, if there is an established convention such as | 5762 | do what makes sense. For example, if there is an established convention such as |
| 5817 | <code>ENOENT</code>, follow the established convention. | 5763 | <code>ENOENT</code>, follow the established convention. |
| 5818 | </p> | 5764 | </p> |
| 5819 | <h3 id="style-guide-examples">Examples</h3> | 5765 | {#header_close#} |
| | 5766 | {#header_open|Examples#} |
| 5820 | <pre><code class="zig">const namespace_name = @import("dir_name/file_name.zig"); | 5767 | <pre><code class="zig">const namespace_name = @import("dir_name/file_name.zig"); |
| 5821 | var global_var: i32 = undefined; | 5768 | var global_var: i32 = undefined; |
| 5822 | const const_name = 42; | 5769 | const const_name = 42; |
| ... | @@ -5858,7 +5805,9 @@ fn readU32Be() -&gt; u32 {}</code></pre> | ... | @@ -5858,7 +5805,9 @@ fn readU32Be() -&gt; u32 {}</code></pre> |
| 5858 | <p> | 5805 | <p> |
| 5859 | See the Zig Standard Library for more examples. | 5806 | See the Zig Standard Library for more examples. |
| 5860 | </p> | 5807 | </p> |
| 5861 | <h2 id="grammar">Grammar</h2> | 5808 | {#header_close#} |
| | 5809 | {#header_close#} |
| | 5810 | {#header_open|Grammar#} |
| 5862 | <pre><code>Root = many(TopLevelItem) EOF | 5811 | <pre><code>Root = many(TopLevelItem) EOF |
| 5863 | | 5812 | |
| 5864 | TopLevelItem = ErrorValueDecl | CompTimeExpression(Block) | TopLevelDecl | TestDecl | 5813 | TopLevelItem = ErrorValueDecl | CompTimeExpression(Block) | TopLevelDecl | TestDecl |
| ... | @@ -6010,7 +5959,8 @@ KeywordLiteral = "true" | "false" | "null" | "undefined" | "error" | "this" | "u | ... | @@ -6010,7 +5959,8 @@ KeywordLiteral = "true" | "false" | "null" | "undefined" | "error" | "this" | "u |
| 6010 | ContainerDecl = option("extern" | "packed") | 5959 | ContainerDecl = option("extern" | "packed") |
| 6011 | ("struct" option(GroupedExpression) | "union" option("enum" option(GroupedExpression) | GroupedExpression) | ("enum" option(GroupedExpression))) | 5960 | ("struct" option(GroupedExpression) | "union" option("enum" option(GroupedExpression) | GroupedExpression) | ("enum" option(GroupedExpression))) |
| 6012 | "{" many(ContainerMember) "}"</code></pre> | 5961 | "{" many(ContainerMember) "}"</code></pre> |
| 6013 | <h2 id="zen">Zen</h2> | 5962 | {#header_close#} |
| | 5963 | {#header_open|Zen#} |
| 6014 | <ul> | 5964 | <ul> |
| 6015 | <li>Communicate intent precisely.</li> | 5965 | <li>Communicate intent precisely.</li> |
| 6016 | <li>Edge cases matter.</li> | 5966 | <li>Edge cases matter.</li> |
| ... | @@ -6024,8 +5974,10 @@ ContainerDecl = option("extern" | "packed") | ... | @@ -6024,8 +5974,10 @@ ContainerDecl = option("extern" | "packed") |
| 6024 | <li>Minimize energy spent on coding style.</li> | 5974 | <li>Minimize energy spent on coding style.</li> |
| 6025 | <li>Together we serve end users.</li> | 5975 | <li>Together we serve end users.</li> |
| 6026 | </ul> | 5976 | </ul> |
| 6027 | <h2>TODO</h2> | 5977 | {#header_close#} |
| | 5978 | {#header_open|TODO#} |
| 6028 | <p>TODO: document changes from a31b23c46ba2a8c28df01adc1aa0b4d878b9a5cf (compile time reflection additions)</p> | 5979 | <p>TODO: document changes from a31b23c46ba2a8c28df01adc1aa0b4d878b9a5cf (compile time reflection additions)</p> |
| | 5980 | {#header_close#} |
| 6029 | </div> | 5981 | </div> |
| 6030 | <script src="highlight/highlight.pack.js"></script> | 5982 | <script src="highlight/highlight.pack.js"></script> |
| 6031 | <script>hljs.initHighlightingOnLoad();</script> | 5983 | <script>hljs.initHighlightingOnLoad();</script> |