2017-11-07 08:22:27 +00:00
<!doctype html>
2019-12-05 21:38:59 +00:00
< html lang = "en" >
2017-11-07 08:22:27 +00:00
< head >
< meta charset = "utf-8" >
2020-07-27 14:10:55 +00:00
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" >
2024-02-03 14:06:18 +00:00
< meta name = "color-scheme" content = "light dark" >
2017-11-07 08:22:27 +00:00
< title > Documentation - The Zig Programming Language< / title >
2022-12-12 10:43:41 +00:00
< link rel = "icon" href = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAgklEQVR4AWMYWuD7EllJIM4G4g4g5oIJ/odhOJ8wToOxSTXgNxDHoeiBMfA4+wGShjyYOCkG/IGqWQziEzYAoUAeiF9D5U+DxEg14DRU7jWIT5IBIOdCxf+A+CQZAAoopEB7QJwBCBwHiip8UYmRdrAlDpIMgApwQZNnNii5Dq0MBgCxxycBnwEd+wAAAABJRU5ErkJggg==" >
2023-09-24 18:17:30 +00:00
< link rel = "icon" href = "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxNTMgMTQwIj48ZyBmaWxsPSIjRjdBNDFEIj48Zz48cG9seWdvbiBwb2ludHM9IjQ2LDIyIDI4LDQ0IDE5LDMwIi8+PHBvbHlnb24gcG9pbnRzPSI0NiwyMiAzMywzMyAyOCw0NCAyMiw0NCAyMiw5NSAzMSw5NSAyMCwxMDAgMTIsMTE3IDAsMTE3IDAsMjIiIHNoYXBlLXJlbmRlcmluZz0iY3Jpc3BFZGdlcyIvPjxwb2x5Z29uIHBvaW50cz0iMzEsOTUgMTIsMTE3IDQsMTA2Ii8+PC9nPjxnPjxwb2x5Z29uIHBvaW50cz0iNTYsMjIgNjIsMzYgMzcsNDQiLz48cG9seWdvbiBwb2ludHM9IjU2LDIyIDExMSwyMiAxMTEsNDQgMzcsNDQgNTYsMzIiIHNoYXBlLXJlbmRlcmluZz0iY3Jpc3BFZGdlcyIvPjxwb2x5Z29uIHBvaW50cz0iMTE2LDk1IDk3LDExNyA5MCwxMDQiLz48cG9seWdvbiBwb2ludHM9IjExNiw5NSAxMDAsMTA0IDk3LDExNyA0MiwxMTcgNDIsOTUiIHNoYXBlLXJlbmRlcmluZz0iY3Jpc3BFZGdlcyIvPjxwb2x5Z29uIHBvaW50cz0iMTUwLDAgNTIsMTE3IDMsMTQwIDEwMSwyMiIvPjwvZz48Zz48cG9seWdvbiBwb2ludHM9IjE0MSwyMiAxNDAsNDAgMTIyLDQ1Ii8+PHBvbHlnb24gcG9pbnRzPSIxNTMsMjIgMTUzLDExNyAxMDYsMTE3IDEyMCwxMDUgMTI1LDk1IDEzMSw5NSAxMzEsNDUgMTIyLDQ1IDEzMiwzNiAxNDEsMjIiIHNoYXBlLXJlbmRlcmluZz0iY3Jpc3BFZGdlcyIvPjxwb2x5Z29uIHBvaW50cz0iMTI1LDk1IDEzMCwxMTAgMTA2LDExNyIvPjwvZz48L2c+PC9zdmc+" >
2019-12-05 21:38:59 +00:00
< style >
2021-08-11 09:01:03 +00:00
:root{
2024-01-22 02:40:41 +00:00
--nav-width: 26em;
2021-08-11 09:01:03 +00:00
--nav-margin-l: 1em;
}
2018-10-24 02:11:31 +00:00
body{
2019-02-22 15:56:49 +00:00
font-family: system-ui, -apple-system, Roboto, "Segoe UI", sans-serif;
2019-10-23 10:55:20 +00:00
margin: 0;
2021-08-11 09:01:03 +00:00
line-height: 1.5;
2018-10-24 02:11:31 +00:00
}
2021-08-11 09:01:03 +00:00
header {
padding: 0 1em;
2018-10-24 02:11:31 +00:00
}
2021-08-11 09:01:03 +00:00
#contents {
max-width: 60em;
margin: auto;
padding: 0 1em;
2017-11-07 08:22:27 +00:00
}
2021-08-11 09:01:03 +00:00
#navigation {
padding: 0 1em;
2017-11-07 08:22:27 +00:00
}
2024-01-22 02:40:41 +00:00
table ul {
list-style-type: none;
padding: 0em;
}
table li {
padding-bottom: 1em;
line-height:1.2em;
}
table, th, td {
border-collapse: collapse;
border: 1px solid grey;
}
th, td {
padding: 0.5em;
}
th[scope=row] {
text-align: left;
font-weight: normal;
}
2021-08-11 09:01:03 +00:00
@media screen and (min-width: 1025px) {
header {
margin-left: calc(var(--nav-width) + var(--nav-margin-l));
}
header h1 {
margin: auto;
max-width: 30em;
}
#navigation {
overflow: auto;
width: var(--nav-width);
height: 100vh;
position: fixed;
top:0;
left:0;
bottom:0;
padding: unset;
margin-left: var(--nav-margin-l);
}
#navigation nav ul {
padding-left: 1em;
}
#contents-wrapper {
margin-left: calc(var(--nav-width) + var(--nav-margin-l));
}
2022-03-21 20:31:41 +00:00
}
2021-08-11 09:01:03 +00:00
2022-03-21 20:31:41 +00:00
a:hover,a:focus {
background: #fff2a8;
}
dt {
2021-08-11 09:01:03 +00:00
font-weight: bold;
}
2023-02-25 10:14:43 +00:00
.sgr-1m {
2022-03-21 20:31:41 +00:00
font-weight: bold;
2018-01-19 08:03:20 +00:00
}
2023-02-25 10:14:43 +00:00
.sgr-2m {
2022-03-21 20:31:41 +00:00
color: #575757;
2018-01-19 08:03:20 +00:00
}
2023-02-25 10:14:43 +00:00
.sgr-31_1m {
2022-03-21 20:31:41 +00:00
color: #b40000;
2018-01-19 08:03:20 +00:00
}
2023-02-25 10:14:43 +00:00
.sgr-32_1m {
2022-03-21 20:31:41 +00:00
color: green;
2018-01-19 08:03:20 +00:00
}
2023-02-25 10:14:43 +00:00
.sgr-36_1m {
2022-03-21 20:31:41 +00:00
color: #005C7A;
}
.file {
font-weight: bold;
border: unset;
2018-01-19 08:03:20 +00:00
}
2021-08-11 09:01:03 +00:00
code {
2022-03-21 20:31:41 +00:00
background: #f8f8f8;
border: 1px dotted silver;
padding-left: 0.3em;
padding-right: 0.3em;
2018-01-19 08:03:20 +00:00
}
2018-01-30 04:33:38 +00:00
pre > code {
2022-03-21 20:31:41 +00:00
display: block;
overflow: auto;
padding: 0.5em;
border: 1px solid #eee;
line-height: normal;
Make clearer inline code blocks in language reference paragraphs (#9317)
* Make clearer inline code blocks in language reference paragraphs
This commit makes the inline code blocks within paragraphs standout against the
descriptive text. The code blocks within tables are left un-styled.
The line-height of the paragraphs has been set to 1.7 based on recommendations
from MDN Web Docs and W3C. The value is unitless based on the recommendation.
Closes #9316, #6313
* Make clearer inline code blocks in language reference paragraphs
Goal: To improve legibility of inline code blocks in the language reference.
This commit alters the styles of code HTML elements in paragraphs, unordered
lists, ordered lists, tables, and preformatted text elements.
Most of the changes here are taken from suggestions from @dbandstra on GitHub
in response to a code review.
* p, ul, ol, but not #toc are set to the same line-height
* p, ul, ol, and table have the same inline code styles
* The inline code background color set to match the preformatted code blocks in
the light theme and dark theme. The border colors are adjusted as well.
* The preformatted code block font size is set to default. The 12pt setting was removed.
The line-height of paragraphs is set to 1.5. This value is chosen based on
recommendations from W3C [1] via MDN Web Docs [2] and the contents of the
language reference.
The MDN Web Docs offers two recommendations:
1. Use a unitless number for line-height.
2. Use a minimum value of 1.5 for main paragraph content.
[1] https://www.w3.org/TR/WCAG21/#visual-presentation
[2] https://developer.mozilla.org/en-US/docs/Web/CSS/line-height
Closes #6313, #9316
* Set language reference line-height and pre code border color
This commit is an update to a group of commits with the goal of improving the
legibility of the language reference. In this commit, the line-height is now set
in the `#contents` ID and reset to normal for preformatted code blocks. This
change better separates lines of all main content text for legibility.
Closes: #9316, #6313
* Style the language reference code elements
This commit sets the style of the HTML code element. Since preformatted code
blocks have an overriding style, this is safe to set for all inline code elements.
2021-08-20 10:02:14 +00:00
}
2021-08-11 09:01:03 +00:00
samp {
2022-03-21 20:31:41 +00:00
background: #fafafa;
2018-01-30 04:33:38 +00:00
}
2021-08-11 09:01:03 +00:00
pre > samp {
2022-03-21 20:31:41 +00:00
display: block;
overflow: auto;
padding: 0.5em;
border: 1px solid #eee;
line-height: normal;
}
kbd {
2023-02-25 10:14:43 +00:00
font-weight: normal;
2022-03-21 20:31:41 +00:00
}
.table-wrapper {
width: 100%;
overflow-x: auto;
2018-01-30 04:33:38 +00:00
}
2022-03-21 20:31:41 +00:00
2018-09-12 21:26:51 +00:00
.tok-kw {
2022-03-21 20:31:41 +00:00
color: #333;
font-weight: bold;
2018-09-12 21:26:51 +00:00
}
.tok-str {
2022-03-21 20:31:41 +00:00
color: #d14;
2018-09-12 21:26:51 +00:00
}
.tok-builtin {
2022-03-21 20:31:41 +00:00
color: #005C7A;
2018-09-12 21:26:51 +00:00
}
.tok-comment {
2022-03-21 20:31:41 +00:00
color: #545454;
font-style: italic;
2018-09-12 21:26:51 +00:00
}
.tok-fn {
2022-03-21 20:31:41 +00:00
color: #900;
font-weight: bold;
2018-09-12 21:26:51 +00:00
}
.tok-null {
2022-03-21 20:31:41 +00:00
color: #005C5C;
2018-09-12 21:26:51 +00:00
}
.tok-number {
2022-03-21 20:31:41 +00:00
color: #005C5C;
2018-09-12 21:26:51 +00:00
}
.tok-type {
2022-03-21 20:31:41 +00:00
color: #458;
font-weight: bold;
2019-10-23 10:55:20 +00:00
}
2022-03-21 20:31:41 +00:00
figure {
margin: auto 0;
}
figure pre {
margin-top: 0;
}
figcaption {
padding-left: 0.5em;
font-size: small;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
2018-10-24 02:11:31 +00:00
}
2021-08-11 09:01:03 +00:00
figcaption.zig-cap {
2022-03-21 20:31:41 +00:00
background: #fcdba5;
}
figcaption.c-cap {
background: #a8b9cc;
color: #000;
2019-04-03 21:39:07 +00:00
}
2021-10-03 20:27:12 +00:00
figcaption.peg-cap {
2022-03-21 20:31:41 +00:00
background: #fcdba5;
2021-10-02 20:54:05 +00:00
}
2022-03-21 20:31:41 +00:00
figcaption.javascript-cap {
background: #365d95;
2021-08-11 09:01:03 +00:00
color: #fff;
2018-10-24 02:11:31 +00:00
}
2022-03-21 20:31:41 +00:00
figcaption.shell-cap {
background: #ccc;
color: #000;
}
aside {
border-left: 0.25em solid #f7a41d;
padding: 0 1em 0 1em;
}
h1 a, h2 a, h3 a, h4 a, h5 a {
text-decoration: none;
color: #333;
}
a.hdr {
visibility: hidden;
}
h1:hover > a.hdr, h2:hover > a.hdr, h3:hover > a.hdr, h4:hover > a.hdr, h5:hover > a.hdr {
visibility: visible;
}
th pre code {
background: none;
}
@media (prefers-color-scheme: dark) {
body{
background:#121212;
color: #ccc;
}
a {
color: #88f;
}
a:hover,a:focus {
color: #000;
}
table, th, td {
border-color: grey;
}
2023-02-25 10:14:43 +00:00
.sgr-2m {
2022-03-21 20:31:41 +00:00
color: grey;
}
2023-02-25 10:14:43 +00:00
.sgr-31_1m {
2022-03-21 20:31:41 +00:00
color: red;
}
2023-02-25 10:14:43 +00:00
.sgr-32_1m {
2022-03-21 20:31:41 +00:00
color: #00B800;
}
2023-02-25 10:14:43 +00:00
.sgr-36_1m {
2022-03-21 20:31:41 +00:00
color: #0086b3;
}
code {
background: #222;
border-color: #444;
}
pre > code {
color: #ccc;
background: #222;
border: unset;
}
samp {
background: #000;
color: #ccc;
}
pre > samp {
border: unset;
}
.tok-kw {
color: #eee;
}
.tok-str {
color: #2e5;
}
.tok-builtin {
color: #ff894c;
}
.tok-comment {
color: #aa7;
}
.tok-fn {
color: #B1A0F8;
}
.tok-null {
color: #ff8080;
}
.tok-number {
color: #ff8080;
}
.tok-type {
color: #68f;
}
h1 a, h2 a, h3 a, h4 a, h5 a {
color: #aaa;
}
figcaption.zig-cap {
background-color: #b27306;
color: #000;
}
figcaption.peg-cap {
background-color: #b27306;
color: #000;
}
figcaption.shell-cap {
background: #2a2a2a;
color: #fff;
}
}
< / style >
2021-08-11 09:01:03 +00:00
< / head >
< body >
< header > < h1 > Zig Language Reference< / h1 > < / header >
< div id = "main-wrapper" >
< div id = "navigation" >
< nav aria-labelledby = "zig-version" >
< h2 id = "zig-version" > Zig Version< / h2 >
< a href = "https://ziglang.org/documentation/0.1.1/" > 0.1.1< / a > |
< a href = "https://ziglang.org/documentation/0.2.0/" > 0.2.0< / a > |
< a href = "https://ziglang.org/documentation/0.3.0/" > 0.3.0< / a > |
< a href = "https://ziglang.org/documentation/0.4.0/" > 0.4.0< / a > |
< a href = "https://ziglang.org/documentation/0.5.0/" > 0.5.0< / a > |
< a href = "https://ziglang.org/documentation/0.6.0/" > 0.6.0< / a > |
< a href = "https://ziglang.org/documentation/0.7.1/" > 0.7.1< / a > |
< a href = "https://ziglang.org/documentation/0.8.1/" > 0.8.1< / a > |
2022-02-15 03:08:04 +00:00
< a href = "https://ziglang.org/documentation/0.9.1/" > 0.9.1< / a > |
2023-01-23 01:19:26 +00:00
< a href = "https://ziglang.org/documentation/0.10.1/" > 0.10.1< / a > |
2023-08-03 18:22:03 +00:00
< a href = "https://ziglang.org/documentation/0.11.0/" > 0.11.0< / a > |
2024-04-19 21:00:35 +00:00
< a href = "https://ziglang.org/documentation/0.12.0/" > 0.12.0< / a > |
2024-06-06 19:05:11 +00:00
< a href = "https://ziglang.org/documentation/0.13.0/" > 0.13.0< / a > |
2021-08-11 09:01:03 +00:00
master
< / nav >
< nav aria-labelledby = "table-of-contents" >
< h2 id = "table-of-contents" > Table of Contents< / h2 >
{#nav#}
< / nav >
2019-10-23 10:55:20 +00:00
< / div >
2021-08-11 09:01:03 +00:00
< div id = "contents-wrapper" > < main id = "contents" >
2018-01-17 04:19:05 +00:00
{#header_open|Introduction#}
2017-11-07 08:22:27 +00:00
< p >
2021-08-11 09:01:03 +00:00
< a href = "https://ziglang.org" > Zig< / a > is a general-purpose programming language and toolchain for maintaining
2019-12-30 23:16:40 +00:00
< strong > robust< / strong > , < strong > optimal< / strong > , and < strong > reusable< / strong > software.
2017-11-07 08:22:27 +00:00
< / p >
2021-08-11 09:01:03 +00:00
< dl >
< dt > Robust< / dt > < dd > Behavior is correct even for edge cases such as out of memory.< / dd >
< dt > Optimal< / dt > < dd > Write programs the best way they can behave and perform.< / dd >
< dt > Reusable< / dt > < dd > The same code works in many environments which have different
constraints.< / dd >
< dt > Maintainable< / dt > < dd > Precisely communicate intent to the compiler and
2019-11-13 18:44:19 +00:00
other programmers. The language imposes a low overhead to reading code and is
2021-08-11 09:01:03 +00:00
resilient to changing requirements and environments.< / dd >
< / dl >
2017-11-07 08:22:27 +00:00
< p >
Often the most efficient way to learn something new is to see examples, so
this documentation shows how to use each of Zig's features. It is
all on one page so you can search with your browser's search tool.
< / p >
< p >
2018-01-19 08:03:20 +00:00
The code samples in this document are compiled and tested as part of the main test suite of Zig.
2020-07-09 11:38:02 +00:00
< / p >
< p >
2018-01-19 08:03:20 +00:00
This HTML document depends on no external files, so you can use it offline.
< / p >
2021-08-11 09:01:03 +00:00
{#header_close#}
{#header_open|Zig Standard Library#}
< p >
The < a href = "https://ziglang.org/documentation/master/std/" > Zig Standard Library< / a > has its own documentation.
< / p >
2019-07-02 19:21:03 +00:00
< p >
2021-08-11 09:01:03 +00:00
Zig's Standard Library contains commonly used algorithms, data structures, and definitions to help you build programs or libraries.
You will see many examples of Zig's Standard Library used in this documentation. To learn more about the Zig Standard Library,
visit the link above.
2019-07-02 19:21:03 +00:00
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
2019-04-03 21:39:07 +00:00
2018-01-17 04:19:05 +00:00
{#header_open|Hello World#}
2017-11-07 08:22:27 +00:00
2024-04-25 00:41:47 +00:00
{#code|hello.zig#}
2018-01-17 06:50:35 +00:00
2017-11-07 08:22:27 +00:00
< p >
2024-02-05 15:35:00 +00:00
Most of the time, it is more appropriate to write to stderr rather than stdout, and
2024-01-21 23:47:18 +00:00
whether or not the message is successfully written to the stream is irrelevant.
For this common case, there is a simpler API:
2020-07-09 11:38:02 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|hello_again.zig#}
2017-11-07 08:22:27 +00:00
2018-01-23 04:06:07 +00:00
< p >
2024-01-21 23:47:18 +00:00
In this case, the {#syntax#}!{#endsyntax#} may be omitted from the return
type because no errors are returned from the function.
2018-01-23 04:06:07 +00:00
< / p >
2020-07-09 11:38:02 +00:00
{#see_also|Values|@import|Errors|Root Source File|Source Encoding#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-07-31 02:27:07 +00:00
{#header_open|Comments#}
2023-06-17 20:35:50 +00:00
< p >
Zig supports 3 types of comments. Normal comments are ignored, but doc comments
and top-level doc comments are used by the compiler to generate the package documentation.
< / p >
< p >
The generated documentation is still experimental, and can be produced with:
< / p >
{#shell_samp#}zig test -femit-docs main.zig{#end_shell_samp#}
2024-04-25 00:41:47 +00:00
{#code|comments.zig#}
2018-07-31 02:27:07 +00:00
< p >
2018-09-14 14:35:03 +00:00
There are no multiline comments in Zig (e.g. like < code class = "c" > /* */< / code >
2023-06-20 18:27:52 +00:00
comments in C). This allows Zig to have the property that each line
2018-07-31 02:27:07 +00:00
of code can be tokenized out of context.
< / p >
2023-06-17 20:35:50 +00:00
{#header_open|Doc Comments#}
2018-07-31 02:27:07 +00:00
< p >
A doc comment is one that begins with exactly three slashes (i.e.
2018-09-14 14:35:03 +00:00
{#syntax#}///{#endsyntax#} but not {#syntax#}////{#endsyntax#});
2018-07-31 02:27:07 +00:00
multiple doc comments in a row are merged together to form a multiline
doc comment. The doc comment documents whatever immediately follows it.
< / p >
2024-04-25 00:41:47 +00:00
{#code|doc_comments.zig#}
2018-07-31 02:27:07 +00:00
< p >
2023-06-17 20:35:50 +00:00
Doc comments are only allowed in certain places; it is a compile error to
have a doc comment in an unexpected place, such as in the middle of an expression,
or just before a non-doc comment.
< / p >
2024-04-25 00:41:47 +00:00
{#code|invalid_doc-comment.zig#}
{#code|unattached_doc-comment.zig#}
2023-06-17 20:35:50 +00:00
< p >
Doc comments can be interleaved with normal comments. Currently, when producing
the package documentation, normal comments are merged with doc comments.
2018-07-31 02:27:07 +00:00
< / p >
{#header_close#}
2020-07-05 22:39:28 +00:00
{#header_open|Top-Level Doc Comments#}
2023-06-17 20:35:50 +00:00
< p >
A top-level doc comment is one that begins with two slashes and an exclamation
point: {#syntax#}//!{#endsyntax#}; it documents the current module.
< / p >
< p >
It is a compile error if a top-level doc comment is not placed at the start
of a {#link|container|Containers#}, before any expressions.
< / p >
2024-04-25 00:41:47 +00:00
{#code|tldoc_comments.zig#}
2023-06-17 20:35:50 +00:00
2020-07-05 22:39:28 +00:00
{#header_close#}
2018-07-31 02:27:07 +00:00
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|Values#}
2024-04-25 00:41:47 +00:00
{#code|values.zig#}
2018-01-17 04:19:05 +00:00
{#header_open|Primitive Types#}
2018-01-30 04:33:38 +00:00
< div class = "table-wrapper" >
2017-11-07 08:22:27 +00:00
< table >
2021-08-11 09:01:03 +00:00
< caption > Primitive Types< / caption >
< thead >
< tr >
< th scope = "col" > Type< / th >
< th scope = "col" > C Equivalent< / th >
< th scope = "col" > Description< / th >
2017-11-07 08:22:27 +00:00
< / tr >
2021-08-11 09:01:03 +00:00
< / thead >
< tbody >
2017-11-07 08:22:27 +00:00
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}i8{#endsyntax#}< / th >
2018-09-14 14:35:03 +00:00
< td > < code class = "c" > int8_t< / code > < / td >
2017-11-07 08:22:27 +00:00
< td > signed 8-bit integer< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}u8{#endsyntax#}< / th >
2018-09-14 14:35:03 +00:00
< td > < code class = "c" > uint8_t< / code > < / td >
2017-11-07 08:22:27 +00:00
< td > unsigned 8-bit integer< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}i16{#endsyntax#}< / th >
2018-09-14 14:35:03 +00:00
< td > < code class = "c" > int16_t< / code > < / td >
2017-11-07 08:22:27 +00:00
< td > signed 16-bit integer< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}u16{#endsyntax#}< / th >
2018-09-14 14:35:03 +00:00
< td > < code class = "c" > uint16_t< / code > < / td >
2017-11-07 08:22:27 +00:00
< td > unsigned 16-bit integer< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}i32{#endsyntax#}< / th >
2018-09-14 14:35:03 +00:00
< td > < code class = "c" > int32_t< / code > < / td >
2017-11-07 08:22:27 +00:00
< td > signed 32-bit integer< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}u32{#endsyntax#}< / th >
2018-09-14 14:35:03 +00:00
< td > < code class = "c" > uint32_t< / code > < / td >
2017-11-07 08:22:27 +00:00
< td > unsigned 32-bit integer< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}i64{#endsyntax#}< / th >
2018-09-14 14:35:03 +00:00
< td > < code class = "c" > int64_t< / code > < / td >
2017-11-07 08:22:27 +00:00
< td > signed 64-bit integer< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}u64{#endsyntax#}< / th >
2018-09-14 14:35:03 +00:00
< td > < code class = "c" > uint64_t< / code > < / td >
2017-11-07 08:22:27 +00:00
< td > unsigned 64-bit integer< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}i128{#endsyntax#}< / th >
2018-09-14 14:35:03 +00:00
< td > < code class = "c" > __int128< / code > < / td >
2017-11-07 08:22:27 +00:00
< td > signed 128-bit integer< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}u128{#endsyntax#}< / th >
2018-09-14 14:35:03 +00:00
< td > < code class = "c" > unsigned __int128< / code > < / td >
2017-11-07 08:22:27 +00:00
< td > unsigned 128-bit integer< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}isize{#endsyntax#}< / th >
2018-09-14 14:35:03 +00:00
< td > < code class = "c" > intptr_t< / code > < / td >
2017-11-07 08:22:27 +00:00
< td > signed pointer sized integer< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}usize{#endsyntax#}< / th >
2022-04-21 18:33:42 +00:00
< td > < code class = "c" > uintptr_t< / code > , < code class = "c" > size_t< / code > < / td >
< td > unsigned pointer sized integer. Also see < a href = "https://github.com/ziglang/zig/issues/5185" > #5185< / a > < / td >
2017-11-07 08:22:27 +00:00
< / tr >
2023-06-28 18:58:45 +00:00
< tr >
< th scope = "row" > {#syntax#}c_char{#endsyntax#}< / th >
< td > < code class = "c" > char< / code > < / td >
< td > for ABI compatibility with C< / td >
< / tr >
2017-11-07 08:22:27 +00:00
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}c_short{#endsyntax#}< / th >
2018-09-14 14:35:03 +00:00
< td > < code class = "c" > short< / code > < / td >
2017-11-07 08:22:27 +00:00
< td > for ABI compatibility with C< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}c_ushort{#endsyntax#}< / th >
2018-09-14 14:35:03 +00:00
< td > < code class = "c" > unsigned short< / code > < / td >
2017-11-07 08:22:27 +00:00
< td > for ABI compatibility with C< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}c_int{#endsyntax#}< / th >
2018-09-14 14:35:03 +00:00
< td > < code class = "c" > int< / code > < / td >
2017-11-07 08:22:27 +00:00
< td > for ABI compatibility with C< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}c_uint{#endsyntax#}< / th >
2018-09-14 14:35:03 +00:00
< td > < code class = "c" > unsigned int< / code > < / td >
2017-11-07 08:22:27 +00:00
< td > for ABI compatibility with C< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}c_long{#endsyntax#}< / th >
2018-09-14 14:35:03 +00:00
< td > < code class = "c" > long< / code > < / td >
2017-11-07 08:22:27 +00:00
< td > for ABI compatibility with C< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}c_ulong{#endsyntax#}< / th >
2018-09-14 14:35:03 +00:00
< td > < code class = "c" > unsigned long< / code > < / td >
2017-11-07 08:22:27 +00:00
< td > for ABI compatibility with C< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}c_longlong{#endsyntax#}< / th >
2018-09-14 14:35:03 +00:00
< td > < code class = "c" > long long< / code > < / td >
2017-11-07 08:22:27 +00:00
< td > for ABI compatibility with C< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}c_ulonglong{#endsyntax#}< / th >
2018-09-14 14:35:03 +00:00
< td > < code class = "c" > unsigned long long< / code > < / td >
2017-11-07 08:22:27 +00:00
< td > for ABI compatibility with C< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}c_longdouble{#endsyntax#}< / th >
2018-09-14 14:35:03 +00:00
< td > < code class = "c" > long double< / code > < / td >
2017-11-07 08:22:27 +00:00
< td > for ABI compatibility with C< / td >
< / tr >
2018-06-27 16:59:12 +00:00
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}f16{#endsyntax#}< / th >
2018-09-24 17:08:21 +00:00
< td > < code class = "c" > _Float16< / code > < / td >
2018-06-27 16:59:12 +00:00
< td > 16-bit floating point (10-bit mantissa) IEEE-754-2008 binary16< / td >
< / tr >
2017-11-07 08:22:27 +00:00
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}f32{#endsyntax#}< / th >
2018-09-14 14:35:03 +00:00
< td > < code class = "c" > float< / code > < / td >
2018-06-16 23:53:52 +00:00
< td > 32-bit floating point (23-bit mantissa) IEEE-754-2008 binary32< / td >
2017-11-07 08:22:27 +00:00
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}f64{#endsyntax#}< / th >
2018-09-14 14:35:03 +00:00
< td > < code class = "c" > double< / code > < / td >
2018-06-16 23:53:52 +00:00
< td > 64-bit floating point (52-bit mantissa) IEEE-754-2008 binary64< / td >
2017-11-07 08:22:27 +00:00
< / tr >
2022-01-19 16:47:51 +00:00
< tr >
< th scope = "row" > {#syntax#}f80{#endsyntax#}< / th >
2024-08-01 16:03:50 +00:00
< td > < code class = "c" > long double< / code > < / td >
2022-02-02 13:35:51 +00:00
< td > 80-bit floating point (64-bit mantissa) IEEE-754-2008 80-bit extended precision< / td >
2022-01-19 16:47:51 +00:00
< / tr >
2017-11-07 08:22:27 +00:00
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}f128{#endsyntax#}< / th >
2018-09-24 17:08:21 +00:00
< td > < code class = "c" > _Float128< / code > < / td >
2018-06-16 23:53:52 +00:00
< td > 128-bit floating point (112-bit mantissa) IEEE-754-2008 binary128< / td >
2017-11-07 08:22:27 +00:00
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}bool{#endsyntax#}< / th >
2018-09-14 14:35:03 +00:00
< td > < code class = "c" > bool< / code > < / td >
< td > {#syntax#}true{#endsyntax#} or {#syntax#}false{#endsyntax#}< / td >
2017-11-07 08:22:27 +00:00
< / tr >
2021-12-19 05:24:45 +00:00
< tr >
< th scope = "row" > {#syntax#}anyopaque{#endsyntax#}< / th >
< td > < code class = "c" > void< / code > < / td >
< td > Used for type-erased pointers.< / td >
< / tr >
2017-11-07 08:22:27 +00:00
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}void{#endsyntax#}< / th >
2017-11-07 08:22:27 +00:00
< td > (none)< / td >
2022-10-30 23:27:02 +00:00
< td > Always the value {#syntax#}void{}{#endsyntax#}< / td >
2017-11-07 08:22:27 +00:00
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}noreturn{#endsyntax#}< / th >
2017-11-07 08:22:27 +00:00
< td > (none)< / td >
2018-09-14 14:35:03 +00:00
< td > the type of {#syntax#}break{#endsyntax#}, {#syntax#}continue{#endsyntax#}, {#syntax#}return{#endsyntax#}, {#syntax#}unreachable{#endsyntax#}, and {#syntax#}while (true) {}{#endsyntax#}< / td >
2017-11-07 08:22:27 +00:00
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}type{#endsyntax#}< / th >
2017-11-07 08:22:27 +00:00
< td > (none)< / td >
< td > the type of types< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}anyerror{#endsyntax#}< / th >
2017-11-07 08:22:27 +00:00
< td > (none)< / td >
< td > an error code< / td >
< / tr >
2018-06-16 23:53:52 +00:00
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}comptime_int{#endsyntax#}< / th >
2018-06-16 23:53:52 +00:00
< td > (none)< / td >
< td > Only allowed for {#link|comptime#}-known values. The type of integer literals.< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}comptime_float{#endsyntax#}< / th >
2018-06-16 23:53:52 +00:00
< td > (none)< / td >
< td > Only allowed for {#link|comptime#}-known values. The type of float literals.< / td >
< / tr >
2021-08-11 09:01:03 +00:00
< / tbody >
2017-11-07 08:22:27 +00:00
< / table >
2018-01-30 04:33:38 +00:00
< / div >
2018-08-25 21:39:43 +00:00
< p >
In addition to the integer types above, arbitrary bit-width integers can be referenced by using
2019-12-05 21:38:59 +00:00
an identifier of < code > i< / code > or < code > u< / code > followed by digits. For example, the identifier
2018-11-06 16:09:14 +00:00
{#syntax#}i7{#endsyntax#} refers to a signed 7-bit integer. The maximum allowed bit-width of an
integer type is {#syntax#}65535{#endsyntax#}.
2018-08-25 21:39:43 +00:00
< / p >
2020-02-24 21:39:03 +00:00
{#see_also|Integers|Floats|void|Errors|@Type#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Primitive Values#}
2018-01-30 04:33:38 +00:00
< div class = "table-wrapper" >
2017-11-07 08:22:27 +00:00
< table >
2021-08-11 09:01:03 +00:00
< caption > Primitive Values< / caption >
< thead >
2017-11-07 08:22:27 +00:00
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "col" > Name< / th >
< th scope = "col" > Description< / th >
2017-11-07 08:22:27 +00:00
< / tr >
2021-08-11 09:01:03 +00:00
< / thead >
< tbody >
2017-11-07 08:22:27 +00:00
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}true{#endsyntax#} and {#syntax#}false{#endsyntax#}< / th >
2018-09-14 14:35:03 +00:00
< td > {#syntax#}bool{#endsyntax#} values< / td >
2017-11-07 08:22:27 +00:00
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}null{#endsyntax#}< / th >
2018-09-14 14:35:03 +00:00
< td > used to set an optional type to {#syntax#}null{#endsyntax#}< / td >
2017-11-07 08:22:27 +00:00
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > {#syntax#}undefined{#endsyntax#}< / th >
2017-11-07 08:22:27 +00:00
< td > used to leave a value unspecified< / td >
< / tr >
2021-08-11 09:01:03 +00:00
< / tbody >
2017-11-07 08:22:27 +00:00
< / table >
2018-01-30 04:33:38 +00:00
< / div >
2018-09-24 17:08:21 +00:00
{#see_also|Optionals|undefined#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2021-02-24 13:26:13 +00:00
{#header_open|String Literals and Unicode Code Point Literals#}
2019-03-23 21:35:21 +00:00
< p >
2021-03-13 01:10:55 +00:00
String literals are constant single-item {#link|Pointers#} to null-terminated byte arrays.
2019-11-20 22:11:07 +00:00
The type of string literals encodes both the length, and the fact that they are null-terminated,
and thus they can be {#link|coerced|Type Coercion#} to both {#link|Slices#} and
2019-11-24 03:29:12 +00:00
{#link|Null-Terminated Pointers|Sentinel-Terminated Pointers#}.
2024-01-21 23:36:27 +00:00
Dereferencing string literals converts them to {#link|Arrays#}.
2019-03-23 21:35:21 +00:00
< / p >
< p >
2024-01-22 00:14:31 +00:00
Because Zig source code is {#link|UTF-8 encoded|Source Encoding#}, any
non-ASCII bytes appearing within a string literal in source code carry
their UTF-8 meaning into the content of the string in the Zig program;
the bytes are not modified by the compiler. It is possible to embed
non-UTF-8 bytes into a string literal using < code > \xNN< / code > notation.
2023-01-27 18:29:28 +00:00
< / p >
2024-01-22 00:14:31 +00:00
< p > Indexing into a string containing non-ASCII bytes returns individual
bytes, whether valid UTF-8 or not.< / p >
2023-01-27 18:29:28 +00:00
< p >
2021-02-24 13:26:13 +00:00
Unicode code point literals have type {#syntax#}comptime_int{#endsyntax#}, the same as
2019-03-23 21:35:21 +00:00
{#link|Integer Literals#}. All {#link|Escape Sequences#} are valid in both string literals
2021-02-24 13:26:13 +00:00
and Unicode code point literals.
< / p >
2024-04-25 00:41:47 +00:00
{#code|string_literals.zig#}
2021-09-29 04:31:41 +00:00
{#see_also|Arrays|Source Encoding#}
2018-01-17 04:19:05 +00:00
{#header_open|Escape Sequences#}
2018-01-30 04:33:38 +00:00
< div class = "table-wrapper" >
2017-11-07 08:22:27 +00:00
< table >
2021-08-11 09:01:03 +00:00
< caption > Escape Sequences< / caption >
< thead >
2017-11-07 08:22:27 +00:00
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "col" > Escape Sequence< / th >
< th scope = "col" > Name< / th >
2017-11-07 08:22:27 +00:00
< / tr >
2021-08-11 09:01:03 +00:00
< / thead >
< tbody >
2017-11-07 08:22:27 +00:00
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > < code > \n< / code > < / th >
2017-11-07 08:22:27 +00:00
< td > Newline< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > < code > \r< / code > < / th >
2017-11-07 08:22:27 +00:00
< td > Carriage Return< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > < code > \t< / code > < / th >
2017-11-07 08:22:27 +00:00
< td > Tab< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > < code > \\< / code > < / th >
2017-11-07 08:22:27 +00:00
< td > Backslash< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > < code > \'< / code > < / th >
2017-11-07 08:22:27 +00:00
< td > Single Quote< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > < code > \"< / code > < / th >
2017-11-07 08:22:27 +00:00
< td > Double Quote< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > < code > \xNN< / code > < / th >
2021-02-24 13:26:13 +00:00
< td > hexadecimal 8-bit byte value (2 digits)< / td >
2017-11-07 08:22:27 +00:00
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" > < code > \u{NNNNNN}< / code > < / th >
2024-06-12 05:13:22 +00:00
< td > hexadecimal Unicode scalar value UTF-8 encoded (1 or more digits)< / td >
2017-11-07 08:22:27 +00:00
< / tr >
2021-08-11 09:01:03 +00:00
< / tbody >
2017-11-07 08:22:27 +00:00
< / table >
2018-01-30 04:33:38 +00:00
< / div >
2024-06-12 05:13:22 +00:00
< p > Note that the maximum valid Unicode scalar value is {#syntax#}0x10ffff{#endsyntax#}.< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Multiline String Literals#}
2017-11-07 08:22:27 +00:00
< p >
Multiline string literals have no escapes and can span across multiple lines.
2018-09-14 14:35:03 +00:00
To start a multiline string literal, use the {#syntax#}\\{#endsyntax#} token. Just like a comment,
2017-11-07 08:22:27 +00:00
the string literal goes until the end of the line. The end of the line is
not included in the string literal.
2018-09-14 14:35:03 +00:00
However, if the next line begins with {#syntax#}\\{#endsyntax#} then a newline is appended and
2017-11-07 08:22:27 +00:00
the string literal continues.
< / p >
2024-04-25 00:41:47 +00:00
{#code|multiline_string_literals.zig#}
2018-01-17 05:22:33 +00:00
{#see_also|@embedFile#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_close#}
{#header_open|Assignment#}
2018-09-14 14:35:03 +00:00
< p > Use the {#syntax#}const{#endsyntax#} keyword to assign a value to an identifier:< / p >
2024-04-25 00:41:47 +00:00
{#code|constant_identifier_cannot_change.zig#}
2017-11-07 08:22:27 +00:00
2018-09-14 14:35:03 +00:00
< p > {#syntax#}const{#endsyntax#} applies to all of the bytes that the identifier immediately addresses. {#link|Pointers#} have their own const-ness.< / p >
< p > If you need a variable that you can modify, use the {#syntax#}var{#endsyntax#} keyword:< / p >
2024-04-25 00:41:47 +00:00
{#code|mutable_var.zig#}
2017-11-07 08:22:27 +00:00
< p > Variables must be initialized:< / p >
2024-04-25 00:41:47 +00:00
{#code|var_must_be_initialized.zig#}
2017-11-07 08:22:27 +00:00
2018-06-14 22:12:05 +00:00
{#header_open|undefined#}
2018-09-14 14:35:03 +00:00
< p > Use {#syntax#}undefined{#endsyntax#} to leave variables uninitialized:< / p >
2024-04-25 00:41:47 +00:00
{#code|assign_undefined.zig#}
2017-11-07 08:22:27 +00:00
2018-07-03 18:03:27 +00:00
< p >
2019-11-08 20:56:21 +00:00
{#syntax#}undefined{#endsyntax#} can be {#link|coerced|Type Coercion#} to any type.
2018-09-14 14:35:03 +00:00
Once this happens, it is no longer possible to detect that the value is {#syntax#}undefined{#endsyntax#}.
{#syntax#}undefined{#endsyntax#} means the value could be anything, even something that is nonsense
according to the type. Translated into English, {#syntax#}undefined{#endsyntax#} means "Not a meaningful
2018-07-03 18:03:27 +00:00
value. Using this value would be a bug. The value will be unused, or overwritten before being used."
< / p >
< p >
2018-09-14 14:35:03 +00:00
In {#link|Debug#} mode, Zig writes {#syntax#}0xaa{#endsyntax#} bytes to undefined memory. This is to catch
2022-09-12 18:13:48 +00:00
bugs early, and to help detect use of undefined memory in a debugger. However, this behavior is only an
implementation feature, not a language semantic, so it is not guaranteed to be observable to code.
2018-07-03 18:03:27 +00:00
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_close#}
2018-06-14 22:12:05 +00:00
{#header_close#}
2021-09-29 04:31:41 +00:00
{#header_open|Zig Test#}
< p >
Code written within one or more {#syntax#}test{#endsyntax#} declarations can be used to ensure behavior meets expectations:
< / p >
2024-04-25 00:41:47 +00:00
{#code|testing_introduction.zig#}
2021-09-29 04:31:41 +00:00
< p >
2023-06-16 19:06:52 +00:00
The < code class = "file" > testing_introduction.zig< / code > code sample tests the {#link|function|Functions#}
2021-09-29 04:31:41 +00:00
{#syntax#}addOne{#endsyntax#} to ensure that it returns {#syntax#}42{#endsyntax#} given the input
{#syntax#}41{#endsyntax#}. From this test's perspective, the {#syntax#}addOne{#endsyntax#} function is
said to be < em > code under test< / em > .
< / p >
< p >
< kbd > zig test< / kbd > is a tool that creates and runs a test build. By default, it builds and runs an
executable program using the < em > default test runner< / em > provided by the {#link|Zig Standard Library#}
as its main entry point. During the build, {#syntax#}test{#endsyntax#} declarations found while
{#link|resolving|Root Source File#} the given Zig source file are included for the default test runner
to run and report on.
< / p >
< aside >
This documentation discusses the features of the default test runner as provided by the Zig Standard Library.
2024-06-27 19:37:44 +00:00
Its source code is located in < code class = "file" > lib/compiler/test_runner.zig< / code > .
2021-09-29 04:31:41 +00:00
< / aside >
< p >
The shell output shown above displays two lines after the < kbd > zig test< / kbd > command. These lines are
printed to standard error by the default test runner:
< / p >
< dl >
2024-02-25 13:04:06 +00:00
< dt > < samp > 1/2 testing_introduction.test.expect addOne adds one to 41...< / samp > < / dt >
2021-09-29 04:31:41 +00:00
< dd > Lines like this indicate which test, out of the total number of tests, is being run.
2024-02-25 13:04:06 +00:00
In this case, < samp > 1/2< / samp > indicates that the first test, out of a total of two tests,
is being run. Note that, when the test runner program's standard error is output
2021-09-29 04:31:41 +00:00
to the terminal, these lines are cleared when a test succeeds.
< / dd >
2024-02-25 13:04:06 +00:00
< dt > < samp > 2/2 testing_introduction.decltest.addOne...< / samp > < / dt >
2023-06-17 20:35:50 +00:00
< dd > When the test name is an identifier, the default test runner uses the text
decltest instead of test.
< / dd >
< dt > < samp > All 2 tests passed.< / samp > < / dt >
2021-09-29 04:31:41 +00:00
< dd > This line indicates the total number of tests that have passed.< / dd >
< / dl >
{#header_open|Test Declarations#}
< p >
Test declarations contain the {#link|keyword|Keyword Reference#} {#syntax#}test{#endsyntax#}, followed by an
2023-06-17 20:35:50 +00:00
optional name written as a {#link|string literal|String Literals and Unicode Code Point Literals#} or an
{#link|identifier|Identifiers#}, followed by a {#link|block|Blocks#} containing any valid Zig code that
is allowed in a {#link|function|Functions#}.
2021-09-29 04:31:41 +00:00
< / p >
2024-01-19 04:55:44 +00:00
< p > Non-named test blocks always run during test builds and are exempt from
{#link|Skip Tests#}.< / p >
2021-09-29 04:31:41 +00:00
< p >
Test declarations are similar to {#link|Functions#}: they have a return type and a block of code. The implicit
return type of {#syntax#}test{#endsyntax#} is the {#link|Error Union Type#} {#syntax#}anyerror!void{#endsyntax#},
and it cannot be changed. When a Zig source file is not built using the < kbd > zig test< / kbd > tool, the test
declarations are omitted from the build.
< / p >
< p >
Test declarations can be written in the same file, where code under test is written, or in a separate Zig source file.
Since test declarations are top-level declarations, they are order-independent and can
be written before or after the code under test.
< / p >
{#see_also|The Global Error Set|Grammar#}
2023-12-29 02:00:32 +00:00
{#header_open|Doctests#}
< p >
Test declarations named using an identifier are < em > doctests< / em > . The identifier must refer to another declaration in
scope. A doctest, like a {#link|doc comment|Doc Comments#}, serves as documentation for the associated declaration, and
will appear in the generated documentation for the declaration.
< / p >
< p >
An effective doctest should be self-contained and focused on the declaration being tested, answering questions a new
user might have about its interface or intended usage, while avoiding unnecessary or confusing details. A doctest is not
a substitute for a doc comment, but rather a supplement and companion providing a testable, code-driven example, verified
by < kbd > zig test< / kbd > .
< / p >
{#header_close#}
2021-09-29 04:31:41 +00:00
{#header_close#}
{#header_open|Test Failure#}
< p >
The default test runner checks for an {#link|error|Errors#} returned from a test.
When a test returns an error, the test is considered a failure and its {#link|error return trace|Error Return Traces#}
is output to standard error. The total number of failures will be reported after all tests have run.
< / p >
2024-04-25 00:41:47 +00:00
{#code|testing_failure.zig#}
2021-09-29 04:31:41 +00:00
{#header_close#}
{#header_open|Skip Tests#}
< p >
One way to skip tests is to filter them out by using the < kbd > zig test< / kbd > command line parameter
< kbd > --test-filter [text]< / kbd > . This makes the test build only include tests whose name contains the
supplied filter text. Note that non-named tests are run even when using the < kbd > --test-filter [text]< / kbd >
command line parameter.
< / p >
< p >
To programmatically skip a test, make a {#syntax#}test{#endsyntax#} return the error
{#syntax#}error.SkipZigTest{#endsyntax#} and the default test runner will consider the test as being skipped.
The total number of skipped tests will be reported after all tests have run.
< / p >
2024-04-25 00:41:47 +00:00
{#code|testing_skip.zig#}
2021-09-29 04:31:41 +00:00
{#header_close#}
2022-12-01 22:28:44 +00:00
2021-09-29 04:31:41 +00:00
{#header_open|Report Memory Leaks#}
< p >
When code allocates {#link|Memory#} using the {#link|Zig Standard Library#}'s testing allocator,
{#syntax#}std.testing.allocator{#endsyntax#}, the default test runner will report any leaks that are
found from using the testing allocator:
< / p >
2024-04-25 00:41:47 +00:00
{#code|testing_detect_leak.zig#}
2021-09-29 04:31:41 +00:00
{#see_also|defer|Memory#}
{#header_close#}
{#header_open|Detecting Test Build#}
< p >
Use the {#link|compile variable|Compile Variables#} {#syntax#}@import("builtin").is_test{#endsyntax#}
to detect a test build:
< / p >
2024-04-25 00:41:47 +00:00
{#code|testing_detect_test.zig#}
2021-09-29 04:31:41 +00:00
{#header_close#}
{#header_open|Test Output and Logging#}
< p >
The default test runner and the Zig Standard Library's testing namespace output messages to standard error.
< / p >
{#header_close#}
{#header_open|The Testing Namespace#}
< p >
The Zig Standard Library's < code > testing< / code > namespace contains useful functions to help
you create tests. In addition to the < code > expect< / code > function, this document uses a couple of more functions
as exemplified here:
< / p >
2024-04-25 00:41:47 +00:00
{#code|testing_namespace.zig#}
2021-09-29 04:31:41 +00:00
< p > The Zig Standard Library also contains functions to compare {#link|Slices#}, strings, and more. See the rest of the
{#syntax#}std.testing{#endsyntax#} namespace in the {#link|Zig Standard Library#} for more available functions.< / p >
{#header_close#}
{#header_open|Test Tool Documentation#}
< p >
< kbd > zig test< / kbd > has a few command line parameters which affect the compilation.
See < kbd > zig test --help< / kbd > for a full list.
< / p >
{#header_close#}
{#header_close#}
2019-04-04 16:26:47 +00:00
{#header_open|Variables#}
< p >
A variable is a unit of {#link|Memory#} storage.
< / p >
< p >
It is generally preferable to use {#syntax#}const{#endsyntax#} rather than
{#syntax#}var{#endsyntax#} when declaring a variable. This causes less work for both
humans and computers to do when reading code, and creates more optimization opportunities.
< / p >
2024-01-16 17:11:34 +00:00
< p >
The {#syntax#}extern{#endsyntax#} keyword or {#link|@extern#} builtin function can be used to link against a variable that is exported
from another object. The {#syntax#}export{#endsyntax#} keyword or {#link|@export#} builtin function
can be used to make a variable available to other objects at link time. In both cases,
the type of the variable must be C ABI compatible.
< / p >
{#see_also|Exporting a C Library#}
2022-02-04 19:27:50 +00:00
{#header_open|Identifiers#}
< p >
Variable identifiers are never allowed to shadow identifiers from an outer scope.
< / p >
< p >
Identifiers must start with an alphabetic character or underscore and may be followed
by any number of alphanumeric characters or underscores.
They must not overlap with any keywords. See {#link|Keyword Reference#}.
< / p >
< p >
If a name that does not fit these requirements is needed, such as for linking with external libraries, the {#syntax#}@""{#endsyntax#} syntax may be used.
< / p >
2024-04-25 00:41:47 +00:00
{#code|identifiers.zig#}
2022-02-04 19:27:50 +00:00
{#header_close#}
2021-06-23 05:45:36 +00:00
{#header_open|Container Level Variables#}
2019-04-04 16:26:47 +00:00
< p >
2022-11-09 08:29:50 +00:00
{#link|Container|Containers#} level variables have static lifetime and are order-independent and lazily analyzed.
2021-06-23 05:45:36 +00:00
The initialization value of container level variables is implicitly
{#link|comptime#}. If a container level variable is {#syntax#}const{#endsyntax#} then its value is
2019-04-04 16:26:47 +00:00
{#syntax#}comptime{#endsyntax#}-known, otherwise it is runtime-known.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_container_level_variables.zig#}
2019-04-04 16:26:47 +00:00
< p >
2022-11-09 08:29:50 +00:00
Container level variables may be declared inside a {#link|struct#}, {#link|union#}, {#link|enum#}, or {#link|opaque#}:
2019-04-04 16:26:47 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_namespaced_container_level_variable.zig#}
2021-06-23 05:45:36 +00:00
{#header_close#}
{#header_open|Static Local Variables#}
< p >
It is also possible to have local variables with static lifetime by using containers inside functions.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_static_local_variable.zig#}
2021-07-08 01:50:57 +00:00
2019-04-04 16:26:47 +00:00
{#header_close#}
{#header_open|Thread Local Variables#}
< p > A variable may be specified to be a thread-local variable using the
2023-04-23 18:06:21 +00:00
{#syntax#}threadlocal{#endsyntax#} keyword,
which makes each thread work with a separate instance of the variable:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_thread_local_variables.zig#}
2019-04-04 16:26:47 +00:00
< p >
2021-06-23 05:45:36 +00:00
For {#link|Single Threaded Builds#}, all thread local variables are treated as regular {#link|Container Level Variables#}.
2019-04-04 16:26:47 +00:00
< / p >
< p >
Thread local variables may not be {#syntax#}const{#endsyntax#}.
< / p >
{#header_close#}
{#header_open|Local Variables#}
< p >
Local variables occur inside {#link|Functions#}, {#link|comptime#} blocks, and {#link|@cImport#} blocks.
< / p >
< p >
When a local variable is {#syntax#}const{#endsyntax#}, it means that after initialization, the variable's
value will not change. If the initialization value of a {#syntax#}const{#endsyntax#} variable is
{#link|comptime#}-known, then the variable is also {#syntax#}comptime{#endsyntax#}-known.
< / p >
< p >
A local variable may be qualified with the {#syntax#}comptime{#endsyntax#} keyword. This causes
the variable's value to be {#syntax#}comptime{#endsyntax#}-known, and all loads and stores of the
variable to happen during semantic analysis of the program, rather than at runtime.
All variables declared in a {#syntax#}comptime{#endsyntax#} expression are implicitly
{#syntax#}comptime{#endsyntax#} variables.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_variables.zig#}
2019-04-04 16:26:47 +00:00
{#header_close#}
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|Integers#}
{#header_open|Integer Literals#}
2024-04-25 00:41:47 +00:00
{#code|integer_literals.zig#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Runtime Integer Values#}
2017-11-07 08:22:27 +00:00
< p >
Integer literals have no size limitation, and if any undefined behavior occurs,
the compiler catches it.
< / p >
< p >
However, once an integer value is no longer known at compile-time, it must have a
known size, and is vulnerable to undefined behavior.
< / p >
2024-04-25 00:41:47 +00:00
{#code|runtime_vs_comptime.zig#}
2017-11-07 08:22:27 +00:00
< p >
2018-09-14 14:35:03 +00:00
In this function, values {#syntax#}a{#endsyntax#} and {#syntax#}b{#endsyntax#} are known only at runtime,
2019-02-22 15:56:49 +00:00
and thus this division operation is vulnerable to both {#link|Integer Overflow#} and
{#link|Division by Zero#}.
2017-11-07 08:22:27 +00:00
< / p >
< p >
2018-09-14 14:35:03 +00:00
Operators such as {#syntax#}+{#endsyntax#} and {#syntax#}-{#endsyntax#} cause undefined behavior on
2022-02-04 19:27:50 +00:00
integer overflow. Alternative operators are provided for wrapping and saturating arithmetic on all targets.
2021-09-09 03:59:55 +00:00
{#syntax#}+%{#endsyntax#} and {#syntax#}-%{#endsyntax#} perform wrapping arithmetic
while {#syntax#}+|{#endsyntax#} and {#syntax#}-|{#endsyntax#} perform saturating arithmetic.
2017-11-07 08:22:27 +00:00
< / p >
2019-02-22 15:56:49 +00:00
< p >
Zig supports arbitrary bit-width integers, referenced by using
2019-12-05 21:38:59 +00:00
an identifier of < code > i< / code > or < code > u< / code > followed by digits. For example, the identifier
2019-02-22 15:56:49 +00:00
{#syntax#}i7{#endsyntax#} refers to a signed 7-bit integer. The maximum allowed bit-width of an
2022-08-22 10:57:51 +00:00
integer type is {#syntax#}65535{#endsyntax#}. For signed integer types, Zig uses a
< a href = "https://en.wikipedia.org/wiki/Two's_complement" > two's complement< / a > representation.
2019-02-22 15:56:49 +00:00
< / p >
{#see_also|Wrapping Operations#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_close#}
{#header_open|Floats#}
2018-06-16 23:53:52 +00:00
< p > Zig has the following floating point types:< / p >
< ul >
2018-09-14 14:35:03 +00:00
< li > {#syntax#}f16{#endsyntax#} - IEEE-754-2008 binary16< / li >
< li > {#syntax#}f32{#endsyntax#} - IEEE-754-2008 binary32< / li >
< li > {#syntax#}f64{#endsyntax#} - IEEE-754-2008 binary64< / li >
2022-01-19 16:47:51 +00:00
< li > {#syntax#}f80{#endsyntax#} - IEEE-754-2008 80-bit extended precision< / li >
2018-09-14 14:35:03 +00:00
< li > {#syntax#}f128{#endsyntax#} - IEEE-754-2008 binary128< / li >
< li > {#syntax#}c_longdouble{#endsyntax#} - matches < code class = "c" > long double< / code > for the target C ABI< / li >
2018-06-16 23:53:52 +00:00
< / ul >
2018-01-17 04:19:05 +00:00
{#header_open|Float Literals#}
2018-06-16 23:53:52 +00:00
< p >
2019-03-23 19:25:38 +00:00
Float literals have type {#syntax#}comptime_float{#endsyntax#} which is guaranteed to have
the same precision and operations of the largest other floating point type, which is
{#syntax#}f128{#endsyntax#}.
< / p >
< p >
2019-11-08 20:56:21 +00:00
Float literals {#link|coerce|Type Coercion#} to any floating point type,
2019-03-23 19:25:38 +00:00
and to any {#link|integer|Integers#} type when there is no fractional component.
2018-06-16 23:53:52 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|float_literals.zig#}
2017-11-07 08:22:27 +00:00
2019-03-23 19:25:38 +00:00
< p >
There is no syntax for NaN, infinity, or negative infinity. For these special values,
one must use the standard library:
< / p >
2024-04-25 00:41:47 +00:00
{#code|float_special_values.zig#}
2019-03-23 19:25:38 +00:00
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Floating Point Operations#}
2018-09-14 14:35:03 +00:00
< p > By default floating point operations use {#syntax#}Strict{#endsyntax#} mode,
but you can switch to {#syntax#}Optimized{#endsyntax#} mode on a per-block basis:< / p >
2024-04-25 00:41:47 +00:00
{#code|float_mode_obj.zig#}
2017-11-07 08:22:27 +00:00
2018-01-19 08:03:20 +00:00
< p > For this test we have to separate code into two object files -
otherwise the optimizer figures out all the values at compile-time,
which operates in strict mode.< / p >
2024-04-25 00:41:47 +00:00
{#code|float_mode_exe.zig#}
2017-11-07 08:22:27 +00:00
2018-01-17 05:22:33 +00:00
{#see_also|@setFloatMode|Division by Zero#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-01-23 04:06:07 +00:00
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|Operators#}
2018-10-05 22:25:20 +00:00
< p >
There is no operator overloading. When you see an operator in Zig, you know that
it is doing something from this table, and nothing else.
< / p >
2018-01-17 04:19:05 +00:00
{#header_open|Table of Operators#}
2018-01-30 04:33:38 +00:00
< div class = "table-wrapper" >
2017-11-07 08:22:27 +00:00
< table >
2021-08-11 09:01:03 +00:00
< thead >
2017-11-07 08:22:27 +00:00
< tr >
2024-01-22 02:40:41 +00:00
< th scope = "col" > Name< / th >
2021-08-11 09:01:03 +00:00
< th scope = "col" > Syntax< / th >
2024-01-22 02:40:41 +00:00
< th scope = "col" > Types< / th >
< th scope = "col" > Remarks< / th >
2021-08-11 09:01:03 +00:00
< th scope = "col" > Example< / th >
2017-11-07 08:22:27 +00:00
< / tr >
2021-08-11 09:01:03 +00:00
< / thead >
< tbody >
2017-11-07 08:22:27 +00:00
< tr >
2024-01-22 02:40:41 +00:00
< td > Addition< / td >
< td > < pre > {#syntax#}a + b
a += b{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Integers#}< / li >
< li > {#link|Floats#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
2024-01-22 02:40:41 +00:00
< td >
2017-11-07 08:22:27 +00:00
< ul >
2018-01-23 04:06:07 +00:00
< li > Can cause {#link|overflow|Default Operations#} for integers.< / li >
2018-08-20 18:21:58 +00:00
< li > Invokes {#link|Peer Type Resolution#} for the operands.< / li >
< li > See also {#link|@addWithOverflow#}.< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}2 + 5 == 7{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Wrapping Addition< / td >
< td > < pre > {#syntax#}a +% b
a +%= b{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Integers#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
2024-01-22 02:40:41 +00:00
< td >
2017-11-07 08:22:27 +00:00
< ul >
2024-01-22 02:40:41 +00:00
< li > Twos-complement wrapping behavior.< / li >
2018-08-20 18:21:58 +00:00
< li > Invokes {#link|Peer Type Resolution#} for the operands.< / li >
2018-08-07 05:10:29 +00:00
< li > See also {#link|@addWithOverflow#}.< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
2024-01-22 02:40:41 +00:00
< pre > {#syntax#}@as(u32, 0xffffffff) +% 1 == 0{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
2021-09-09 03:59:55 +00:00
< tr >
2024-01-22 02:40:41 +00:00
< td > Saturating Addition< / td >
< td > < pre > {#syntax#}a +| b
a +|= b{#endsyntax#}< / pre > < / td >
2021-09-09 03:59:55 +00:00
< td >
< ul >
< li > {#link|Integers#}< / li >
< / ul >
< / td >
2024-01-22 02:40:41 +00:00
< td >
2021-09-09 03:59:55 +00:00
< ul >
< li > Invokes {#link|Peer Type Resolution#} for the operands.< / li >
< / ul >
< / td >
< td >
2024-01-22 02:40:41 +00:00
< pre > {#syntax#}@as(u8, 255) +| 1 == @as(u8, 255){#endsyntax#}< / pre >
2021-09-09 03:59:55 +00:00
< / td >
< / tr >
2017-11-07 08:22:27 +00:00
< tr >
2024-01-22 02:40:41 +00:00
< td > Subtraction< / td >
< td > < pre > {#syntax#}a - b
a -= b{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Integers#}< / li >
< li > {#link|Floats#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
2024-01-22 02:40:41 +00:00
< td >
2017-11-07 08:22:27 +00:00
< ul >
2018-01-23 04:06:07 +00:00
< li > Can cause {#link|overflow|Default Operations#} for integers.< / li >
2018-08-20 18:21:58 +00:00
< li > Invokes {#link|Peer Type Resolution#} for the operands.< / li >
< li > See also {#link|@subWithOverflow#}.< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}2 - 5 == -3{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Wrapping Subtraction< / td >
< td > < pre > {#syntax#}a -% b
a -%= b{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Integers#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
2024-01-22 02:40:41 +00:00
< td >
2017-11-07 08:22:27 +00:00
< ul >
2024-01-22 02:40:41 +00:00
< li > Twos-complement wrapping behavior.< / li >
2018-08-20 18:21:58 +00:00
< li > Invokes {#link|Peer Type Resolution#} for the operands.< / li >
< li > See also {#link|@subWithOverflow#}.< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
2024-01-22 02:40:41 +00:00
< pre > {#syntax#}@as(u8, 0) -% 1 == 255{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
2021-09-09 03:59:55 +00:00
< tr >
2024-01-22 02:40:41 +00:00
< td > Saturating Subtraction< / td >
< td > < pre > {#syntax#}a -| b
a -|= b{#endsyntax#}< / pre > < / td >
2021-09-09 03:59:55 +00:00
< td >
< ul >
< li > {#link|Integers#}< / li >
< / ul >
< / td >
2024-01-22 02:40:41 +00:00
< td >
2021-09-09 03:59:55 +00:00
< ul >
< li > Invokes {#link|Peer Type Resolution#} for the operands.< / li >
< / ul >
< / td >
< td >
< pre > {#syntax#}@as(u32, 0) -| 1 == 0{#endsyntax#}< / pre >
< / td >
< / tr >
2017-11-07 08:22:27 +00:00
< tr >
2024-01-22 02:40:41 +00:00
< td > Negation< / td >
< td > < pre > {#syntax#}-a{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Integers#}< / li >
< li > {#link|Floats#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > Can cause {#link|overflow|Default Operations#} for integers.< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}-1 == 0 - 1{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Wrapping Negation< / td >
< td > < pre > {#syntax#}-%a{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Integers#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
< ul >
2024-01-22 02:40:41 +00:00
< li > Twos-complement wrapping behavior.< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
2024-03-04 09:15:57 +00:00
< pre > {#syntax#}-%@as(i8, -128) == -128{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Multiplication< / td >
< td > < pre > {#syntax#}a * b
a *= b{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Integers#}< / li >
< li > {#link|Floats#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
2024-01-22 02:40:41 +00:00
< td >
2017-11-07 08:22:27 +00:00
< ul >
2018-01-23 04:06:07 +00:00
< li > Can cause {#link|overflow|Default Operations#} for integers.< / li >
2018-08-20 18:21:58 +00:00
< li > Invokes {#link|Peer Type Resolution#} for the operands.< / li >
< li > See also {#link|@mulWithOverflow#}.< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}2 * 5 == 10{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Wrapping Multiplication< / td >
< td > < pre > {#syntax#}a *% b
a *%= b{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Integers#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
2024-01-22 02:40:41 +00:00
< td >
2017-11-07 08:22:27 +00:00
< ul >
2024-01-22 02:40:41 +00:00
< li > Twos-complement wrapping behavior.< / li >
2018-08-20 18:21:58 +00:00
< li > Invokes {#link|Peer Type Resolution#} for the operands.< / li >
< li > See also {#link|@mulWithOverflow#}.< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
2019-12-03 10:56:50 +00:00
< pre > {#syntax#}@as(u8, 200) *% 2 == 144{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
2021-09-09 03:59:55 +00:00
< tr >
2024-01-22 02:40:41 +00:00
< td > Saturating Multiplication< / td >
< td > < pre > {#syntax#}a *| b
a *|= b{#endsyntax#}< / pre > < / td >
2021-09-09 03:59:55 +00:00
< td >
< ul >
< li > {#link|Integers#}< / li >
< / ul >
< / td >
2024-01-22 02:40:41 +00:00
< td >
2021-09-09 03:59:55 +00:00
< ul >
< li > Invokes {#link|Peer Type Resolution#} for the operands.< / li >
< / ul >
< / td >
< td >
< pre > {#syntax#}@as(u8, 200) *| 2 == 255{#endsyntax#}< / pre >
< / td >
< / tr >
2017-11-07 08:22:27 +00:00
< tr >
2024-01-22 02:40:41 +00:00
< td > Division< / td >
< td > < pre > {#syntax#}a / b
a /= b{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Integers#}< / li >
< li > {#link|Floats#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
2024-01-22 02:40:41 +00:00
< td >
2017-11-07 08:22:27 +00:00
< ul >
2018-01-23 04:06:07 +00:00
< li > Can cause {#link|overflow|Default Operations#} for integers.< / li >
< li > Can cause {#link|Division by Zero#} for integers.< / li >
< li > Can cause {#link|Division by Zero#} for floats in {#link|FloatMode.Optimized Mode|Floating Point Operations#}.< / li >
2020-12-18 13:04:03 +00:00
< li > Signed integer operands must be comptime-known and positive. In other cases, use
2018-01-23 04:06:07 +00:00
{#link|@divTrunc#},
{#link|@divFloor#}, or
2020-12-18 13:04:03 +00:00
{#link|@divExact#} instead.
2017-11-07 08:22:27 +00:00
< / li >
2018-08-20 18:21:58 +00:00
< li > Invokes {#link|Peer Type Resolution#} for the operands.< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}10 / 5 == 2{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Remainder Division< / td >
< td > < pre > {#syntax#}a % b
a %= b{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Integers#}< / li >
< li > {#link|Floats#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
2024-01-22 02:40:41 +00:00
< td >
2017-11-07 08:22:27 +00:00
< ul >
2018-01-23 04:06:07 +00:00
< li > Can cause {#link|Division by Zero#} for integers.< / li >
< li > Can cause {#link|Division by Zero#} for floats in {#link|FloatMode.Optimized Mode|Floating Point Operations#}.< / li >
2020-12-18 13:04:03 +00:00
< li > Signed or floating-point operands must be comptime-known and positive. In other cases, use
2018-01-23 04:06:07 +00:00
{#link|@rem#} or
2020-12-18 13:04:03 +00:00
{#link|@mod#} instead.
2017-11-07 08:22:27 +00:00
< / li >
2018-08-20 18:21:58 +00:00
< li > Invokes {#link|Peer Type Resolution#} for the operands.< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}10 % 3 == 1{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Bit Shift Left< / td >
< td > < pre > {#syntax#}a < < b
a < < = b{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Integers#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
2024-01-22 02:40:41 +00:00
< td >
2017-11-07 08:22:27 +00:00
< ul >
2024-01-22 02:40:41 +00:00
< li > Moves all bits to the left, inserting new zeroes at the
least-significant bit.< / li >
< li > {#syntax#}b{#endsyntax#} must be
{#link|comptime-known|comptime#} or have a type with log2 number
of bits as {#syntax#}a{#endsyntax#}.< / li >
2018-01-23 04:06:07 +00:00
< li > See also {#link|@shlExact#}.< / li >
< li > See also {#link|@shlWithOverflow#}.< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
2024-01-22 02:40:41 +00:00
< pre > {#syntax#}0b1 < < 8 = = 0b100000000 { # endsyntax # } < / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
2021-09-09 03:59:55 +00:00
< tr >
2024-01-22 02:40:41 +00:00
< td > Saturating Bit Shift Left< / td >
< td > < pre > {#syntax#}a < < | b
a < < |= b{#endsyntax#}< / pre > < / td >
2021-09-09 03:59:55 +00:00
< td >
< ul >
< li > {#link|Integers#}< / li >
< / ul >
< / td >
2024-01-22 02:40:41 +00:00
< td >
2021-09-09 03:59:55 +00:00
< ul >
< li > See also {#link|@shlExact#}.< / li >
< li > See also {#link|@shlWithOverflow#}.< / li >
< / ul >
< / td >
< td >
< pre > {#syntax#}@as(u8, 1) < < | 8 == 255{#endsyntax#}< / pre >
< / td >
< / tr >
2017-11-07 08:22:27 +00:00
< tr >
2024-01-22 02:40:41 +00:00
< td > Bit Shift Right< / td >
< td > < pre > {#syntax#}a >> b
a >>= b{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Integers#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
2024-01-22 02:40:41 +00:00
< td >
2017-11-07 08:22:27 +00:00
< ul >
2024-01-22 02:40:41 +00:00
< li > Moves all bits to the right, inserting zeroes at the most-significant bit.< / li >
< li > {#syntax#}b{#endsyntax#} must be
{#link|comptime-known|comptime#} or have a type with log2 number
of bits as {#syntax#}a{#endsyntax#}.< / li >
2018-01-23 04:06:07 +00:00
< li > See also {#link|@shrExact#}.< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
2024-01-22 02:40:41 +00:00
< pre > {#syntax#}0b1010 >> 1 == 0b101{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Bitwise And< / td >
< td > < pre > {#syntax#}a & b
a & = b{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Integers#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
2024-01-22 02:40:41 +00:00
< td >
2018-08-20 18:21:58 +00:00
< ul >
< li > Invokes {#link|Peer Type Resolution#} for the operands.< / li >
< / ul >
2017-11-07 08:22:27 +00:00
< / td >
< td >
2018-10-05 14:21:08 +00:00
< pre > {#syntax#}0b011 & 0b101 == 0b001{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Bitwise Or< / td >
< td > < pre > {#syntax#}a | b
a |= b{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Integers#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
2024-01-22 02:40:41 +00:00
< td >
2018-08-20 18:21:58 +00:00
< ul >
< li > Invokes {#link|Peer Type Resolution#} for the operands.< / li >
< / ul >
2017-11-07 08:22:27 +00:00
< / td >
< td >
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}0b010 | 0b100 == 0b110{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Bitwise Xor< / td >
< td > < pre > {#syntax#}a ^ b
a ^= b{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Integers#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
2024-01-22 02:40:41 +00:00
< td >
2018-08-20 18:21:58 +00:00
< ul >
< li > Invokes {#link|Peer Type Resolution#} for the operands.< / li >
< / ul >
2017-11-07 08:22:27 +00:00
< / td >
< td >
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}0b011 ^ 0b101 == 0b110{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Bitwise Not< / td >
< td > < pre > {#syntax#}~a{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Integers#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
2024-01-22 02:40:41 +00:00
< td > < / td >
2017-11-07 08:22:27 +00:00
< td >
2019-12-03 10:56:50 +00:00
< pre > {#syntax#}~@as(u8, 0b10101111) == 0b01010000{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Defaulting Optional Unwrap< / td >
< td > < pre > {#syntax#}a orelse b{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-06-10 03:42:14 +00:00
< li > {#link|Optionals#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
2018-09-14 14:35:03 +00:00
< td > If {#syntax#}a{#endsyntax#} is {#syntax#}null{#endsyntax#},
2024-01-22 02:40:41 +00:00
returns {#syntax#}b{#endsyntax#} ("default value"),
otherwise returns the unwrapped value of {#syntax#}a{#endsyntax#}.
Note that {#syntax#}b{#endsyntax#} may be a value of type {#link|noreturn#}.
2017-11-07 08:22:27 +00:00
< / td >
< td >
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}const value: ?u32 = null;
2018-06-10 05:13:51 +00:00
const unwrapped = value orelse 1234;
2018-09-14 14:35:03 +00:00
unwrapped == 1234{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Optional Unwrap< / td >
< td > < pre > {#syntax#}a.?{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-06-10 03:42:14 +00:00
< li > {#link|Optionals#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
Equivalent to:
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}a orelse unreachable{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< td >
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}const value: ?u32 = 5678;
value.? == 5678{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Defaulting Error Unwrap< / td >
< td > < pre > {#syntax#}a catch b
a catch |err| b{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Error Unions|Errors#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
2018-11-13 13:08:37 +00:00
< td > If {#syntax#}a{#endsyntax#} is an {#syntax#}error{#endsyntax#},
2024-01-22 02:40:41 +00:00
returns {#syntax#}b{#endsyntax#} ("default value"),
otherwise returns the unwrapped value of {#syntax#}a{#endsyntax#}.
Note that {#syntax#}b{#endsyntax#} may be a value of type {#link|noreturn#}.
{#syntax#}err{#endsyntax#} is the {#syntax#}error{#endsyntax#} and is in scope of the expression {#syntax#}b{#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / td >
< td >
2018-11-13 13:08:37 +00:00
< pre > {#syntax#}const value: anyerror!u32 = error.Broken;
2018-01-07 22:28:20 +00:00
const unwrapped = value catch 1234;
2018-09-14 14:35:03 +00:00
unwrapped == 1234{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Logical And< / td >
< td > < pre > {#syntax#}a and b{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|bool|Primitive Types#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
2024-01-22 02:40:41 +00:00
If {#syntax#}a{#endsyntax#} is {#syntax#}false{#endsyntax#}, returns {#syntax#}false{#endsyntax#}
without evaluating {#syntax#}b{#endsyntax#}. Otherwise, returns {#syntax#}b{#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / td >
< td >
2019-09-22 15:13:36 +00:00
< pre > {#syntax#}(false and true) == false{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Logical Or< / td >
< td > < pre > {#syntax#}a or b{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|bool|Primitive Types#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
2024-01-22 02:40:41 +00:00
If {#syntax#}a{#endsyntax#} is {#syntax#}true{#endsyntax#},
returns {#syntax#}true{#endsyntax#} without evaluating
{#syntax#}b{#endsyntax#}. Otherwise, returns
{#syntax#}b{#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / td >
< td >
2021-12-10 09:22:32 +00:00
< pre > {#syntax#}(false or true) == true{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Boolean Not< / td >
< td > < pre > {#syntax#}!a{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|bool|Primitive Types#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
2024-01-22 02:40:41 +00:00
< td > < / td >
2017-11-07 08:22:27 +00:00
< td >
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}!false == true{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Equality< / td >
< td > < pre > {#syntax#}a == b{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Integers#}< / li >
< li > {#link|Floats#}< / li >
< li > {#link|bool|Primitive Types#}< / li >
< li > {#link|type|Primitive Types#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
2018-09-14 14:35:03 +00:00
Returns {#syntax#}true{#endsyntax#} if a and b are equal, otherwise returns {#syntax#}false{#endsyntax#}.
2018-08-20 18:21:58 +00:00
Invokes {#link|Peer Type Resolution#} for the operands.
2017-11-07 08:22:27 +00:00
< / td >
< td >
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}(1 == 1) == true{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Null Check< / td >
< td > < pre > {#syntax#}a == null{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-06-10 03:42:14 +00:00
< li > {#link|Optionals#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
2018-09-14 14:35:03 +00:00
Returns {#syntax#}true{#endsyntax#} if a is {#syntax#}null{#endsyntax#}, otherwise returns {#syntax#}false{#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / td >
< td >
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}const value: ?u32 = null;
2024-01-16 17:50:47 +00:00
(value == null) == true{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Inequality< / td >
< td > < pre > {#syntax#}a != b{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Integers#}< / li >
< li > {#link|Floats#}< / li >
< li > {#link|bool|Primitive Types#}< / li >
< li > {#link|type|Primitive Types#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
2018-09-14 14:35:03 +00:00
Returns {#syntax#}false{#endsyntax#} if a and b are equal, otherwise returns {#syntax#}true{#endsyntax#}.
2018-08-20 18:21:58 +00:00
Invokes {#link|Peer Type Resolution#} for the operands.
2017-11-07 08:22:27 +00:00
< / td >
< td >
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}(1 != 1) == false{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
2024-01-16 17:50:47 +00:00
< tr >
2024-01-22 02:40:41 +00:00
< td > Non-Null Check< / td >
< td > < pre > {#syntax#}a != null{#endsyntax#}< / pre > < / td >
2024-01-16 17:50:47 +00:00
< td >
< ul >
< li > {#link|Optionals#}< / li >
< / ul >
< / td >
< td >
Returns {#syntax#}false{#endsyntax#} if a is {#syntax#}null{#endsyntax#}, otherwise returns {#syntax#}true{#endsyntax#}.
< / td >
< td >
< pre > {#syntax#}const value: ?u32 = null;
(value != null) == false{#endsyntax#}< / pre >
< / td >
< / tr >
2017-11-07 08:22:27 +00:00
< tr >
2024-01-22 02:40:41 +00:00
< td > Greater Than< / td >
< td > < pre > {#syntax#}a > b{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Integers#}< / li >
< li > {#link|Floats#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
2018-09-14 14:35:03 +00:00
Returns {#syntax#}true{#endsyntax#} if a is greater than b, otherwise returns {#syntax#}false{#endsyntax#}.
2018-08-20 18:21:58 +00:00
Invokes {#link|Peer Type Resolution#} for the operands.
2017-11-07 08:22:27 +00:00
< / td >
< td >
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}(2 > 1) == true{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Greater or Equal< / td >
< td > < pre > {#syntax#}a >= b{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Integers#}< / li >
< li > {#link|Floats#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
2018-09-14 14:35:03 +00:00
Returns {#syntax#}true{#endsyntax#} if a is greater than or equal to b, otherwise returns {#syntax#}false{#endsyntax#}.
2018-08-20 18:21:58 +00:00
Invokes {#link|Peer Type Resolution#} for the operands.
2017-11-07 08:22:27 +00:00
< / td >
< td >
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}(2 >= 1) == true{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Less Than< / td >
< td > < pre > {#syntax#}a < b { # endsyntax # } < / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Integers#}< / li >
< li > {#link|Floats#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
2018-09-14 14:35:03 +00:00
Returns {#syntax#}true{#endsyntax#} if a is less than b, otherwise returns {#syntax#}false{#endsyntax#}.
2018-08-20 18:21:58 +00:00
Invokes {#link|Peer Type Resolution#} for the operands.
2017-11-07 08:22:27 +00:00
< / td >
< td >
2024-03-04 09:15:57 +00:00
< pre > {#syntax#}(1 < 2 ) = = true { # endsyntax # } < / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Lesser or Equal< / td >
< td > < pre > {#syntax#}a < = b{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Integers#}< / li >
< li > {#link|Floats#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
2018-09-14 14:35:03 +00:00
Returns {#syntax#}true{#endsyntax#} if a is less than or equal to b, otherwise returns {#syntax#}false{#endsyntax#}.
2018-08-20 18:21:58 +00:00
Invokes {#link|Peer Type Resolution#} for the operands.
2017-11-07 08:22:27 +00:00
< / td >
< td >
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}(1 < = 2) == true{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Array Concatenation< / td >
< td > < pre > {#syntax#}a ++ b{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Arrays#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
< ul >
2023-07-23 18:24:32 +00:00
< li > Only available when the lengths of both {#syntax#}a{#endsyntax#} and {#syntax#}b{#endsyntax#} are {#link|compile-time known|comptime#}.< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}const mem = @import("std").mem;
2019-06-09 23:24:24 +00:00
const array1 = [_]u32{1,2};
const array2 = [_]u32{3,4};
2017-11-07 08:22:27 +00:00
const together = array1 ++ array2;
2020-05-10 12:35:21 +00:00
mem.eql(u32, & together, & [_]u32{1,2,3,4}){#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Array Multiplication< / td >
< td > < pre > {#syntax#}a ** b{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Arrays#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
< ul >
2023-07-23 18:24:32 +00:00
< li > Only available when the length of {#syntax#}a{#endsyntax#} and {#syntax#}b{#endsyntax#} are {#link|compile-time known|comptime#}.< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}const mem = @import("std").mem;
2017-11-07 08:22:27 +00:00
const pattern = "ab" ** 3;
2018-09-14 14:35:03 +00:00
mem.eql(u8, pattern, "ababab"){#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Pointer Dereference< / td >
< td > < pre > {#syntax#}a.*{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Pointers#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< / td >
< td >
Pointer dereference.
< / td >
< td >
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}const x: u32 = 1234;
const ptr = &x;
2019-03-22 17:57:00 +00:00
ptr.* == 1234{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
< tr >
2024-01-22 02:40:41 +00:00
< td > Address Of< / td >
< td > < pre > {#syntax#}& a{#endsyntax#}< / pre > < / td >
2017-11-07 08:22:27 +00:00
< td >
All types
< / td >
< td >
< / td >
< td >
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}const x: u32 = 1234;
const ptr = &x;
2019-03-22 17:57:00 +00:00
ptr.* == 1234{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< / td >
< / tr >
2018-06-13 15:09:41 +00:00
< tr >
2024-01-22 02:40:41 +00:00
< td > Error Set Merge< / td >
< td > < pre > {#syntax#}a || b{#endsyntax#}< / pre > < / td >
2018-06-13 15:09:41 +00:00
< td >
< ul >
< li > {#link|Error Set Type#}< / li >
< / ul >
< / td >
< td >
{#link|Merging Error Sets#}
< / td >
< td >
2018-11-13 13:08:37 +00:00
< pre > {#syntax#}const A = error{One};
const B = error{Two};
(A || B) == error{One, Two}{#endsyntax#}< / pre >
2018-06-13 15:09:41 +00:00
< / td >
< / tr >
2021-08-11 09:01:03 +00:00
< / tbody >
2017-11-07 08:22:27 +00:00
< / table >
2018-01-30 04:33:38 +00:00
< / div >
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Precedence#}
2021-05-25 04:09:28 +00:00
< pre > {#syntax#}x() x[] x.y x.* x.?
2018-02-08 08:02:41 +00:00
a!b
2021-05-25 04:09:28 +00:00
x{}
2020-12-09 06:58:24 +00:00
!x -x -%x ~x & x ?x
2021-09-09 03:59:55 +00:00
* / % ** *% *| ||
+ - ++ +% -% +| -|
< < >> < < |
2021-05-24 20:58:20 +00:00
& ^ | orelse catch
2018-09-14 14:35:03 +00:00
== != < > < = >=
2017-11-07 08:22:27 +00:00
and
or
2021-09-09 03:59:55 +00:00
= *= *%= *|= /= %= += +%= +|= -= -%= -|= < < = < < |= >>= & = ^= |={#endsyntax#}< / pre >
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_close#}
{#header_open|Arrays#}
2024-04-25 00:41:47 +00:00
{#code|test_arrays.zig#}
2017-11-07 08:22:27 +00:00
2019-07-16 17:13:21 +00:00
{#see_also|for|Slices#}
2019-07-15 22:20:56 +00:00
2019-07-16 17:13:21 +00:00
{#header_open|Multidimensional Arrays#}
< p >
2021-09-24 17:39:20 +00:00
Multidimensional arrays can be created by nesting arrays:
2019-07-16 17:13:21 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_multidimensional_arrays.zig#}
2019-07-16 17:13:21 +00:00
{#header_close#}
2019-11-20 22:11:07 +00:00
2019-11-24 03:29:12 +00:00
{#header_open|Sentinel-Terminated Arrays#}
2019-11-20 22:11:07 +00:00
< p >
2020-01-11 21:59:19 +00:00
The syntax {#syntax#}[N:x]T{#endsyntax#} describes an array which has a sentinel element of value {#syntax#}x{#endsyntax#} at the
2023-09-21 14:50:48 +00:00
index corresponding to the length {#syntax#}N{#endsyntax#}.
2019-11-20 22:11:07 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_null_terminated_array.zig#}
2023-09-21 14:50:48 +00:00
2019-11-24 03:29:12 +00:00
{#see_also|Sentinel-Terminated Pointers|Sentinel-Terminated Slices#}
2019-11-20 22:11:07 +00:00
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2019-01-31 04:36:52 +00:00
{#header_open|Vectors#}
< p >
2022-03-14 05:21:16 +00:00
A vector is a group of booleans, {#link|Integers#}, {#link|Floats#}, or
{#link|Pointers#} which are operated on in parallel, using SIMD instructions if possible.
Vector types are created with the builtin function {#link|@Vector#}.
2019-01-31 04:36:52 +00:00
< / p >
< p >
2022-03-14 05:21:16 +00:00
Vectors support the same builtin operators as their underlying base types.
These operations are performed element-wise, and return a vector of the same length
as the input vectors. This includes:
2021-12-19 06:57:00 +00:00
< / p >
2021-12-19 04:40:57 +00:00
< ul >
2022-02-04 19:27:50 +00:00
< li > Arithmetic ({#syntax#}+{#endsyntax#}, {#syntax#}-{#endsyntax#}, {#syntax#}/{#endsyntax#}, {#syntax#}*{#endsyntax#},
{#syntax#}@divFloor{#endsyntax#}, {#syntax#}@sqrt{#endsyntax#}, {#syntax#}@ceil{#endsyntax#},
2021-12-19 04:40:57 +00:00
{#syntax#}@log{#endsyntax#}, etc.)< / li >
2022-02-04 19:27:50 +00:00
< li > Bitwise operators ({#syntax#}>>{#endsyntax#}, {#syntax#}< < {#endsyntax#}, {#syntax#}& {#endsyntax#},
2021-12-19 04:40:57 +00:00
{#syntax#}|{#endsyntax#}, {#syntax#}~{#endsyntax#}, etc.)< / li >
< li > Comparison operators ({#syntax#}< {#endsyntax#}, {#syntax#}>{#endsyntax#}, {#syntax#}=={#endsyntax#}, etc.)< / li >
< / ul >
2019-01-31 04:36:52 +00:00
< p >
2022-03-14 05:21:16 +00:00
It is prohibited to use a math operator on a mixture of scalars (individual numbers)
and vectors. Zig provides the {#link|@splat#} builtin to easily convert from scalars
to vectors, and it supports {#link|@reduce#} and array indexing syntax to convert
from vectors to scalars. Vectors also support assignment to and from fixed-length
2022-12-09 11:02:27 +00:00
arrays with comptime-known length.
2019-01-31 04:36:52 +00:00
< / p >
2021-12-19 04:40:57 +00:00
< p >
For rearranging elements within and between vectors, Zig provides the {#link|@shuffle#} and {#link|@select#} functions.
< / p >
< p >
2022-02-04 19:27:50 +00:00
Operations on vectors shorter than the target machine's native SIMD size will typically compile to single SIMD
instructions, while vectors longer than the target machine's native SIMD size will compile to multiple SIMD
instructions. If a given operation doesn't have SIMD support on the target architecture, the compiler will default
to operating on each vector element one at a time. Zig supports any comptime-known vector length up to 2^32-1,
although small powers of two (2-64) are most typical. Note that excessively long vector lengths (e.g. 2^20) may
2021-12-19 04:40:57 +00:00
result in compiler crashes on current versions of Zig.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_vector.zig#}
2021-12-19 04:40:57 +00:00
< p >
TODO talk about C ABI interop< br >
TODO consider suggesting std.MultiArrayList
< / p >
{#see_also|@splat|@shuffle|@select|@reduce#}
2022-02-04 19:27:50 +00:00
2019-01-31 04:36:52 +00:00
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|Pointers#}
2018-10-05 02:51:36 +00:00
< p >
2021-03-13 01:10:55 +00:00
Zig has two kinds of pointers: single-item and many-item.
2018-10-05 02:51:36 +00:00
< / p >
< ul >
2021-03-13 01:10:55 +00:00
< li > {#syntax#}*T{#endsyntax#} - single-item pointer to exactly one item.
2018-10-05 02:51:36 +00:00
< ul >
< li > Supports deref syntax: {#syntax#}ptr.*{#endsyntax#}< / li >
2024-07-17 14:06:34 +00:00
< li > Supports slice syntax: {#syntax#}ptr[0..1]{#endsyntax#}< / li >
2024-07-15 18:18:38 +00:00
< li > Supports pointer subtraction: {#syntax#}ptr - ptr{#endsyntax#}< / li >
2018-10-05 02:51:36 +00:00
< / ul >
< / li >
2021-03-13 01:10:55 +00:00
< li > {#syntax#}[*]T{#endsyntax#} - many-item pointer to unknown number of items.
2018-10-05 02:51:36 +00:00
< ul >
< li > Supports index syntax: {#syntax#}ptr[i]{#endsyntax#}< / li >
2023-11-07 09:19:59 +00:00
< li > Supports slice syntax: {#syntax#}ptr[start..end]{#endsyntax#} and {#syntax#}ptr[start..]{#endsyntax#}< / li >
2024-07-15 18:18:38 +00:00
< li > Supports pointer-integer arithmetic: {#syntax#}ptr + int{#endsyntax#}, {#syntax#}ptr - int{#endsyntax#}< / li >
< li > Supports pointer subtraction: {#syntax#}ptr - ptr{#endsyntax#}< / li >
2018-10-05 02:51:36 +00:00
< / ul >
2024-07-15 18:18:38 +00:00
{#syntax#}T{#endsyntax#} must have a known size, which means that it cannot be
{#syntax#}anyopaque{#endsyntax#} or any other {#link|opaque type|opaque#}.
2018-10-05 02:51:36 +00:00
< / li >
< / ul >
< p > These types are closely related to {#link|Arrays#} and {#link|Slices#}:< / p >
< ul >
2021-03-13 01:10:55 +00:00
< li > {#syntax#}*[N]T{#endsyntax#} - pointer to N items, same as single-item pointer to an array.
2018-10-05 02:51:36 +00:00
< ul >
< li > Supports index syntax: {#syntax#}array_ptr[i]{#endsyntax#}< / li >
< li > Supports slice syntax: {#syntax#}array_ptr[start..end]{#endsyntax#}< / li >
< li > Supports len property: {#syntax#}array_ptr.len{#endsyntax#}< / li >
2024-07-15 18:18:38 +00:00
< li > Supports pointer subtraction: {#syntax#}array_ptr - array_ptr{#endsyntax#}< / li >
2018-10-05 02:51:36 +00:00
< / ul >
< / li >
< / ul >
< ul >
2022-09-01 01:53:00 +00:00
< li > {#syntax#}[]T{#endsyntax#} - is a slice (a fat pointer, which contains a pointer of type {#syntax#}[*]T{#endsyntax#} and a length).
2018-10-05 02:51:36 +00:00
< ul >
< li > Supports index syntax: {#syntax#}slice[i]{#endsyntax#}< / li >
< li > Supports slice syntax: {#syntax#}slice[start..end]{#endsyntax#}< / li >
< li > Supports len property: {#syntax#}slice.len{#endsyntax#}< / li >
< / ul >
< / li >
< / ul >
< p > Use {#syntax#}& x{#endsyntax#} to obtain a single-item pointer:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_single_item_pointer.zig#}
2017-11-07 08:22:27 +00:00
2022-07-23 10:57:40 +00:00
< p >
Zig supports pointer arithmetic. It's better to assign the pointer to {#syntax#}[*]T{#endsyntax#} and increment that variable. For example, directly incrementing the pointer from a slice will corrupt it.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_pointer_arithmetic.zig#}
2022-07-23 10:57:40 +00:00
2018-10-05 02:51:36 +00:00
< p >
2019-11-24 03:29:12 +00:00
In Zig, we generally prefer {#link|Slices#} rather than {#link|Sentinel-Terminated Pointers#}.
2018-10-05 02:51:36 +00:00
You can turn an array or pointer into a slice using slice syntax.
< / p >
< p >
Slices have bounds checking and are therefore protected
against this kind of undefined behavior. This is one reason
we prefer slices to pointers.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_slice_bounds.zig#}
2017-11-07 08:22:27 +00:00
2019-02-22 15:56:49 +00:00
< p > Pointers work at compile-time too, as long as the code does not depend on
2018-10-05 02:51:36 +00:00
an undefined memory layout:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_pointers.zig#}
2023-06-15 07:14:16 +00:00
< p > To convert an integer address into a pointer, use {#syntax#}@ptrFromInt{#endsyntax#}.
To convert a pointer to an integer, use {#syntax#}@intFromPtr{#endsyntax#}:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_integer_pointer_conversion.zig#}
2018-10-05 02:51:36 +00:00
< p > Zig is able to preserve memory addresses in comptime code, as long as
the pointer is never dereferenced:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_pointer_conversion.zig#}
2023-06-15 07:14:16 +00:00
{#see_also|Optional Pointers|@ptrFromInt|@intFromPtr|C Pointers#}
2018-10-05 02:51:36 +00:00
{#header_open|volatile#}
< p > Loads and stores are assumed to not have side effects. If a given load or store
should have side effects, such as Memory Mapped Input/Output (MMIO), use {#syntax#}volatile{#endsyntax#}.
In the following code, loads and stores with {#syntax#}mmio_ptr{#endsyntax#} are guaranteed to all happen
and in the same order as in source code:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_volatile.zig#}
2017-11-07 08:22:27 +00:00
2018-10-05 02:51:36 +00:00
< p >
Note that {#syntax#}volatile{#endsyntax#} is unrelated to concurrency and {#link|Atomics#}.
If you see code that is using {#syntax#}volatile{#endsyntax#} for something other than Memory Mapped
Input/Output, it is probably a bug.
< / p >
{#header_close#}
< p >
2024-01-19 03:58:35 +00:00
{#link|@ptrCast#} converts a pointer's element type to another. This
creates a new pointer that can cause undetectable illegal behavior
depending on the loads and stores that pass through it. Generally, other
kinds of type conversions are preferable to
{#syntax#}@ptrCast{#endsyntax#} if possible.
2018-10-05 02:51:36 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_pointer_casting.zig#}
2017-11-07 08:22:27 +00:00
2018-01-17 04:19:05 +00:00
{#header_open|Alignment#}
2017-11-07 08:22:27 +00:00
< p >
Each type has an < strong > alignment< / strong > - a number of bytes such that,
when a value of the type is loaded from or stored to memory,
the memory address must be evenly divisible by this number. You can use
2018-01-23 04:06:07 +00:00
{#link|@alignOf#} to find out this value for any type.
2017-11-07 08:22:27 +00:00
< / p >
< p >
Alignment depends on the CPU architecture, but is always a power of two, and
2018-09-14 14:35:03 +00:00
less than {#syntax#}1 < < 29 { # endsyntax # } .
2017-11-07 08:22:27 +00:00
< / p >
< p >
In Zig, a pointer type has an alignment value. If the value is equal to the
alignment of the underlying type, it can be omitted from the type:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_variable_alignment.zig#}
2019-11-08 20:56:21 +00:00
< p > In the same way that a {#syntax#}*i32{#endsyntax#} can be {#link|coerced|Type Coercion#} to a
2018-09-14 14:35:03 +00:00
{#syntax#}*const i32{#endsyntax#}, a pointer with a larger alignment can be implicitly
2017-11-07 08:22:27 +00:00
cast to a pointer with a smaller alignment, but not vice versa.
< / p >
< p >
You can specify alignment on variables and functions. If you do this, then
pointers to them get the specified alignment:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_variable_func_alignment.zig#}
2024-03-16 15:46:45 +00:00
2017-11-07 08:22:27 +00:00
< p >
If you have a pointer or a slice that has a small alignment, but you know that it actually
2018-01-23 04:06:07 +00:00
has a bigger alignment, use {#link|@alignCast#} to change the
2017-11-07 08:22:27 +00:00
pointer into a more aligned pointer. This is a no-op at runtime, but inserts a
2018-01-23 04:06:07 +00:00
{#link|safety check|Incorrect Pointer Alignment#}:
2017-11-07 08:22:27 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_incorrect_pointer_alignment.zig#}
2017-11-07 08:22:27 +00:00
2018-01-17 04:19:05 +00:00
{#header_close#}
2019-03-25 16:55:45 +00:00
{#header_open|allowzero#}
< p >
This pointer attribute allows a pointer to have address zero. This is only ever needed on the
2019-05-31 02:38:26 +00:00
freestanding OS target, where the address zero is mappable. If you want to represent null pointers, use
2019-12-17 04:55:35 +00:00
{#link|Optional Pointers#} instead. {#link|Optional Pointers#} with {#syntax#}allowzero{#endsyntax#}
are not the same size as pointers. In this code example, if the pointer
did not have the {#syntax#}allowzero{#endsyntax#} attribute, this would be a
{#link|Pointer Cast Invalid Null#} panic:
2019-03-25 16:55:45 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_allowzero.zig#}
2019-03-25 16:55:45 +00:00
{#header_close#}
2019-11-20 22:11:07 +00:00
2019-11-24 03:29:12 +00:00
{#header_open|Sentinel-Terminated Pointers#}
2019-11-20 22:11:07 +00:00
< p >
2019-11-24 03:29:12 +00:00
The syntax {#syntax#}[*:x]T{#endsyntax#} describes a pointer that
has a length determined by a sentinel value. This provides protection
2019-11-20 22:11:07 +00:00
against buffer overflow and overreads.
< / p >
2024-04-25 00:41:47 +00:00
{#code|sentinel-terminated_pointer.zig#}
2019-11-20 22:11:07 +00:00
2019-11-24 03:29:12 +00:00
{#see_also|Sentinel-Terminated Slices|Sentinel-Terminated Arrays#}
2019-11-20 22:11:07 +00:00
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2019-02-14 23:59:20 +00:00
2018-01-17 04:19:05 +00:00
{#header_open|Slices#}
2023-10-27 23:11:56 +00:00
< p >
A slice is a pointer and a length. The difference between an array and
a slice is that the array's length is part of the type and known at
compile-time, whereas the slice's length is known at runtime.
2024-06-08 19:37:07 +00:00
Both can be accessed with the {#syntax#}len{#endsyntax#} field.
2023-10-27 23:11:56 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_basic_slices.zig#}
2017-11-07 08:22:27 +00:00
< p > This is one reason we prefer slices to pointers.< / p >
2024-04-25 00:41:47 +00:00
{#code|test_slices.zig#}
2017-11-07 08:22:27 +00:00
2018-01-17 05:22:33 +00:00
{#see_also|Pointers|for|Arrays#}
2019-11-20 22:11:07 +00:00
2019-11-24 03:29:12 +00:00
{#header_open|Sentinel-Terminated Slices#}
2019-11-20 22:11:07 +00:00
< p >
2022-12-09 11:02:27 +00:00
The syntax {#syntax#}[:x]T{#endsyntax#} is a slice which has a runtime-known length
2019-11-24 03:29:12 +00:00
and also guarantees a sentinel value at the element indexed by the length. The type does not
guarantee that there are no sentinel elements before that. Sentinel-terminated slices allow element
2019-11-20 22:11:07 +00:00
access to the {#syntax#}len{#endsyntax#} index.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_null_terminated_slice.zig#}
2019-11-20 22:11:07 +00:00
2021-12-04 01:37:48 +00:00
< p >
2022-02-04 19:27:50 +00:00
Sentinel-terminated slices can also be created using a variation of the slice syntax
{#syntax#}data[start..end :x]{#endsyntax#}, where {#syntax#}data{#endsyntax#} is a many-item pointer,
2021-12-04 01:37:48 +00:00
array or slice and {#syntax#}x{#endsyntax#} is the sentinel value.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_null_terminated_slicing.zig#}
2021-12-04 01:37:48 +00:00
< p >
2022-02-04 19:27:50 +00:00
Sentinel-terminated slicing asserts that the element in the sentinel position of the backing data is
2021-12-04 01:37:48 +00:00
actually the sentinel value. If this is not the case, safety-protected {#link|Undefined Behavior#} results.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_sentinel_mismatch.zig#}
2021-12-04 01:37:48 +00:00
2019-11-24 03:29:12 +00:00
{#see_also|Sentinel-Terminated Pointers|Sentinel-Terminated Arrays#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2019-11-20 22:11:07 +00:00
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|struct#}
2024-04-25 00:41:47 +00:00
{#code|test_structs.zig#}
2021-06-23 18:32:28 +00:00
2019-02-27 03:46:35 +00:00
2019-05-30 19:35:30 +00:00
{#header_open|Default Field Values#}
< p >
2024-03-15 23:37:13 +00:00
Each struct field may have an expression indicating the default field
value. Such expressions are executed at {#link|comptime#}, and allow the
field to be omitted in a struct literal expression:
2019-05-30 19:35:30 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|struct_default_field_values.zig#}
2019-05-30 19:35:30 +00:00
2024-08-31 04:12:17 +00:00
{#header_open|Faulty Default Field Values#}
2024-03-15 23:37:13 +00:00
< p >
Default field values are only appropriate when the data invariants of a struct
cannot be violated by omitting that field from an initialization.
< / p >
< p >
For example, here is an inappropriate use of default struct field initialization:
< / p >
2024-04-25 00:41:47 +00:00
{#code|bad_default_value.zig#}
2024-03-15 23:37:13 +00:00
< p >
Above you can see the danger of ignoring this principle. The default
field values caused the data invariant to be violated, causing illegal
behavior.
< / p >
< p >
To fix this, remove the default values from all the struct fields, and provide
a named default value:
< / p >
2024-04-25 00:41:47 +00:00
{#code|struct_default_value.zig#}
2024-03-15 23:37:13 +00:00
< p > If a struct value requires a runtime-known value in order to be initialized
without violating data invariants, then use an initialization method that accepts
those runtime values, and populates the remaining fields.< / p >
2019-05-30 19:35:30 +00:00
{#header_close#}
2024-08-31 04:12:17 +00:00
{#header_close#}
2019-05-30 19:35:30 +00:00
2019-02-27 03:46:35 +00:00
{#header_open|extern struct#}
2023-10-30 23:27:12 +00:00
< p > An {#syntax#}extern struct{#endsyntax#} has in-memory layout matching
the C ABI for the target.< / p >
< p > If well-defined in-memory layout is not required, {#link|struct#} is a better choice
because it places fewer restrictions on the compiler.< / p >
< p > See {#link|packed struct#} for a struct that has the ABI of its backing integer,
which can be useful for modeling flags.< / p >
2019-02-27 03:46:35 +00:00
{#see_also|extern union|extern enum#}
{#header_close#}
2018-09-21 18:15:58 +00:00
{#header_open|packed struct#}
2019-02-22 15:56:49 +00:00
< p >
Unlike normal structs, {#syntax#}packed{#endsyntax#} structs have guaranteed in-memory layout:
< / p >
< ul >
2022-11-17 06:41:54 +00:00
< li > Fields remain in the order declared, least to most significant.< / li >
2019-02-22 15:56:49 +00:00
< li > There is no padding between fields.< / li >
< li > Zig supports arbitrary width {#link|Integers#} and although normally, integers with fewer
than 8 bits will still use 1 byte of memory, in packed structs, they use
exactly their bit width.
< / li >
< li > {#syntax#}bool{#endsyntax#} fields use exactly 1 bit.< / li >
2021-04-23 02:21:50 +00:00
< li > An {#link|enum#} field uses exactly the bit width of its integer tag type.< / li >
2019-02-22 15:56:49 +00:00
< li > A {#link|packed union#} field uses exactly the bit width of the union field with
the largest bit width.< / li >
2024-10-13 03:59:12 +00:00
< li > Packed structs support equality operators.< / li >
2019-02-22 15:56:49 +00:00
< / ul >
< p >
This means that a {#syntax#}packed struct{#endsyntax#} can participate
in a {#link|@bitCast#} or a {#link|@ptrCast#} to reinterpret memory.
This even works at {#link|comptime#}:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_packed_structs.zig#}
2019-02-22 15:56:49 +00:00
2024-04-08 03:01:30 +00:00
< p >
The backing integer is inferred from the fields' total bit width.
Optionally, it can be explicitly provided and enforced at compile time:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_missized_packed_struct.zig#}
2019-02-22 15:56:49 +00:00
< p >
Zig allows the address to be taken of a non-byte-aligned field:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_pointer_to_non-byte_aligned_field.zig#}
2019-02-22 15:56:49 +00:00
< p >
However, the pointer to a non-byte-aligned field has special properties and cannot
be passed when a normal pointer is expected:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_misaligned_pointer.zig#}
2019-02-22 15:56:49 +00:00
< p >
2021-09-24 17:39:20 +00:00
In this case, the function {#syntax#}bar{#endsyntax#} cannot be called because the pointer
2019-04-02 18:44:25 +00:00
to the non-ABI-aligned field mentions the bit offset, but the function expects an ABI-aligned pointer.
2019-02-22 15:56:49 +00:00
< / p >
< p >
2019-04-02 18:44:25 +00:00
Pointers to non-ABI-aligned fields share the same address as the other fields within their host integer:
2019-02-22 15:56:49 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_packed_struct_field_address.zig#}
2019-02-22 15:56:49 +00:00
< p >
2021-06-10 20:56:55 +00:00
This can be observed with {#link|@bitOffsetOf#} and {#link|offsetOf#}:
2019-02-22 15:56:49 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_bitOffsetOf_offsetOf.zig#}
2019-02-22 15:56:49 +00:00
< p >
2022-08-08 06:10:57 +00:00
Packed structs have the same alignment as their backing integer, however, overaligned
pointers to packed structs can override this:
2019-02-22 15:56:49 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_overaligned_packed_struct.zig#}
2022-08-08 06:10:57 +00:00
2019-02-22 15:56:49 +00:00
< p >
2021-04-13 12:53:44 +00:00
It's also possible to set alignment of struct fields:
2019-02-22 15:56:49 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_aligned_struct_fields.zig#}
2021-04-13 12:53:44 +00:00
2024-10-13 03:59:12 +00:00
< p >
Equating packed structs results in a comparison of the backing integer,
and only works for the `==` and `!=` operators.
< / p >
{#code|test_packed_struct_equality.zig#}
2019-02-22 15:56:49 +00:00
< p >
Using packed structs with {#link|volatile#} is problematic, and may be a compile error in the future.
For details on this subscribe to
< a href = "https://github.com/ziglang/zig/issues/1761" > this issue< / a > .
TODO update these docs with a recommendation on how to use packed structs with MMIO
(the use case for volatile packed structs) once this issue is resolved.
Don't worry, there will be a good solution for this use case in zig.
< / p >
2018-09-21 18:15:58 +00:00
{#header_close#}
2019-11-12 00:57:57 +00:00
{#header_open|Struct Naming#}
2018-09-03 15:32:39 +00:00
< p > Since all structs are anonymous, Zig infers the type name based on a few rules.< / p >
< ul >
< li > If the struct is in the initialization expression of a variable, it gets named after
that variable.< / li >
2018-09-14 14:35:03 +00:00
< li > If the struct is in the {#syntax#}return{#endsyntax#} expression, it gets named after
2018-09-03 15:32:39 +00:00
the function it is returning from, with the parameter values serialized.< / li >
2022-12-09 13:10:47 +00:00
< li > Otherwise, the struct gets a name such as < code > (filename.funcname.__struct_ID)< / code > .< / li >
2022-03-19 11:26:21 +00:00
< li > If the struct is declared inside another struct, it gets named after both the parent
struct and the name inferred by the previous rules, separated by a dot.< / li >
2018-09-03 15:32:39 +00:00
< / ul >
2024-04-25 00:41:47 +00:00
{#code|struct_name.zig#}
2018-09-03 15:32:39 +00:00
{#header_close#}
2019-11-12 00:57:57 +00:00
{#header_open|Anonymous Struct Literals#}
< p >
Zig allows omitting the struct type of a literal. When the result is {#link|coerced|Type Coercion#},
2022-12-09 10:53:26 +00:00
the struct literal will directly instantiate the {#link|result location|Result Location Semantics#},
with no copy:
2019-11-12 00:57:57 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_struct_result.zig#}
2019-11-12 00:57:57 +00:00
< p >
2022-12-09 10:53:26 +00:00
The struct type can be inferred. Here the {#link|result location|Result Location Semantics#}
does not include a type, and so Zig infers the type:
2019-11-12 00:57:57 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_anonymous_struct.zig#}
2019-11-12 00:57:57 +00:00
2022-12-10 03:10:55 +00:00
{#header_close#}
{#header_open|Tuples#}
2021-04-30 23:05:45 +00:00
< p >
Anonymous structs can be created without specifying field names, and are referred to as "tuples".
< / p >
< p >
The fields are implicitly named using numbers starting from 0. Because their names are integers,
2022-12-10 03:10:55 +00:00
they cannot be accessed with {#syntax#}.{#endsyntax#} syntax without also wrapping them in
{#syntax#}@""{#endsyntax#}. Names inside {#syntax#}@""{#endsyntax#} are always recognised as
{#link|identifiers|Identifiers#}.
2021-04-30 23:05:45 +00:00
< / p >
< p >
2022-12-10 03:10:55 +00:00
Like arrays, tuples have a .len field, can be indexed (provided the index is comptime-known)
and work with the ++ and ** operators. They can also be iterated over with {#link|inline for#}.
2021-04-30 23:05:45 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_tuples.zig#}
2019-11-12 00:57:57 +00:00
{#header_close#}
2018-01-17 05:22:33 +00:00
{#see_also|comptime|@fieldParentPtr#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|enum#}
2024-04-25 00:41:47 +00:00
{#code|test_enums.zig#}
2017-12-04 06:42:02 +00:00
2020-02-24 21:21:11 +00:00
{#see_also|@typeInfo|@tagName|@sizeOf#}
2019-07-04 03:09:58 +00:00
2018-01-23 03:24:07 +00:00
{#header_open|extern enum#}
< p >
By default, enums are not guaranteed to be compatible with the C ABI:
< / p >
2024-04-25 00:41:47 +00:00
{#code|enum_export_error.zig#}
2018-01-23 03:24:07 +00:00
< p >
2021-06-23 18:32:28 +00:00
For a C-ABI-compatible enum, provide an explicit tag type to
the enum:
2018-01-23 03:24:07 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|enum_export.zig#}
2018-01-23 03:24:07 +00:00
{#header_close#}
2019-07-04 03:09:58 +00:00
{#header_open|Enum Literals#}
< p >
Enum literals allow specifying the name of an enum field without specifying the enum type:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_enum_literals.zig#}
2019-07-04 03:09:58 +00:00
{#header_close#}
2020-01-15 21:05:52 +00:00
{#header_open|Non-exhaustive enum#}
< p >
2023-09-21 14:50:48 +00:00
A non-exhaustive enum can be created by adding a trailing {#syntax#}_{#endsyntax#} field.
The enum must specify a tag type and cannot consume every enumeration value.
2020-01-15 21:05:52 +00:00
< / p >
< p >
2023-06-15 07:14:16 +00:00
{#link|@enumFromInt#} on a non-exhaustive enum involves the safety semantics
2021-08-23 22:30:57 +00:00
of {#link|@intCast#} to the integer tag type, but beyond that always results in
a well-defined enum value.
2020-01-15 21:05:52 +00:00
< / p >
< p >
2023-09-21 14:50:48 +00:00
A switch on a non-exhaustive enum can include a {#syntax#}_{#endsyntax#} prong as an alternative to an {#syntax#}else{#endsyntax#} prong.
With a {#syntax#}_{#endsyntax#} prong the compiler errors if all the known tag names are not handled by the switch.
2020-01-15 21:05:52 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_switch_non-exhaustive.zig#}
2020-01-15 21:05:52 +00:00
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2019-07-04 03:09:58 +00:00
2018-01-17 04:19:05 +00:00
{#header_open|union#}
2019-02-27 03:46:35 +00:00
< p >
A bare {#syntax#}union{#endsyntax#} defines a set of possible types that a value
can be as a list of fields. Only one field can be active at a time.
The in-memory representation of bare unions is not guaranteed.
Bare unions cannot be used to reinterpret memory. For that, use {#link|@ptrCast#},
or use an {#link|extern union#} or a {#link|packed union#} which have
guaranteed in-memory layout.
{#link|Accessing the non-active field|Wrong Union Field Access#} is
safety-checked {#link|Undefined Behavior#}:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_wrong_union_access.zig#}
2019-02-27 03:46:35 +00:00
< p > You can activate another field by assigning the entire union:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_simple_union.zig#}
2019-02-27 03:46:35 +00:00
< p >
In order to use {#link|switch#} with a union, it must be a {#link|Tagged union#}.
< / p >
2019-07-05 18:10:50 +00:00
< p >
To initialize a union when the tag is a {#link|comptime#}-known name, see {#link|@unionInit#}.
< / p >
2019-02-27 03:46:35 +00:00
{#header_open|Tagged union#}
< p > Unions can be declared with an enum tag type.
This turns the union into a < em > tagged< / em > union, which makes it eligible
2021-01-11 18:04:38 +00:00
to use with {#link|switch#} expressions.
2023-01-25 08:26:41 +00:00
Tagged unions coerce to their tag type: {#link|Type Coercion: Unions and Enums#}.
2019-02-27 03:46:35 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_tagged_union.zig#}
2017-12-04 06:42:02 +00:00
2019-02-27 03:46:35 +00:00
< p > In order to modify the payload of a tagged union in a switch expression,
place a {#syntax#}*{#endsyntax#} before the variable name to make it a pointer:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_switch_modify_tagged_union.zig#}
2017-11-07 08:22:27 +00:00
2019-02-27 03:46:35 +00:00
< p >
Unions can be made to infer the enum tag type.
Further, unions can have methods just like structs and enums.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_union_method.zig#}
2018-04-08 22:03:02 +00:00
2019-02-27 03:46:35 +00:00
< p >
{#link|@tagName#} can be used to return a {#link|comptime#}
2021-04-27 22:57:28 +00:00
{#syntax#}[:0]const u8{#endsyntax#} value representing the field name:
2019-02-27 03:46:35 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_tagName.zig#}
2017-12-04 06:42:02 +00:00
2019-02-27 03:46:35 +00:00
{#header_close#}
{#header_open|extern union#}
2017-11-07 08:22:27 +00:00
< p >
2019-02-27 03:46:35 +00:00
An {#syntax#}extern union{#endsyntax#} has memory layout guaranteed to be compatible with
the target C ABI.
2017-11-07 08:22:27 +00:00
< / p >
2019-02-27 03:46:35 +00:00
{#see_also|extern struct#}
{#header_close#}
2019-02-22 15:56:49 +00:00
{#header_open|packed union#}
< p > A {#syntax#}packed union{#endsyntax#} has well-defined in-memory layout and is eligible
2021-08-11 09:01:03 +00:00
to be in a {#link|packed struct#}.< / p >
2019-02-22 15:56:49 +00:00
{#header_close#}
2019-11-12 00:57:57 +00:00
{#header_open|Anonymous Union Literals#}
< p > {#link|Anonymous Struct Literals#} syntax can be used to initialize unions without specifying
the type:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_anonymous_union.zig#}
2019-11-12 00:57:57 +00:00
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2019-02-22 15:56:49 +00:00
2020-10-17 04:29:33 +00:00
{#header_open|opaque#}
< p >
{#syntax#}opaque {}{#endsyntax#} declares a new type with an unknown (but non-zero) size and alignment.
It can contain declarations the same as {#link|structs|struct#}, {#link|unions|union#},
and {#link|enums|enum#}.
< / p >
< p >
This is typically used for type safety when interacting with C code that does not expose struct details.
Example:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_opaque.zig#}
2020-10-17 04:29:33 +00:00
{#header_close#}
2022-10-30 23:27:02 +00:00
{#header_open|Blocks#}
2018-08-28 00:59:28 +00:00
< p >
Blocks are used to limit the scope of variable declarations:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_blocks.zig#}
2018-09-14 14:35:03 +00:00
< p > Blocks are expressions. When labeled, {#syntax#}break{#endsyntax#} can be used
2018-08-28 00:59:28 +00:00
to return a value from the block:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_labeled_break.zig#}
2018-08-28 00:59:28 +00:00
2018-09-14 14:35:03 +00:00
< p > Here, {#syntax#}blk{#endsyntax#} can be any name.< / p >
2018-08-28 00:59:28 +00:00
{#see_also|Labeled while|Labeled for#}
2019-02-18 22:49:26 +00:00
{#header_open|Shadowing#}
2022-02-04 19:27:50 +00:00
< p > {#link|Identifiers#} are never allowed to "hide" other identifiers by using the same name:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_shadowing.zig#}
2019-02-18 22:49:26 +00:00
< p >
2022-02-04 19:27:50 +00:00
Because of this, when you read Zig code you can always rely on an identifier to consistently mean
the same thing within the scope it is defined. Note that you can, however, use the same name if
2021-10-23 04:13:35 +00:00
the scopes are separate:
2019-02-18 22:49:26 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_scopes.zig#}
2018-08-28 00:59:28 +00:00
{#header_close#}
2022-10-30 23:27:02 +00:00
{#header_open|Empty Blocks#}
< p > An empty block is equivalent to {#syntax#}void{}{#endsyntax#}:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_empty_block.zig#}
2022-10-30 23:27:02 +00:00
{#header_close#}
2019-02-18 22:49:26 +00:00
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|switch#}
2024-04-25 00:41:47 +00:00
{#code|test_switch.zig#}
2017-11-07 08:22:27 +00:00
2019-02-27 03:46:35 +00:00
< p >
{#syntax#}switch{#endsyntax#} can be used to capture the field values
of a {#link|Tagged union#}. Modifications to the field values can be
done by placing a {#syntax#}*{#endsyntax#} before the capture variable name,
turning it into a pointer.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_switch_tagged_union.zig#}
2017-11-07 08:22:27 +00:00
2018-01-17 05:22:33 +00:00
{#see_also|comptime|enum|@compileError|Compile Variables#}
2019-07-04 03:09:58 +00:00
{#header_open|Exhaustive Switching#}
< p >
When a {#syntax#}switch{#endsyntax#} expression does not have an {#syntax#}else{#endsyntax#} clause,
it must exhaustively list all the possible values. Failure to do so is a compile error:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_unhandled_enumeration_value.zig#}
2019-07-04 03:09:58 +00:00
2018-01-17 04:19:05 +00:00
{#header_close#}
2019-07-04 03:09:58 +00:00
{#header_open|Switching with Enum Literals#}
< p >
{#link|Enum Literals#} can be useful to use with {#syntax#}switch{#endsyntax#} to avoid
repetitively specifying {#link|enum#} or {#link|union#} types:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_exhaustive_switch.zig#}
2019-07-04 03:09:58 +00:00
2024-09-13 03:06:49 +00:00
{#header_close#}
{#header_open|Labeled switch#}
< p >
When a switch statement is labeled, it can be referenced from a
{#syntax#}break{#endsyntax#} or {#syntax#}continue{#endsyntax#}.
{#syntax#}break{#endsyntax#} will return a value from the {#syntax#}
switch{#endsyntax#}.
< / p >
< p >
A {#syntax#}continue{#endsyntax#} targeting a switch must have an
operand. When executed, it will jump to the matching prong, as if the
{#syntax#}switch{#endsyntax#} were executed again with the {#syntax#}
continue{#endsyntax#}'s operand replacing the initial switch value.
< / p >
{#code|test_switch_continue.zig#}
< p >
Semantically, this is equivalent to the following loop:
< / p >
{#code|test_switch_continue_equivalent.zig#}
< p >
This can improve clarity of (for example) state machines, where the syntax {#syntax#}continue :sw .next_state{#endsyntax#} is unambiguous, explicit, and immediately understandable.
< / p >
< p >
However, the motivating example is a switch on each element of an array, where using a single switch can improve clarity and performance:
< / p >
{#code|test_switch_dispatch_loop.zig#}
< p >
If the operand to {#syntax#}continue{#endsyntax#} is
{#link|comptime#}-known, then it can be lowered to an unconditional branch
to the relevant case. Such a branch is perfectly predicted, and hence
typically very fast to execute.
< / p >
< p >
If the operand is runtime-known, each {#syntax#}continue{#endsyntax#} can
embed a conditional branch inline (ideally through a jump table), which
allows a CPU to predict its target independently of any other prong. A
loop-based lowering would force every branch through the same dispatch
point, hindering branch prediction.
< / p >
2019-07-04 03:09:58 +00:00
{#header_close#}
2022-09-27 14:38:01 +00:00
2024-01-21 23:11:20 +00:00
{#header_open|Inline Switch Prongs#}
2022-09-27 14:38:01 +00:00
< p >
Switch prongs can be marked as {#syntax#}inline{#endsyntax#} to generate
2024-01-21 23:11:20 +00:00
the prong's body for each possible value it could have, making the
captured value {#link|comptime#}.
2022-09-27 14:38:01 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_inline_switch.zig#}
2022-09-27 14:38:01 +00:00
2024-01-21 23:11:20 +00:00
< p > The {#syntax#}inline{#endsyntax#} keyword may also be combined with ranges:< / p >
2024-04-25 00:41:47 +00:00
{#code|inline_prong_range.zig#}
2022-09-27 14:38:01 +00:00
< p >
{#syntax#}inline else{#endsyntax#} prongs can be used as a type safe
alternative to {#syntax#}inline for{#endsyntax#} loops:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_inline_else.zig#}
2022-09-27 14:38:01 +00:00
< p >
When using an inline prong switching on an union an additional
capture can be used to obtain the union's enum tag value.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_inline_switch_union_tag.zig#}
2022-09-27 14:38:01 +00:00
{#see_also|inline while|inline for#}
{#header_close#}
2019-07-04 03:09:58 +00:00
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|while#}
2018-07-03 18:03:27 +00:00
< p >
A while loop is used to repeatedly execute an expression until
some condition is no longer true.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_while.zig#}
2017-11-07 08:22:27 +00:00
2018-07-03 18:03:27 +00:00
< p >
2018-09-14 14:35:03 +00:00
Use {#syntax#}break{#endsyntax#} to exit a while loop early.
2018-07-03 18:03:27 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_while_break.zig#}
2017-11-07 08:22:27 +00:00
2018-07-03 18:03:27 +00:00
< p >
2018-09-14 14:35:03 +00:00
Use {#syntax#}continue{#endsyntax#} to jump back to the beginning of the loop.
2018-07-03 18:03:27 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_while_continue.zig#}
2017-11-07 08:22:27 +00:00
2018-07-03 18:03:27 +00:00
< p >
While loops support a continue expression which is executed when the loop
2018-09-14 14:35:03 +00:00
is continued. The {#syntax#}continue{#endsyntax#} keyword respects this expression.
2018-07-03 18:03:27 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_while_continue_expression.zig#}
2017-11-07 08:22:27 +00:00
2018-07-03 18:03:27 +00:00
< p >
While loops are expressions. The result of the expression is the
2018-09-14 14:35:03 +00:00
result of the {#syntax#}else{#endsyntax#} clause of a while loop, which is executed when
2018-07-03 18:03:27 +00:00
the condition of the while loop is tested as false.
< / p >
< p >
2018-09-14 14:35:03 +00:00
{#syntax#}break{#endsyntax#}, like {#syntax#}return{#endsyntax#}, accepts a value
parameter. This is the result of the {#syntax#}while{#endsyntax#} expression.
When you {#syntax#}break{#endsyntax#} from a while loop, the {#syntax#}else{#endsyntax#} branch is not
2018-07-03 18:03:27 +00:00
evaluated.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_while_else.zig#}
2017-11-07 08:22:27 +00:00
2018-08-28 00:59:28 +00:00
{#header_open|Labeled while#}
2018-09-14 14:35:03 +00:00
< p > When a {#syntax#}while{#endsyntax#} loop is labeled, it can be referenced from a {#syntax#}break{#endsyntax#}
or {#syntax#}continue{#endsyntax#} from within a nested loop:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_while_nested_break.zig#}
2018-08-28 00:59:28 +00:00
{#header_close#}
2018-07-03 18:03:27 +00:00
{#header_open|while with Optionals#}
< p >
Just like {#link|if#} expressions, while loops can take an optional as the
condition and capture the payload. When {#link|null#} is encountered the loop
exits.
< / p >
< p >
2018-09-14 14:35:03 +00:00
When the {#syntax#}|x|{#endsyntax#} syntax is present on a {#syntax#}while{#endsyntax#} expression,
2018-07-03 18:03:27 +00:00
the while condition must have an {#link|Optional Type#}.
< / p >
< p >
2018-09-14 14:35:03 +00:00
The {#syntax#}else{#endsyntax#} branch is allowed on optional iteration. In this case, it will
2018-07-03 18:03:27 +00:00
be executed on the first null value encountered.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_while_null_capture.zig#}
2018-02-08 08:02:41 +00:00
2018-07-03 18:03:27 +00:00
{#header_close#}
{#header_open|while with Error Unions#}
< p >
Just like {#link|if#} expressions, while loops can take an error union as
the condition and capture the payload or the error code. When the
condition results in an error code the else branch is evaluated and
the loop is finished.
< / p >
< p >
2018-09-14 14:35:03 +00:00
When the {#syntax#}else |x|{#endsyntax#} syntax is present on a {#syntax#}while{#endsyntax#} expression,
2018-07-03 18:03:27 +00:00
the while condition must have an {#link|Error Union Type#}.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_while_error_capture.zig#}
2018-07-03 18:03:27 +00:00
{#header_close#}
2018-06-19 21:21:08 +00:00
{#header_open|inline while#}
< p >
While loops can be inlined. This causes the loop to be unrolled, which
allows the code to do some things which only work at compile time,
such as use types as first class values.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_inline_while.zig#}
2017-11-07 08:22:27 +00:00
2018-06-19 21:21:08 +00:00
< p >
2018-09-14 14:35:03 +00:00
It is recommended to use {#syntax#}inline{#endsyntax#} loops only for one of these reasons:
2018-06-19 21:21:08 +00:00
< / p >
< ul >
< li > You need the loop to execute at {#link|comptime#} for the semantics to work.< / li >
< li >
You have a benchmark to prove that forcibly unrolling the loop in this way is measurably faster.
< / li >
< / ul >
{#header_close#}
2018-06-10 03:42:14 +00:00
{#see_also|if|Optionals|Errors|comptime|unreachable#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|for#}
2024-04-25 00:41:47 +00:00
{#code|test_for.zig#}
2017-11-07 08:22:27 +00:00
2018-08-28 00:59:28 +00:00
{#header_open|Labeled for#}
2018-09-14 14:35:03 +00:00
< p > When a {#syntax#}for{#endsyntax#} loop is labeled, it can be referenced from a {#syntax#}break{#endsyntax#}
or {#syntax#}continue{#endsyntax#} from within a nested loop:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_for_nested_break.zig#}
2018-08-28 00:59:28 +00:00
{#header_close#}
2018-06-19 21:21:08 +00:00
{#header_open|inline for#}
< p >
For loops can be inlined. This causes the loop to be unrolled, which
allows the code to do some things which only work at compile time,
such as use types as first class values.
The capture value and iterator value of inlined for loops are
compile-time known.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_inline_for.zig#}
2017-11-07 08:22:27 +00:00
2018-06-19 21:21:08 +00:00
< p >
2018-09-14 14:35:03 +00:00
It is recommended to use {#syntax#}inline{#endsyntax#} loops only for one of these reasons:
2018-06-19 21:21:08 +00:00
< / p >
< ul >
< li > You need the loop to execute at {#link|comptime#} for the semantics to work.< / li >
< li >
You have a benchmark to prove that forcibly unrolling the loop in this way is measurably faster.
< / li >
< / ul >
{#header_close#}
2018-01-17 05:22:33 +00:00
{#see_also|while|comptime|Arrays|Slices#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|if#}
2024-04-25 00:41:47 +00:00
{#code|test_if.zig#}
2017-11-07 08:22:27 +00:00
2023-12-07 21:51:03 +00:00
{#header_open|if with Optionals#}
2017-11-07 08:22:27 +00:00
2024-04-25 00:41:47 +00:00
{#code|test_if_optionals.zig#}
2020-07-08 20:40:16 +00:00
2023-12-07 21:51:03 +00:00
{#header_close#}
2018-06-10 03:42:14 +00:00
{#see_also|Optionals|Errors#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-01-17 05:22:33 +00:00
{#header_open|defer#}
2024-01-19 04:55:44 +00:00
< p > Executes an expression unconditionally at scope exit.< / p >
2024-04-25 00:41:47 +00:00
{#code|test_defer.zig#}
2017-11-07 08:22:27 +00:00
2024-01-19 04:55:44 +00:00
< p > Defer expressions are evaluated in reverse order.< / p >
2024-04-25 00:41:47 +00:00
{#code|defer_unwind.zig#}
2017-11-07 08:22:27 +00:00
2024-01-19 04:55:44 +00:00
< p > Inside a defer expression the return statement is not allowed.< / p >
2024-04-25 00:41:47 +00:00
{#code|test_invalid_defer.zig#}
2022-12-11 16:40:36 +00:00
2018-01-17 05:22:33 +00:00
{#see_also|Errors#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|unreachable#}
2017-11-07 08:22:27 +00:00
< p >
2022-09-28 10:30:55 +00:00
In {#link|Debug#} and {#link|ReleaseSafe#} mode
2018-09-14 14:35:03 +00:00
{#syntax#}unreachable{#endsyntax#} emits a call to {#syntax#}panic{#endsyntax#} with the message < code > reached unreachable code< / code > .
2017-11-07 08:22:27 +00:00
< / p >
< p >
2022-09-28 10:30:55 +00:00
In {#link|ReleaseFast#} and {#link|ReleaseSmall#} mode, the optimizer uses the assumption that {#syntax#}unreachable{#endsyntax#} code
will never be hit to perform optimizations.
2017-11-07 08:22:27 +00:00
< / p >
2018-01-17 04:19:05 +00:00
{#header_open|Basics#}
2024-04-25 00:41:47 +00:00
{#code|test_unreachable.zig#}
2020-07-11 02:27:26 +00:00
< p > In fact, this is how {#syntax#}std.debug.assert{#endsyntax#} is implemented:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_assertion_failure.zig#}
2017-11-07 08:22:27 +00:00
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|At Compile-Time#}
2024-04-25 00:41:47 +00:00
{#code|test_comptime_unreachable.zig#}
2017-11-07 08:22:27 +00:00
2018-01-17 05:22:33 +00:00
{#see_also|Zig Test|Build Mode|comptime#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_close#}
{#header_open|noreturn#}
2017-11-07 08:22:27 +00:00
< p >
2018-09-14 14:35:03 +00:00
{#syntax#}noreturn{#endsyntax#} is the type of:
2017-11-07 08:22:27 +00:00
< / p >
< ul >
2018-09-14 14:35:03 +00:00
< li > {#syntax#}break{#endsyntax#}< / li >
< li > {#syntax#}continue{#endsyntax#}< / li >
< li > {#syntax#}return{#endsyntax#}< / li >
< li > {#syntax#}unreachable{#endsyntax#}< / li >
< li > {#syntax#}while (true) {}{#endsyntax#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
2018-09-14 14:35:03 +00:00
< p > When resolving types together, such as {#syntax#}if{#endsyntax#} clauses or {#syntax#}switch{#endsyntax#} prongs,
the {#syntax#}noreturn{#endsyntax#} type is compatible with every other type. Consider:
2017-11-07 08:22:27 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_noreturn.zig#}
2017-11-07 08:22:27 +00:00
2024-04-25 00:41:47 +00:00
< p > Another use case for {#syntax#}noreturn{#endsyntax#} is the {#syntax#}exit{#endsyntax#} function:< / p >
{#code|test_noreturn_from_exit.zig#}
2017-11-07 08:22:27 +00:00
2018-01-17 04:19:05 +00:00
{#header_close#}
2021-12-03 04:51:14 +00:00
2018-01-17 04:19:05 +00:00
{#header_open|Functions#}
2024-04-25 00:41:47 +00:00
{#code|test_functions.zig#}
2017-11-07 08:22:27 +00:00
2022-08-08 06:10:57 +00:00
< p > There is a difference between a function < em > body< / em > and a function < em > pointer< / em > .
Function bodies are {#link|comptime#}-only types while function {#link|Pointers#} may be
runtime-known.< / p >
2018-01-17 04:19:05 +00:00
{#header_open|Pass-by-value Parameters#}
2017-11-07 08:22:27 +00:00
< p >
2019-02-05 15:28:05 +00:00
Primitive types such as {#link|Integers#} and {#link|Floats#} passed as parameters
are copied, and then the copy is available in the function body. This is called "passing by value".
Copying a primitive type is essentially free and typically involves nothing more than
setting a register.
< / p >
< p >
Structs, unions, and arrays can sometimes be more efficiently passed as a reference, since a copy
could be arbitrarily expensive depending on the size. When these types are passed
as parameters, Zig may choose to copy and pass by value, or pass by reference, whichever way
Zig decides will be faster. This is made possible, in part, by the fact that parameters are immutable.
2017-11-07 08:22:27 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_pass_by_reference_or_value.zig#}
2017-11-07 08:22:27 +00:00
< p >
2018-06-14 02:40:38 +00:00
For extern functions, Zig follows the C ABI for passing structs and unions by value.
2017-11-07 08:22:27 +00:00
< / p >
2019-10-11 08:41:21 +00:00
{#header_close#}
{#header_open|Function Parameter Type Inference#}
2019-12-09 20:59:42 +00:00
< p >
2020-07-11 11:08:20 +00:00
Function parameters can be declared with {#syntax#}anytype{#endsyntax#} in place of the type.
2019-10-11 08:41:21 +00:00
In this case the parameter types will be inferred when the function is called.
2019-12-09 20:59:42 +00:00
Use {#link|@TypeOf#} and {#link|@typeInfo#} to get information about the inferred type.
2019-10-11 08:41:21 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_fn_type_inference.zig#}
2019-10-11 08:41:21 +00:00
2019-12-09 20:59:42 +00:00
2018-01-17 04:19:05 +00:00
{#header_close#}
2024-01-11 02:31:28 +00:00
{#header_open|inline fn#}
< p >
Adding the {#syntax#}inline{#endsyntax#} keyword to a function definition makes that
function become < em > semantically inlined< / em > at the callsite. This is
not a hint to be possibly observed by optimization passes, but has
implications on the types and values involved in the function call.
< / p >
< p >
Unlike normal function calls, arguments at an inline function callsite which are
compile-time known are treated as {#link|Compile Time Parameters#}. This can potentially
propagate all the way to the return value:
< / p >
2024-04-25 00:41:47 +00:00
{#code|inline_call.zig#}
2024-01-11 02:31:28 +00:00
< p > If {#syntax#}inline{#endsyntax#} is removed, the test fails with the compile error
instead of passing.< / p >
< p > It is generally better to let the compiler decide when to inline a
function, except for these scenarios:< / p >
< ul >
< li > To change how many stack frames are in the call stack, for debugging purposes.< / li >
2024-01-11 09:02:21 +00:00
< li > To force comptime-ness of the arguments to propagate to the return value of the function, as in the above example.< / li >
2024-01-11 02:31:28 +00:00
< li > Real world performance measurements demand it.< / li >
< / ul >
< p > Note that {#syntax#}inline{#endsyntax#} actually < em > restricts< / em >
what the compiler is allowed to do. This can harm binary size,
compilation speed, and even runtime performance.< / p >
{#header_close#}
2018-01-31 16:47:56 +00:00
{#header_open|Function Reflection#}
2024-04-25 00:41:47 +00:00
{#code|test_fn_reflection.zig#}
2022-12-12 08:03:51 +00:00
2018-01-31 16:47:56 +00:00
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Errors#}
2018-02-24 01:43:47 +00:00
{#header_open|Error Set Type#}
2017-11-07 08:22:27 +00:00
< p >
2018-02-24 01:43:47 +00:00
An error set is like an {#link|enum#}.
However, each error name across the entire compilation gets assigned an unsigned integer
greater than 0. You are allowed to declare the same error name more than once, and if you do, it
gets assigned the same integer value.
2017-11-07 08:22:27 +00:00
< / p >
< p >
2023-10-19 21:32:44 +00:00
The error set type defaults to a {#syntax#}u16{#endsyntax#}, though if the maximum number of distinct
error values is provided via the < kbd > --error-limit [num]< / kbd > command line parameter an integer type
with the minimum number of bits required to represent all of the error values will be used.
2017-11-07 08:22:27 +00:00
< / p >
< p >
2019-11-08 20:56:21 +00:00
You can {#link|coerce|Type Coercion#} an error from a subset to a superset:
2017-11-07 08:22:27 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_coerce_error_subset_to_superset.zig#}
2018-02-24 01:43:47 +00:00
2017-11-07 08:22:27 +00:00
< p >
2019-11-08 20:56:21 +00:00
But you cannot {#link|coerce|Type Coercion#} an error from a superset to a subset:
2018-02-24 01:43:47 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_coerce_error_superset_to_subset.zig#}
2018-02-24 01:43:47 +00:00
< p >
There is a shortcut for declaring an error set with only 1 value, and then getting that value:
< / p >
2024-04-25 00:41:47 +00:00
{#code|single_value_error_set_shortcut.zig#}
2018-02-24 01:43:47 +00:00
< p > This is equivalent to:< / p >
2024-04-25 00:41:47 +00:00
{#code|single_value_error_set.zig#}
2018-02-24 01:43:47 +00:00
< p >
This becomes useful when using {#link|Inferred Error Sets#}.
< / p >
{#header_open|The Global Error Set#}
2018-11-19 18:20:39 +00:00
< p > {#syntax#}anyerror{#endsyntax#} refers to the global error set.
2018-02-24 01:43:47 +00:00
This is the error set that contains all errors in the entire compilation unit.
It is a superset of all other error sets and a subset of none of them.
2017-11-07 08:22:27 +00:00
< / p >
< p >
2019-11-08 20:56:21 +00:00
You can {#link|coerce|Type Coercion#} any error set to the global one, and you can explicitly
2018-11-19 18:20:39 +00:00
cast an error of the global error set to a non-global one. This inserts a language-level
2018-02-24 01:43:47 +00:00
assert to make sure the error value is in fact in the destination error set.
2017-11-07 08:22:27 +00:00
< / p >
< p >
2018-03-25 22:48:07 +00:00
The global error set should generally be avoided because it prevents the
compiler from knowing what errors are possible at compile-time. Knowing
the error set at compile-time is better for generated documentation and
helpful error messages, such as forgetting a possible error value in a {#link|switch#}.
2017-11-07 08:22:27 +00:00
< / p >
2018-02-24 01:43:47 +00:00
{#header_close#}
{#header_close#}
{#header_open|Error Union Type#}
2017-11-07 08:22:27 +00:00
< p >
2018-09-14 14:35:03 +00:00
An error set type and normal type can be combined with the {#syntax#}!{#endsyntax#}
2018-03-25 22:48:07 +00:00
binary operator to form an error union type. You are likely to use an
error union type more often than an error set type by itself.
2017-11-07 08:22:27 +00:00
< / p >
< p >
Here is a function to parse a string into a 64-bit integer:
< / p >
2024-04-25 00:41:47 +00:00
{#code|error_union_parsing_u64.zig#}
2017-11-07 08:22:27 +00:00
< p >
2018-09-14 14:35:03 +00:00
Notice the return type is {#syntax#}!u64{#endsyntax#}. This means that the function
2018-02-08 08:02:41 +00:00
either returns an unsigned 64 bit integer, or an error. We left off the error set
2018-09-14 14:35:03 +00:00
to the left of the {#syntax#}!{#endsyntax#}, so the error set is inferred.
2017-11-07 08:22:27 +00:00
< / p >
< p >
Within the function definition, you can see some return statements that return
2018-09-14 14:35:03 +00:00
an error, and at the bottom a return statement that returns a {#syntax#}u64{#endsyntax#}.
2019-11-08 20:56:21 +00:00
Both types {#link|coerce|Type Coercion#} to {#syntax#}anyerror!u64{#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / p >
< p >
What it looks like to use this function varies depending on what you're
trying to do. One of the following:
< / p >
< ul >
< li > You want to provide a default value if it returned an error.< / li >
< li > If it returned an error then you want to return the same error.< / li >
< li > You know with complete certainty it will not return an error, so want to unconditionally unwrap it.< / li >
< li > You want to take a different action for each possible error.< / li >
< / ul >
2018-06-14 22:12:05 +00:00
{#header_open|catch#}
2018-09-14 14:35:03 +00:00
< p > If you want to provide a default value, you can use the {#syntax#}catch{#endsyntax#} binary operator:< / p >
2024-04-25 00:41:47 +00:00
{#code|catch.zig#}
2021-09-20 08:32:34 +00:00
2017-11-07 08:22:27 +00:00
< p >
2018-09-14 14:35:03 +00:00
In this code, {#syntax#}number{#endsyntax#} will be equal to the successfully parsed string, or
a default value of 13. The type of the right hand side of the binary {#syntax#}catch{#endsyntax#} operator must
match the unwrapped error union type, or be of type {#syntax#}noreturn{#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / p >
2023-03-23 09:06:46 +00:00
< p >
If you want to provide a default value with
{#syntax#}catch{#endsyntax#} after performing some logic, you
can combine {#syntax#}catch{#endsyntax#} with named {#link|Blocks#}:
< / p >
2024-04-25 00:41:47 +00:00
{#code|handle_error_with_catch_block.zig.zig#}
2023-03-23 09:06:46 +00:00
2018-06-14 22:12:05 +00:00
{#header_close#}
{#header_open|try#}
2017-11-07 08:22:27 +00:00
< p > Let's say you wanted to return the error if you got one, otherwise continue with the
function logic:< / p >
2024-04-25 00:41:47 +00:00
{#code|catch_err_return.zig#}
2021-09-20 08:32:34 +00:00
2017-11-07 08:22:27 +00:00
< p >
2018-09-14 14:35:03 +00:00
There is a shortcut for this. The {#syntax#}try{#endsyntax#} expression:
2017-11-07 08:22:27 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|try.zig#}
2021-09-20 08:32:34 +00:00
2017-11-07 08:22:27 +00:00
< p >
2018-09-14 14:35:03 +00:00
{#syntax#}try{#endsyntax#} evaluates an error union expression. If it is an error, it returns
2017-11-07 08:22:27 +00:00
from the current function with the same error. Otherwise, the expression results in
the unwrapped value.
< / p >
2018-06-14 22:12:05 +00:00
{#header_close#}
2017-11-07 08:22:27 +00:00
< p >
Maybe you know with complete certainty that an expression will never be an error.
In this case you can do this:
< / p >
2021-09-20 08:32:34 +00:00
{#syntax#}const number = parseU64("1234", 10) catch unreachable;{#endsyntax#}
2017-11-07 08:22:27 +00:00
< p >
Here we know for sure that "1234" will parse successfully. So we put the
2018-09-14 14:35:03 +00:00
{#syntax#}unreachable{#endsyntax#} value on the right hand side. {#syntax#}unreachable{#endsyntax#} generates
2022-12-13 20:02:17 +00:00
a panic in {#link|Debug#} and {#link|ReleaseSafe#} modes and undefined behavior in
{#link|ReleaseFast#} and {#link|ReleaseSmall#} modes. So, while we're debugging the
2017-11-07 08:22:27 +00:00
application, if there < em > was< / em > a surprise error here, the application would crash
appropriately.
< / p >
< p >
2023-06-21 21:39:14 +00:00
You may want to take a different action for every situation. For that, we combine
2018-06-14 22:12:05 +00:00
the {#link|if#} and {#link|switch#} expression:
2017-11-07 08:22:27 +00:00
< / p >
2021-09-20 08:32:34 +00:00
{#syntax_block|zig|handle_all_error_scenarios.zig#}
2018-01-25 09:10:11 +00:00
fn doAThing(str: []u8) void {
2017-11-07 08:22:27 +00:00
if (parseU64(str, 10)) |number| {
doSomethingWithNumber(number);
} else |err| switch (err) {
2018-01-19 08:03:20 +00:00
error.Overflow => {
2017-11-07 08:22:27 +00:00
// handle overflow...
},
// we promise that InvalidChar won't happen (or crash in debug mode if it does)
2018-01-19 08:03:20 +00:00
error.InvalidChar => unreachable,
2017-11-07 08:22:27 +00:00
}
2023-06-21 21:39:14 +00:00
}
{#end_syntax_block#}
< p >
Finally, you may want to handle only some errors. For that, you can capture the unhandled
errors in the {#syntax#}else{#endsyntax#} case, which now contains a narrower error set:
< / p >
{#syntax_block|zig|handle_some_error_scenarios.zig#}
2024-04-05 13:01:17 +00:00
fn doAnotherThing(str: []u8) error{InvalidChar}!void {
2023-06-21 21:39:14 +00:00
if (parseU64(str, 10)) |number| {
doSomethingWithNumber(number);
} else |err| switch (err) {
error.Overflow => {
// handle overflow...
},
else => |leftover_err| return leftover_err,
}
2023-03-23 09:06:46 +00:00
}
{#end_syntax_block#}
< p >
You must use the variable capture syntax. If you don't need the
variable, you can capture with {#syntax#}_{#endsyntax#} and avoid the
{#syntax#}switch{#endsyntax#}.
< / p >
{#syntax_block|zig|handle_no_error_scenarios.zig#}
fn doADifferentThing(str: []u8) void {
if (parseU64(str, 10)) |number| {
doSomethingWithNumber(number);
} else |_| {
// do as you'd like
}
2018-01-19 08:03:20 +00:00
}
2021-09-20 08:32:34 +00:00
{#end_syntax_block#}
2018-06-14 22:12:05 +00:00
{#header_open|errdefer#}
2017-11-07 08:22:27 +00:00
< p >
The other component to error handling is defer statements.
2018-09-14 14:35:03 +00:00
In addition to an unconditional {#link|defer#}, Zig has {#syntax#}errdefer{#endsyntax#},
2017-11-07 08:22:27 +00:00
which evaluates the deferred expression on block exit path if and only if
the function returned with an error from the block.
< / p >
< p >
Example:
< / p >
2021-09-20 08:32:34 +00:00
{#syntax_block|zig|errdefer_example.zig#}
2018-02-08 08:02:41 +00:00
fn createFoo(param: i32) !Foo {
2018-01-07 21:51:46 +00:00
const foo = try tryToAllocateFoo();
2017-11-07 08:22:27 +00:00
// now we have allocated foo. we need to free it if the function fails.
// but we want to return it if the function succeeds.
2018-01-24 04:08:09 +00:00
errdefer deallocateFoo(foo);
2017-11-07 08:22:27 +00:00
2018-06-10 05:13:51 +00:00
const tmp_buf = allocateTmpBuffer() orelse return error.OutOfMemory;
2017-11-07 08:22:27 +00:00
// tmp_buf is truly a temporary resource, and we for sure want to clean it up
// before this block leaves scope
defer deallocateTmpBuffer(tmp_buf);
2018-01-19 08:03:20 +00:00
if (param > 1337) return error.InvalidParam;
2017-11-07 08:22:27 +00:00
2018-01-24 04:08:09 +00:00
// here the errdefer will not run since we're returning success from the function.
2017-11-07 08:22:27 +00:00
// but the defer will run!
return foo;
2018-01-19 08:03:20 +00:00
}
2021-09-20 08:32:34 +00:00
{#end_syntax_block#}
2017-11-07 08:22:27 +00:00
< p >
The neat thing about this is that you get robust error handling without
the verbosity and cognitive overhead of trying to make sure every exit path
is covered. The deallocation code is always directly following the allocation code.
< / p >
2024-06-28 12:32:56 +00:00
< p >
The {#syntax#}errdefer{#endsyntax#} statement can optionally capture the error:
< / p >
{#code|test_errdefer_capture.zig#}
2018-06-14 22:12:05 +00:00
{#header_close#}
2022-02-23 12:33:51 +00:00
{#header_open|Common errdefer Slip-Ups#}
< p >
It should be noted that {#syntax#}errdefer{#endsyntax#} statements only last until the end of the block
they are written in, and therefore are not run if an error is returned outside of that block:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_errdefer_slip_ups.zig#}
2022-02-23 12:33:51 +00:00
< p >
To ensure that {#syntax#}deallocateFoo{#endsyntax#} is properly called
when returning an error, you must add an {#syntax#}errdefer{#endsyntax#} outside of the block:
2023-01-10 23:02:29 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_errdefer_block.zig#}
2022-02-23 12:33:51 +00:00
< p >
The fact that errdefers only last for the block they are declared in is
especially important when using loops:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_errdefer_loop_leak.zig#}
2022-02-23 12:33:51 +00:00
< p >
Special care must be taken with code that allocates in a loop
to make sure that no memory is leaked when returning an error:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_errdefer_loop.zig#}
2022-02-23 12:33:51 +00:00
{#header_close#}
2017-11-07 08:22:27 +00:00
< p >
A couple of other tidbits about error handling:
< / p >
< ul >
< li > These primitives give enough expressiveness that it's completely practical
to have failing to check for an error be a compile error. If you really want
2018-09-14 14:35:03 +00:00
to ignore the error, you can add {#syntax#}catch unreachable{#endsyntax#} and
2017-11-07 08:22:27 +00:00
get the added benefit of crashing in Debug and ReleaseSafe modes if your assumption was wrong.
< / li >
< li >
Since Zig understands error types, it can pre-weight branches in favor of
2018-11-16 18:33:58 +00:00
errors not occurring. Just a small optimization benefit that is not available
2017-11-07 08:22:27 +00:00
in other languages.
< / li >
< / ul >
2018-01-17 05:22:33 +00:00
{#see_also|defer|if|switch#}
2018-02-24 01:43:47 +00:00
2018-09-14 14:35:03 +00:00
< p > An error union is created with the {#syntax#}!{#endsyntax#} binary operator.
2018-01-31 16:47:56 +00:00
You can use compile-time reflection to access the child type of an error union:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_error_union.zig#}
2018-01-31 16:47:56 +00:00
2018-06-12 23:38:59 +00:00
{#header_open|Merging Error Sets#}
< p >
2018-09-14 14:35:03 +00:00
Use the {#syntax#}||{#endsyntax#} operator to merge two error sets together. The resulting
2018-06-12 23:38:59 +00:00
error set contains the errors of both error sets. Doc comments from the left-hand
side override doc comments from the right-hand side. In this example, the doc
2018-09-14 14:35:03 +00:00
comments for {#syntax#}C.PathNotFound{#endsyntax#} is < code > A doc comment< / code > .
2018-06-12 23:38:59 +00:00
< / p >
< p >
This is especially useful for functions which return different error sets depending
on {#link|comptime#} branches. For example, the Zig standard library uses
2018-09-14 14:35:03 +00:00
{#syntax#}LinuxFileOpenError || WindowsFileOpenError{#endsyntax#} for the error set of opening
2018-06-12 23:38:59 +00:00
files.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_merging_error_sets.zig#}
2018-06-12 23:38:59 +00:00
{#header_close#}
2018-02-24 01:43:47 +00:00
{#header_open|Inferred Error Sets#}
2018-05-25 00:59:19 +00:00
< p >
Because many functions in Zig return a possible error, Zig supports inferring the error set.
2021-09-12 17:08:56 +00:00
To infer the error set for a function, prepend the {#syntax#}!{#endsyntax#} operator to the function’ s return type, like {#syntax#}!T{#endsyntax#}:
2018-05-25 00:59:19 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_inferred_error_sets.zig#}
2018-05-25 00:59:19 +00:00
< p >
When a function has an inferred error set, that function becomes generic and thus it becomes
trickier to do certain things with it, such as obtain a function pointer, or have an error
set that is consistent across different build targets. Additionally, inferred error sets
are incompatible with recursion.
< / p >
< p >
In these situations, it is recommended to use an explicit error set. You can generally start
with an empty error set and let compile errors guide you toward completing the set.
< / p >
< p >
These limitations may be overcome in a future version of Zig.
< / p >
2018-01-31 16:47:56 +00:00
{#header_close#}
2018-02-24 01:43:47 +00:00
{#header_close#}
{#header_open|Error Return Traces#}
2018-06-14 22:12:05 +00:00
< p >
Error Return Traces show all the points in the code that an error was returned to the calling function. This makes it practical to use {#link|try#} everywhere and then still be able to know what happened if an error ends up bubbling all the way out of your application.
< / p >
2024-04-25 00:41:47 +00:00
{#code|error_return_trace.zig#}
2018-06-14 22:12:05 +00:00
< p >
Look closely at this example. This is no stack trace.
< / p >
< p >
2018-09-14 14:35:03 +00:00
You can see that the final error bubbled up was {#syntax#}PermissionDenied{#endsyntax#},
but the original error that started this whole thing was {#syntax#}FileNotFound{#endsyntax#}. In the {#syntax#}bar{#endsyntax#} function, the code handles the original error code,
2018-06-14 22:12:05 +00:00
and then returns another one, from the switch statement. Error Return Traces make this clear, whereas a stack trace would look like this:
< / p >
2024-04-25 00:41:47 +00:00
{#code|stack_trace.zig#}
2018-06-14 22:12:05 +00:00
< p >
Here, the stack trace does not explain how the control
2018-09-14 14:35:03 +00:00
flow in {#syntax#}bar{#endsyntax#} got to the {#syntax#}hello(){#endsyntax#} call.
2018-06-14 22:12:05 +00:00
One would have to open a debugger or further instrument the application
2019-02-22 15:56:49 +00:00
in order to find out. The error return trace, on the other hand,
2018-06-14 22:12:05 +00:00
shows exactly how the error bubbled up.
< / p >
< p >
This debugging feature makes it easier to iterate quickly on code that
robustly handles all error conditions. This means that Zig developers
will naturally find themselves writing correct, robust code in order
to increase their development pace.
< / p >
< p >
Error Return Traces are enabled by default in {#link|Debug#} and {#link|ReleaseSafe#} builds and disabled by default in {#link|ReleaseFast#} and {#link|ReleaseSmall#} builds.
< / p >
< p >
There are a few ways to activate this error return tracing feature:
< / p >
< ul >
< li > Return an error from main< / li >
2018-09-14 14:35:03 +00:00
< li > An error makes its way to {#syntax#}catch unreachable{#endsyntax#} and you have not overridden the default panic handler< / li >
< li > Use {#link|errorReturnTrace#} to access the current return trace. You can use {#syntax#}std.debug.dumpStackTrace{#endsyntax#} to print it. This function returns comptime-known {#link|null#} when building without error return tracing support.< / li >
2018-06-14 22:12:05 +00:00
< / ul >
{#header_open|Implementation Details#}
< p >
To analyze performance cost, there are two cases:
< / p >
< ul >
< li > when no errors are returned< / li >
< li > when returning errors< / li >
< / ul >
< p >
2018-11-13 13:08:37 +00:00
For the case when no errors are returned, the cost is a single memory write operation, only in the first non-failable function in the call graph that calls a failable function, i.e. when a function returning {#syntax#}void{#endsyntax#} calls a function returning {#syntax#}error{#endsyntax#}.
2018-06-14 22:12:05 +00:00
This is to initialize this struct in the stack memory:
< / p >
2021-09-20 08:32:34 +00:00
{#syntax_block|zig|stack_trace_struct.zig#}
2018-11-13 13:08:37 +00:00
pub const StackTrace = struct {
2018-06-14 22:12:05 +00:00
index: usize,
instruction_addresses: [N]usize,
};
2021-09-20 08:32:34 +00:00
{#end_syntax_block#}
2018-06-14 22:12:05 +00:00
< p >
Here, N is the maximum function call depth as determined by call graph analysis. Recursion is ignored and counts for 2.
< / p >
< p >
2018-09-14 14:35:03 +00:00
A pointer to {#syntax#}StackTrace{#endsyntax#} is passed as a secret parameter to every function that can return an error, but it's always the first parameter, so it can likely sit in a register and stay there.
2018-06-14 22:12:05 +00:00
< / p >
< p >
That's it for the path when no errors occur. It's practically free in terms of performance.
< / p >
< p >
2018-09-14 14:35:03 +00:00
When generating the code for a function that returns an error, just before the {#syntax#}return{#endsyntax#} statement (only for the {#syntax#}return{#endsyntax#} statements that return errors), Zig generates a call to this function:
2018-06-14 22:12:05 +00:00
< / p >
2021-09-20 08:32:34 +00:00
{#syntax_block|zig|zig_return_error_fn.zig#}
2018-06-14 22:12:05 +00:00
// marked as "no-inline" in LLVM IR
fn __zig_return_error(stack_trace: *StackTrace) void {
stack_trace.instruction_addresses[stack_trace.index] = @returnAddress();
stack_trace.index = (stack_trace.index + 1) % N;
}
2021-09-20 08:32:34 +00:00
{#end_syntax_block#}
2018-06-14 22:12:05 +00:00
< p >
The cost is 2 math operations plus some memory reads and writes. The memory accessed is constrained and should remain cached for the duration of the error return bubbling.
< / p >
< p >
As for code size cost, 1 function call before a return statement is no big deal. Even so,
I have < a href = "https://github.com/ziglang/zig/issues/690" > a plan< / a > to make the call to
2018-11-13 13:08:37 +00:00
{#syntax#}__zig_return_error{#endsyntax#} a tail call, which brings the code size cost down to actually zero. What is a return statement in code without error return tracing can become a jump instruction in code with error return tracing.
2018-06-14 22:12:05 +00:00
< / p >
{#header_close#}
2018-01-31 16:47:56 +00:00
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-06-10 03:42:14 +00:00
{#header_open|Optionals#}
2017-11-07 08:22:27 +00:00
< p >
One area that Zig provides safety without compromising efficiency or
2018-06-10 03:42:14 +00:00
readability is with the optional type.
2017-11-07 08:22:27 +00:00
< / p >
< p >
2018-06-10 03:42:14 +00:00
The question mark symbolizes the optional type. You can convert a type to an optional
2017-11-07 08:22:27 +00:00
type by putting a question mark in front of it, like this:
< / p >
2024-04-25 00:41:47 +00:00
{#code|optional_integer.zig#}
2017-11-07 08:22:27 +00:00
< p >
2018-09-14 14:35:03 +00:00
Now the variable {#syntax#}optional_int{#endsyntax#} could be an {#syntax#}i32{#endsyntax#}, or {#syntax#}null{#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / p >
< p >
Instead of integers, let's talk about pointers. Null references are the source of many runtime
exceptions, and even stand accused of being
< a href = "https://www.lucidchart.com/techblog/2015/08/31/the-worst-mistake-of-computer-science/" > the worst mistake of computer science< / a > .
< / p >
< p > Zig does not have them.< / p >
< p >
2018-06-10 03:42:14 +00:00
Instead, you can use an optional pointer. This secretly compiles down to a normal pointer,
since we know we can use 0 as the null value for the optional type. But the compiler
2017-11-07 08:22:27 +00:00
can check your work and make sure you don't assign null to something that can't be null.
< / p >
< p >
Typically the downside of not having null is that it makes the code more verbose to
write. But, let's compare some equivalent C code and Zig code.
< / p >
< p >
Task: call malloc, if the result is null, return null.
< / p >
< p > C code< / p >
2023-01-19 11:33:00 +00:00
{#syntax_block|c|call_malloc_in_c.c#}
// malloc prototype included for reference
2017-11-07 08:22:27 +00:00
void *malloc(size_t size);
struct Foo *do_a_thing(void) {
char *ptr = malloc(1234);
if (!ptr) return NULL;
// ...
2023-01-19 11:33:00 +00:00
}
{#end_syntax_block#}
2017-11-07 08:22:27 +00:00
< p > Zig code< / p >
2021-09-20 08:32:34 +00:00
{#syntax_block|zig|call_malloc_from_zig.zig#}
2018-01-19 08:03:20 +00:00
// malloc prototype included for reference
2023-11-04 10:17:44 +00:00
extern fn malloc(size: usize) ?[*]u8;
2017-11-07 08:22:27 +00:00
2018-05-31 14:56:59 +00:00
fn doAThing() ?*Foo {
2018-06-10 05:13:51 +00:00
const ptr = malloc(1234) orelse return null;
2021-06-23 18:32:28 +00:00
_ = ptr; // ...
2018-01-19 08:03:20 +00:00
}
2021-09-20 08:32:34 +00:00
{#end_syntax_block#}
2017-11-07 08:22:27 +00:00
< p >
Here, Zig is at least as convenient, if not more, than C. And, the type of "ptr"
2023-11-04 10:17:44 +00:00
is {#syntax#}[*]u8{#endsyntax#} < em > not< / em > {#syntax#}?[*]u8{#endsyntax#}. The {#syntax#}orelse{#endsyntax#} keyword
2018-09-14 14:35:03 +00:00
unwrapped the optional type and therefore {#syntax#}ptr{#endsyntax#} is guaranteed to be non-null everywhere
2017-11-07 08:22:27 +00:00
it is used in the function.
< / p >
< p >
The other form of checking against NULL you might see looks like this:
< / p >
2023-01-19 11:33:00 +00:00
{#syntax_block|c|checking_null_in_c.c#}
void do_a_thing(struct Foo *foo) {
2017-11-07 08:22:27 +00:00
// do some stuff
if (foo) {
do_something_with_foo(foo);
}
// do some stuff
2023-01-19 11:33:00 +00:00
}
{#end_syntax_block#}
2017-11-07 08:22:27 +00:00
< p >
In Zig you can accomplish the same thing:
< / p >
2024-04-25 00:41:47 +00:00
{#code|checking_null_in_zig.zig#}
2017-11-07 08:22:27 +00:00
< p >
Once again, the notable thing here is that inside the if block,
2018-09-14 14:35:03 +00:00
{#syntax#}foo{#endsyntax#} is no longer an optional pointer, it is a pointer, which
2017-11-07 08:22:27 +00:00
cannot be null.
< / p >
< p >
One benefit to this is that functions which take pointers as arguments can
be annotated with the "nonnull" attribute - < code > __attribute__((nonnull))< / code > in
< a href = "https://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Function-Attributes.html" > GCC< / a > .
The optimizer can sometimes make better decisions knowing that pointer arguments
cannot be null.
< / p >
2018-06-10 03:42:14 +00:00
{#header_open|Optional Type#}
2018-09-14 14:35:03 +00:00
< p > An optional is created by putting {#syntax#}?{#endsyntax#} in front of a type. You can use compile-time
2018-06-10 03:42:14 +00:00
reflection to access the child type of an optional:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_optional_type.zig#}
2018-01-31 16:47:56 +00:00
2018-06-14 22:12:05 +00:00
{#header_close#}
{#header_open|null#}
< p >
2018-09-14 14:35:03 +00:00
Just like {#link|undefined#}, {#syntax#}null{#endsyntax#} has its own type, and the only way to use it is to
2018-06-14 22:12:05 +00:00
cast it to a different type:
< / p >
2024-04-25 00:41:47 +00:00
{#code|null.zig#}
2018-10-05 02:51:36 +00:00
{#header_close#}
{#header_open|Optional Pointers#}
< p > An optional pointer is guaranteed to be the same size as a pointer. The {#syntax#}null{#endsyntax#} of
the optional is guaranteed to be address 0.< / p >
2024-04-25 00:41:47 +00:00
{#code|test_optional_pointer.zig#}
2018-10-05 02:51:36 +00:00
2018-01-31 16:47:56 +00:00
{#header_close#}
2023-12-07 18:53:05 +00:00
2023-12-07 21:51:03 +00:00
{#see_also|while with Optionals|if with Optionals#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Casting#}
2018-06-21 18:43:55 +00:00
< p >
A < strong > type cast< / strong > converts a value of one type to another.
2019-11-08 20:56:21 +00:00
Zig has {#link|Type Coercion#} for conversions that are known to be completely safe and unambiguous,
2018-06-21 18:43:55 +00:00
and {#link|Explicit Casts#} for conversions that one would not want to happen on accident.
There is also a third kind of type conversion called {#link|Peer Type Resolution#} for
the case when a result type must be decided given multiple operand types.
< / p >
2019-11-08 20:56:21 +00:00
{#header_open|Type Coercion#}
2018-06-21 18:43:55 +00:00
< p >
2019-11-08 20:56:21 +00:00
Type coercion occurs when one type is expected, but different type is provided:
2018-06-21 18:43:55 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_type_coercion.zig#}
2018-06-21 18:43:55 +00:00
2018-06-26 19:48:42 +00:00
< p >
2019-11-08 20:56:21 +00:00
Type coercions are only allowed when it is completely unambiguous how to get from one type to another,
2019-02-14 23:59:20 +00:00
and the transformation is guaranteed to be safe. There is one exception, which is {#link|C Pointers#}.
2018-06-26 19:48:42 +00:00
< / p >
2019-11-08 20:56:21 +00:00
{#header_open|Type Coercion: Stricter Qualification#}
2018-06-21 18:43:55 +00:00
< p >
Values which have the same representation at runtime can be cast to increase the strictness
of the qualifiers, no matter how nested the qualifiers are:
< / p >
< ul >
2018-09-14 14:35:03 +00:00
< li > {#syntax#}const{#endsyntax#} - non-const to const is allowed< / li >
< li > {#syntax#}volatile{#endsyntax#} - non-volatile to volatile is allowed< / li >
< li > {#syntax#}align{#endsyntax#} - bigger to smaller alignment is allowed < / li >
2018-06-21 18:43:55 +00:00
< li > {#link|error sets|Error Set Type#} to supersets is allowed< / li >
< / ul >
< p >
These casts are no-ops at runtime since the value representation does not change.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_no_op_casts.zig#}
2018-06-21 18:43:55 +00:00
< p >
2019-11-08 20:56:21 +00:00
In addition, pointers coerce to const optional pointers:
2018-06-21 18:43:55 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_pointer_coerce_const_optional.zig#}
2018-06-21 18:43:55 +00:00
{#header_close#}
2019-11-08 20:56:21 +00:00
{#header_open|Type Coercion: Integer and Float Widening#}
2018-06-21 18:43:55 +00:00
< p >
2019-11-08 20:56:21 +00:00
{#link|Integers#} coerce to integer types which can represent every value of the old type, and likewise
{#link|Floats#} coerce to float types which can represent every value of the old type.
2018-06-21 18:43:55 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_integer_widening.zig#}
2018-06-21 18:43:55 +00:00
2020-06-29 00:42:37 +00:00
{#header_close#}
2023-01-25 08:26:41 +00:00
{#header_open|Type Coercion: Float to Int#}
2020-06-29 00:42:37 +00:00
< p >
A compiler error is appropriate because this ambiguous expression leaves the compiler
two choices about the coercion.
2020-08-14 16:33:45 +00:00
< / p >
2020-06-29 00:42:37 +00:00
< ul >
2022-08-08 06:10:57 +00:00
< li > Cast {#syntax#}54.0{#endsyntax#} to {#syntax#}comptime_int{#endsyntax#} resulting in {#syntax#}@as(comptime_int, 10){#endsyntax#}, which is casted to {#syntax#}@as(f32, 10){#endsyntax#}< / li >
< li > Cast {#syntax#}5{#endsyntax#} to {#syntax#}comptime_float{#endsyntax#} resulting in {#syntax#}@as(comptime_float, 10.8){#endsyntax#}, which is casted to {#syntax#}@as(f32, 10.8){#endsyntax#}< / li >
2020-06-29 00:42:37 +00:00
< / ul >
2024-04-25 00:41:47 +00:00
{#code|test_ambiguous_coercion.zig#}
2018-06-21 18:43:55 +00:00
{#header_close#}
2021-07-23 06:32:20 +00:00
{#header_open|Type Coercion: Slices, Arrays and Pointers#}
2024-04-25 00:41:47 +00:00
{#code|test_coerce_slices_arrays_and_pointers.zig#}
2019-03-11 23:34:58 +00:00
{#see_also|C Pointers#}
2018-06-21 18:43:55 +00:00
{#header_close#}
2019-11-08 20:56:21 +00:00
{#header_open|Type Coercion: Optionals#}
2019-03-11 23:34:58 +00:00
< p >
2019-11-08 20:56:21 +00:00
The payload type of {#link|Optionals#}, as well as {#link|null#}, coerce to the optional type.
2019-03-11 23:34:58 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_coerce_optionals.zig#}
2019-03-11 23:34:58 +00:00
2023-09-21 14:50:48 +00:00
< p > Optionals work nested inside the {#link|Error Union Type#}, too:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_coerce_optional_wrapped_error_union.zig#}
2019-03-11 23:34:58 +00:00
2018-06-21 18:43:55 +00:00
{#header_close#}
2019-11-08 20:56:21 +00:00
{#header_open|Type Coercion: Error Unions#}
2019-06-09 03:49:50 +00:00
< p > The payload type of an {#link|Error Union Type#} as well as the {#link|Error Set Type#}
2019-11-08 20:56:21 +00:00
coerce to the error union type:
2019-03-11 23:34:58 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_coerce_to_error_union.zig#}
2019-03-11 23:34:58 +00:00
2018-06-21 18:43:55 +00:00
{#header_close#}
2019-11-08 20:56:21 +00:00
{#header_open|Type Coercion: Compile-Time Known Numbers#}
2019-03-11 23:34:58 +00:00
< p > When a number is {#link|comptime#}-known to be representable in the destination type,
2019-11-08 20:56:21 +00:00
it may be coerced:
2019-03-11 23:34:58 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_coerce_large_to_small.zig#}
2019-03-11 23:34:58 +00:00
2018-06-21 18:43:55 +00:00
{#header_close#}
2023-01-25 08:26:41 +00:00
{#header_open|Type Coercion: Unions and Enums#}
2019-11-08 20:56:21 +00:00
< p > Tagged unions can be coerced to enums, and enums can be coerced to tagged unions
2019-03-11 23:34:58 +00:00
when they are {#link|comptime#}-known to be a field of the union that has only one possible value, such as
{#link|void#}:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_coerce_unions_enums.zig#}
2023-01-24 15:11:38 +00:00
2019-03-11 23:34:58 +00:00
{#see_also|union|enum#}
2018-06-21 18:43:55 +00:00
{#header_close#}
2019-11-08 20:56:21 +00:00
{#header_open|Type Coercion: undefined#}
2022-12-10 03:10:55 +00:00
< p > {#link|undefined#} can be coerced to any type.< / p >
{#header_close#}
2023-01-25 08:26:41 +00:00
{#header_open|Type Coercion: Tuples to Arrays#}
2022-12-10 03:10:55 +00:00
< p > {#link|Tuples#} can be coerced to arrays, if all of the fields have the same type.< / p >
2024-04-25 00:41:47 +00:00
{#code|test_coerce_tuples_arrays.zig#}
2018-06-21 18:43:55 +00:00
{#header_close#}
{#header_close#}
{#header_open|Explicit Casts#}
2018-06-26 19:48:42 +00:00
< p >
Explicit casts are performed via {#link|Builtin Functions#}.
Some explicit casts are safe; some are not.
Some explicit casts perform language-level assertions; some do not.
Some explicit casts are no-ops at runtime; some are not.
< / p >
< ul >
< li > {#link|@bitCast#} - change type but maintain bit representation< / li >
< li > {#link|@alignCast#} - make a pointer have more alignment< / li >
2023-06-28 09:22:32 +00:00
< li > {#link|@enumFromInt#} - obtain an enum value based on its integer tag value< / li >
< li > {#link|@errorFromInt#} - obtain an error code based on its integer value< / li >
2023-10-01 10:16:02 +00:00
< li > {#link|@errorCast#} - convert to a smaller error set< / li >
2023-06-28 09:22:32 +00:00
< li > {#link|@floatCast#} - convert a larger float to a smaller float< / li >
< li > {#link|@floatFromInt#} - convert an integer to a float value< / li >
< li > {#link|@intCast#} - convert between integer types< / li >
2023-06-15 07:14:16 +00:00
< li > {#link|@intFromBool#} - convert true to 1 and false to 0< / li >
< li > {#link|@intFromEnum#} - obtain the integer tag value of an enum or tagged union< / li >
< li > {#link|@intFromError#} - obtain the integer value of an error code< / li >
< li > {#link|@intFromFloat#} - obtain the integer part of a float value< / li >
2023-06-28 09:22:32 +00:00
< li > {#link|@intFromPtr#} - obtain the address of a pointer< / li >
2023-06-15 07:14:16 +00:00
< li > {#link|@ptrFromInt#} - convert an address to a pointer< / li >
2018-06-26 19:48:42 +00:00
< li > {#link|@ptrCast#} - convert between pointer types< / li >
< li > {#link|@truncate#} - convert between integer types, chopping off bits< / li >
< / ul >
2018-06-21 18:43:55 +00:00
{#header_close#}
{#header_open|Peer Type Resolution#}
2018-08-20 18:21:58 +00:00
< p > Peer Type Resolution occurs in these places:< / p >
< ul >
< li > {#link|switch#} expressions< / li >
< li > {#link|if#} expressions< / li >
< li > {#link|while#} expressions< / li >
< li > {#link|for#} expressions< / li >
< li > Multiple break statements in a block< / li >
< li > Some {#link|binary operations|Table of Operators#}< / li >
< / ul >
< p >
2019-11-08 20:56:21 +00:00
This kind of type resolution chooses a type that all peer types can coerce into. Here are
2018-08-20 18:21:58 +00:00
some examples:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_peer_type_resolution.zig#}
2023-12-08 09:16:11 +00:00
2018-06-21 18:43:55 +00:00
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-06-18 16:02:30 +00:00
2019-02-25 23:31:37 +00:00
{#header_open|Zero Bit Types#}
< p > For some types, {#link|@sizeOf#} is 0:< / p >
< ul >
< li > {#link|void#}< / li >
< li > The {#link|Integers#} {#syntax#}u0{#endsyntax#} and {#syntax#}i0{#endsyntax#}.< / li >
< li > {#link|Arrays#} and {#link|Vectors#} with len 0, or with an element type that is a zero bit type.< / li >
< li > An {#link|enum#} with only 1 tag.< / li >
2019-06-18 13:26:07 +00:00
< li > A {#link|struct#} with all fields being zero bit types.< / li >
2019-02-25 23:31:37 +00:00
< li > A {#link|union#} with only 1 field which is a zero bit type.< / li >
< / ul >
2018-06-18 16:02:30 +00:00
< p >
2019-02-25 23:31:37 +00:00
These types can only ever have one possible value, and thus
require 0 bits to represent. Code that makes use of these types is
2018-06-18 16:02:30 +00:00
not included in the final generated code:
< / p >
2024-04-25 00:41:47 +00:00
{#code|zero_bit_types.zig#}
2019-02-25 23:31:37 +00:00
< p > When this turns into machine code, there is no code generated in the
body of {#syntax#}entry{#endsyntax#}, even in {#link|Debug#} mode. For example, on x86_64:< / p >
2018-06-18 16:02:30 +00:00
< pre > < code > 0000000000000010 < entry> :
10: 55 push %rbp
11: 48 89 e5 mov %rsp,%rbp
14: 5d pop %rbp
15: c3 retq < / code > < / pre >
< p > These assembly instructions do not have any code associated with the void values -
2022-09-24 18:42:00 +00:00
they only perform the function call prologue and epilogue.< / p >
2019-02-25 23:31:37 +00:00
{#header_open|void#}
2018-06-18 16:02:30 +00:00
< p >
2018-09-14 14:35:03 +00:00
{#syntax#}void{#endsyntax#} can be useful for instantiating generic types. For example, given a
{#syntax#}Map(Key, Value){#endsyntax#}, one can pass {#syntax#}void{#endsyntax#} for the {#syntax#}Value{#endsyntax#}
type to make it into a {#syntax#}Set{#endsyntax#}:
2018-06-18 16:02:30 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_void_in_hashmap.zig#}
2018-06-18 16:02:30 +00:00
2019-06-09 03:49:50 +00:00
< p > Note that this is different from using a dummy value for the hash map value.
2018-09-14 14:35:03 +00:00
By using {#syntax#}void{#endsyntax#} as the type of the value, the hash map entry type has no value field, and
2018-06-18 16:02:30 +00:00
thus the hash map takes up less space. Further, all the code that deals with storing and loading the
value is deleted, as seen above.
< / p >
< p >
2021-12-21 13:13:32 +00:00
{#syntax#}void{#endsyntax#} is distinct from {#syntax#}anyopaque{#endsyntax#}.
{#syntax#}void{#endsyntax#} has a known size of 0 bytes, and {#syntax#}anyopaque{#endsyntax#} has an unknown, but non-zero, size.
2018-06-18 16:02:30 +00:00
< / p >
< p >
2023-09-21 14:50:48 +00:00
Expressions of type {#syntax#}void{#endsyntax#} are the only ones whose value can be ignored. For example, ignoring
a non-{#syntax#}void{#endsyntax#} expression is a compile error:
2018-06-18 16:02:30 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_expression_ignored.zig#}
2018-06-18 16:02:30 +00:00
2023-09-21 14:50:48 +00:00
< p > However, if the expression has type {#syntax#}void{#endsyntax#}, there will be no error. Expression results can be explicitly ignored by assigning them to {#syntax#}_{#endsyntax#}. < / p >
2024-04-25 00:41:47 +00:00
{#code|test_void_ignored.zig#}
2018-12-10 05:50:12 +00:00
2018-01-17 04:19:05 +00:00
{#header_close#}
2019-02-25 23:31:37 +00:00
{#header_close#}
2019-07-03 17:40:40 +00:00
{#header_open|Result Location Semantics#}
< p >
2023-11-19 17:03:25 +00:00
During compilation, every Zig expression and sub-expression is assigned optional result location
information. This information dictates what type the expression should have (its result type), and
where the resulting value should be placed in memory (its result location). The information is
optional in the sense that not every expression has this information: assignment to
{#syntax#}_{#endsyntax#}, for instance, does not provide any information about the type of an
expression, nor does it provide a concrete memory location to place it in.
2019-07-03 17:40:40 +00:00
< / p >
2023-11-19 17:03:25 +00:00
< p >
As a motivating example, consider the statement {#syntax#}const x: u32 = 42;{#endsyntax#}. The type
annotation here provides a result type of {#syntax#}u32{#endsyntax#} to the initialization expression
2024-07-09 21:25:42 +00:00
{#syntax#}42{#endsyntax#}, instructing the compiler to coerce this integer (initially of type
2023-11-19 17:03:25 +00:00
{#syntax#}comptime_int{#endsyntax#}) to this type. We will see more examples shortly.
< / p >
< p >
This is not an implementation detail: the logic outlined above is codified into the Zig language
specification, and is the primary mechanism of type inference in the language. This system is
collectively referred to as "Result Location Semantics".
< / p >
{#header_open|Result Types#}
< p >
Result types are propagated recursively through expressions where possible. For instance, if the
expression {#syntax#}& e{#endsyntax#} has result type {#syntax#}*u32{#endsyntax#}, then
{#syntax#}e{#endsyntax#} is given a result type of {#syntax#}u32{#endsyntax#}, allowing the
language to perform this coercion before taking a reference.
< / p >
< p >
The result type mechanism is utilized by casting builtins such as {#syntax#}@intCast{#endsyntax#}.
Rather than taking as an argument the type to cast to, these builtins use their result type to
determine this information. The result type is often known from context; where it is not, the
{#syntax#}@as{#endsyntax#} builtin can be used to explicitly provide a result type.
< / p >
< p >
We can break down the result types for each component of a simple expression as follows:
< / p >
2024-04-25 00:41:47 +00:00
{#code|result_type_propagation.zig#}
2023-11-19 17:03:25 +00:00
< p >
This result type information is useful for the aforementioned cast builtins, as well as to avoid
the construction of pre-coercion values, and to avoid the need for explicit type coercions in some
cases. The following table details how some common expressions propagate result types, where
{#syntax#}x{#endsyntax#} and {#syntax#}y{#endsyntax#} are arbitrary sub-expressions.
< / p >
< div class = "table-wrapper" >
< table >
< thead >
< tr >
< th scope = "col" > Expression< / th >
< th scope = "col" > Parent Result Type< / th >
< th scope = "col" > Sub-expression Result Type< / th >
< / tr >
< / thead >
< tbody >
< tr >
< th scope = "row" > {#syntax#}const val: T = x{#endsyntax#}< / th >
< td > -< / td >
< td > {#syntax#}x{#endsyntax#} is a {#syntax#}T{#endsyntax#}< / td >
< / tr >
< tr >
< th scope = "row" > {#syntax#}var val: T = x{#endsyntax#}< / th >
< td > -< / td >
< td > {#syntax#}x{#endsyntax#} is a {#syntax#}T{#endsyntax#}< / td >
< / tr >
< tr >
< th scope = "row" > {#syntax#}val = x{#endsyntax#}< / th >
< td > -< / td >
< td > {#syntax#}x{#endsyntax#} is a {#syntax#}@TypeOf(val){#endsyntax#}< / td >
< / tr >
< tr >
< th scope = "row" > {#syntax#}@as(T, x){#endsyntax#}< / th >
< td > -< / td >
< td > {#syntax#}x{#endsyntax#} is a {#syntax#}T{#endsyntax#}< / td >
< / tr >
< tr >
< th scope = "row" > {#syntax#}& x{#endsyntax#}< / th >
< td > {#syntax#}*T{#endsyntax#}< / td >
< td > {#syntax#}x{#endsyntax#} is a {#syntax#}T{#endsyntax#}< / td >
< / tr >
< tr >
< th scope = "row" > {#syntax#}& x{#endsyntax#}< / th >
< td > {#syntax#}[]T{#endsyntax#}< / td >
< td > {#syntax#}x{#endsyntax#} is some array of {#syntax#}T{#endsyntax#}< / td >
< / tr >
< tr >
< th scope = "row" > {#syntax#}f(x){#endsyntax#}< / th >
< td > -< / td >
< td > {#syntax#}x{#endsyntax#} has the type of the first parameter of {#syntax#}f{#endsyntax#}< / td >
< / tr >
< tr >
< th scope = "row" > {#syntax#}.{x}{#endsyntax#}< / th >
< td > {#syntax#}T{#endsyntax#}< / td >
< td > {#syntax#}x{#endsyntax#} is a {#syntax#}std.meta.FieldType(T, .@"0"){#endsyntax#}< / td >
< / tr >
< tr >
< th scope = "row" > {#syntax#}.{ .a = x }{#endsyntax#}< / th >
< td > {#syntax#}T{#endsyntax#}< / td >
< td > {#syntax#}x{#endsyntax#} is a {#syntax#}std.meta.FieldType(T, .a){#endsyntax#}< / td >
< / tr >
< tr >
< th scope = "row" > {#syntax#}T{x}{#endsyntax#}< / th >
< td > -< / td >
< td > {#syntax#}x{#endsyntax#} is a {#syntax#}std.meta.FieldType(T, .@"0"){#endsyntax#}< / td >
< / tr >
< tr >
< th scope = "row" > {#syntax#}T{ .a = x }{#endsyntax#}< / th >
< td > -< / td >
< td > {#syntax#}x{#endsyntax#} is a {#syntax#}std.meta.FieldType(T, .a){#endsyntax#}< / td >
< / tr >
< tr >
< th scope = "row" > {#syntax#}@Type(x){#endsyntax#}< / th >
< td > -< / td >
< td > {#syntax#}x{#endsyntax#} is a {#syntax#}std.builtin.Type{#endsyntax#}< / td >
< / tr >
< tr >
< th scope = "row" > {#syntax#}@typeInfo(x){#endsyntax#}< / th >
< td > -< / td >
< td > {#syntax#}x{#endsyntax#} is a {#syntax#}type{#endsyntax#}< / td >
< / tr >
< tr >
< th scope = "row" > {#syntax#}x < < y { # endsyntax # } < / th >
< td > -< / td >
< td > {#syntax#}y{#endsyntax#} is a {#syntax#}std.math.Log2IntCeil(@TypeOf(x)){#endsyntax#}< / td >
< / tr >
< / tbody >
< / table >
< / div >
{#header_close#}
{#header_open|Result Locations#}
< p >
In addition to result type information, every expression may be optionally assigned a result
location: a pointer to which the value must be directly written. This system can be used to prevent
intermediate copies when initializing data structures, which can be important for types which must
have a fixed memory address ("pinned" types).
< / p >
< p >
When compiling the simple assignment expression {#syntax#}x = e{#endsyntax#}, many languages would
create the temporary value {#syntax#}e{#endsyntax#} on the stack, and then assign it to
{#syntax#}x{#endsyntax#}, potentially performing a type coercion in the process. Zig approaches this
differently. The expression {#syntax#}e{#endsyntax#} is given a result type matching the type of
{#syntax#}x{#endsyntax#}, and a result location of {#syntax#}& x{#endsyntax#}. For many syntactic
forms of {#syntax#}e{#endsyntax#}, this has no practical impact. However, it can have important
semantic effects when working with more complex syntax forms.
< / p >
< p >
For instance, if the expression {#syntax#}.{ .a = x, .b = y }{#endsyntax#} has a result location of
{#syntax#}ptr{#endsyntax#}, then {#syntax#}x{#endsyntax#} is given a result location of
{#syntax#}& ptr.a{#endsyntax#}, and {#syntax#}y{#endsyntax#} a result location of {#syntax#}& ptr.b{#endsyntax#}.
Without this system, this expression would construct a temporary struct value entirely on the stack, and
only then copy it to the destination address. In essence, Zig desugars the assignment
{#syntax#}foo = .{ .a = x, .b = y }{#endsyntax#} to the two statements {#syntax#}foo.a = x; foo.b = y;{#endsyntax#}.
< / p >
< p >
This can sometimes be important when assigning an aggregate value where the initialization
expression depends on the previous value of the aggregate. The easiest way to demonstrate this is by
attempting to swap fields of a struct or array - the following logic looks sound, but in fact is not:
< / p >
2024-04-25 00:41:47 +00:00
{#code|result_location_interfering_with_swap.zig#}
2023-11-19 17:03:25 +00:00
< p >
The following table details how some common expressions propagate result locations, where
{#syntax#}x{#endsyntax#} and {#syntax#}y{#endsyntax#} are arbitrary sub-expressions. Note that
some expressions cannot provide meaningful result locations to sub-expressions, even if they
themselves have a result location.
< / p >
< div class = "table-wrapper" >
< table >
< thead >
< tr >
< th scope = "col" > Expression< / th >
< th scope = "col" > Result Location< / th >
< th scope = "col" > Sub-expression Result Locations< / th >
< / tr >
< / thead >
< tbody >
< tr >
< th scope = "row" > {#syntax#}const val: T = x{#endsyntax#}< / th >
< td > -< / td >
< td > {#syntax#}x{#endsyntax#} has result location {#syntax#}& val{#endsyntax#}< / td >
< / tr >
< tr >
< th scope = "row" > {#syntax#}var val: T = x{#endsyntax#}< / th >
< td > -< / td >
< td > {#syntax#}x{#endsyntax#} has result location {#syntax#}& val{#endsyntax#}< / td >
< / tr >
< tr >
< th scope = "row" > {#syntax#}val = x{#endsyntax#}< / th >
< td > -< / td >
< td > {#syntax#}x{#endsyntax#} has result location {#syntax#}& val{#endsyntax#}< / td >
< / tr >
< tr >
< th scope = "row" > {#syntax#}@as(T, x){#endsyntax#}< / th >
< td > {#syntax#}ptr{#endsyntax#}< / td >
< td > {#syntax#}x{#endsyntax#} has no result location< / td >
< / tr >
< tr >
< th scope = "row" > {#syntax#}& x{#endsyntax#}< / th >
< td > {#syntax#}ptr{#endsyntax#}< / td >
< td > {#syntax#}x{#endsyntax#} has no result location< / td >
< / tr >
< tr >
< th scope = "row" > {#syntax#}f(x){#endsyntax#}< / th >
< td > {#syntax#}ptr{#endsyntax#}< / td >
< td > {#syntax#}x{#endsyntax#} has no result location< / td >
< / tr >
< tr >
< th scope = "row" > {#syntax#}.{x}{#endsyntax#}< / th >
< td > {#syntax#}ptr{#endsyntax#}< / td >
< td > {#syntax#}x{#endsyntax#} has result location {#syntax#}& ptr[0]{#endsyntax#}< / td >
< / tr >
< tr >
< th scope = "row" > {#syntax#}.{ .a = x }{#endsyntax#}< / th >
< td > {#syntax#}ptr{#endsyntax#}< / td >
< td > {#syntax#}x{#endsyntax#} has result location {#syntax#}& ptr.a{#endsyntax#}< / td >
< / tr >
< tr >
< th scope = "row" > {#syntax#}T{x}{#endsyntax#}< / th >
< td > {#syntax#}ptr{#endsyntax#}< / td >
< td > {#syntax#}x{#endsyntax#} has no result location (typed initializers do not propagate result locations)< / td >
< / tr >
< tr >
< th scope = "row" > {#syntax#}T{ .a = x }{#endsyntax#}< / th >
< td > {#syntax#}ptr{#endsyntax#}< / td >
< td > {#syntax#}x{#endsyntax#} has no result location (typed initializers do not propagate result locations)< / td >
< / tr >
< tr >
< th scope = "row" > {#syntax#}@Type(x){#endsyntax#}< / th >
< td > {#syntax#}ptr{#endsyntax#}< / td >
< td > {#syntax#}x{#endsyntax#} has no result location< / td >
< / tr >
< tr >
< th scope = "row" > {#syntax#}@typeInfo(x){#endsyntax#}< / th >
< td > {#syntax#}ptr{#endsyntax#}< / td >
< td > {#syntax#}x{#endsyntax#} has no result location< / td >
< / tr >
< tr >
< th scope = "row" > {#syntax#}x < < y { # endsyntax # } < / th >
< td > {#syntax#}ptr{#endsyntax#}< / td >
< td > {#syntax#}x{#endsyntax#} and {#syntax#}y{#endsyntax#} do not have result locations< / td >
< / tr >
< / tbody >
< / table >
< / div >
{#header_close#}
2019-07-03 17:40:40 +00:00
{#header_close#}
2019-09-25 01:23:12 +00:00
{#header_open|usingnamespace#}
< p >
2021-09-01 00:01:57 +00:00
{#syntax#}usingnamespace{#endsyntax#} is a declaration that mixes all the public
declarations of the operand, which must be a {#link|struct#}, {#link|union#}, {#link|enum#},
or {#link|opaque#}, into the namespace:
2019-09-25 01:23:12 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_usingnamespace.zig#}
2019-09-25 01:23:12 +00:00
< p >
2021-09-01 00:01:57 +00:00
{#syntax#}usingnamespace{#endsyntax#} has an important use case when organizing the public
2021-08-11 09:01:03 +00:00
API of a file or package. For example, one might have < code class = "file" > c.zig< / code > with all of the
2019-09-25 01:23:12 +00:00
{#link|C imports|Import from C Header File#}:
< / p >
2021-08-11 09:01:03 +00:00
{#syntax_block|zig|c.zig#}
2019-09-25 01:23:12 +00:00
pub usingnamespace @cImport({
@cInclude("epoxy/gl.h");
@cInclude("GLFW/glfw3.h");
@cDefine("STBI_ONLY_PNG", "");
@cDefine("STBI_NO_STDIO", "");
@cInclude("stb_image.h");
});
2021-08-11 09:01:03 +00:00
{#end_syntax_block#}
2019-09-25 01:23:12 +00:00
< p >
The above example demonstrates using {#syntax#}pub{#endsyntax#} to qualify the
{#syntax#}usingnamespace{#endsyntax#} additionally makes the imported declarations
{#syntax#}pub{#endsyntax#}. This can be used to forward declarations, giving precise control
over what declarations a given file exposes.
< / p >
{#header_close#}
2021-09-01 00:01:57 +00:00
2018-01-17 04:19:05 +00:00
{#header_open|comptime#}
2017-11-07 08:22:27 +00:00
< p >
Zig places importance on the concept of whether an expression is known at compile-time.
There are a few different places this concept is used, and these building blocks are used
to keep the language small, readable, and powerful.
< / p >
2018-01-17 04:19:05 +00:00
{#header_open|Introducing the Compile-Time Concept#}
{#header_open|Compile-Time Parameters#}
2017-11-07 08:22:27 +00:00
< p >
Compile-time parameters is how Zig implements generics. It is compile-time duck typing.
< / p >
2024-04-25 00:41:47 +00:00
{#code|compile-time_duck_typing.zig#}
2017-11-07 08:22:27 +00:00
< p >
In Zig, types are first-class citizens. They can be assigned to variables, passed as parameters to functions,
and returned from functions. However, they can only be used in expressions which are known at < em > compile-time< / em > ,
2018-09-14 14:35:03 +00:00
which is why the parameter {#syntax#}T{#endsyntax#} in the above snippet must be marked with {#syntax#}comptime{#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / p >
< p >
2018-09-14 14:35:03 +00:00
A {#syntax#}comptime{#endsyntax#} parameter means that:
2017-11-07 08:22:27 +00:00
< / p >
< ul >
< li > At the callsite, the value must be known at compile-time, or it is a compile error.< / li >
< li > In the function definition, the value is known at compile-time.< / li >
< / ul >
< p >
For example, if we were to introduce another function to the above snippet:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_unresolved_comptime_value.zig#}
2017-11-07 08:22:27 +00:00
< p >
This is an error because the programmer attempted to pass a value only known at run-time
to a function which expects a value known at compile-time.
< / p >
< p >
Another way to get an error is if we pass a type that violates the type checker when the
function is analyzed. This is what it means to have < em > compile-time duck typing< / em > .
< / p >
< p >
For example:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_mismatched_type.zig#}
2017-11-07 08:22:27 +00:00
< p >
2018-09-14 14:35:03 +00:00
On the flip side, inside the function definition with the {#syntax#}comptime{#endsyntax#} parameter, the
2017-11-07 08:22:27 +00:00
value is known at compile-time. This means that we actually could make this work for the bool type
if we wanted to:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_max_with_bool.zig#}
2017-11-07 08:22:27 +00:00
< p >
2018-09-14 14:35:03 +00:00
This works because Zig implicitly inlines {#syntax#}if{#endsyntax#} expressions when the condition
2017-11-07 08:22:27 +00:00
is known at compile-time, and the compiler guarantees that it will skip analysis of
the branch not taken.
< / p >
< p >
2018-09-14 14:35:03 +00:00
This means that the actual function generated for {#syntax#}max{#endsyntax#} in this situation looks like
2017-11-07 08:22:27 +00:00
this:
< / p >
2024-04-25 00:41:47 +00:00
{#code|compiler_generated_function.zig#}
2017-11-07 08:22:27 +00:00
< p >
All the code that dealt with compile-time known values is eliminated and we are left with only
the necessary run-time code to accomplish the task.
< / p >
< p >
2018-09-14 14:35:03 +00:00
This works the same way for {#syntax#}switch{#endsyntax#} expressions - they are implicitly inlined
2017-11-07 08:22:27 +00:00
when the target expression is compile-time known.
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Compile-Time Variables#}
2017-11-07 08:22:27 +00:00
< p >
2018-09-14 14:35:03 +00:00
In Zig, the programmer can label variables as {#syntax#}comptime{#endsyntax#}. This guarantees to the compiler
2017-11-07 08:22:27 +00:00
that every load and store of the variable is performed at compile-time. Any violation of this results in a
compile error.
< / p >
< p >
2018-09-14 14:35:03 +00:00
This combined with the fact that we can {#syntax#}inline{#endsyntax#} loops allows us to write
2017-11-07 08:22:27 +00:00
a function which is partially evaluated at compile-time and partially at run-time.
< / p >
< p >
For example:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_evaluation.zig#}
2017-11-07 08:22:27 +00:00
< p >
This example is a bit contrived, because the compile-time evaluation component is unnecessary;
this code would work fine if it was all done at run-time. But it does end up generating
2018-09-14 14:35:03 +00:00
different code. In this example, the function {#syntax#}performFn{#endsyntax#} is generated three different times,
for the different values of {#syntax#}prefix_char{#endsyntax#} provided:
2017-11-07 08:22:27 +00:00
< / p >
2021-09-20 08:32:34 +00:00
{#syntax_block|zig|performFn_1#}
2018-01-19 08:03:20 +00:00
// From the line:
2020-07-11 02:27:26 +00:00
// expect(performFn('t', 1) == 6);
2018-01-25 09:10:11 +00:00
fn performFn(start_value: i32) i32 {
2017-11-07 08:22:27 +00:00
var result: i32 = start_value;
result = two(result);
result = three(result);
return result;
}
2021-09-20 08:32:34 +00:00
{#end_syntax_block#}
{#syntax_block|zig|performFn_2#}
2017-11-07 08:22:27 +00:00
// From the line:
2020-07-11 02:27:26 +00:00
// expect(performFn('o', 0) == 1);
2018-01-25 09:10:11 +00:00
fn performFn(start_value: i32) i32 {
2017-11-07 08:22:27 +00:00
var result: i32 = start_value;
result = one(result);
return result;
}
2021-09-20 08:32:34 +00:00
{#end_syntax_block#}
{#syntax_block|zig|performFn_3#}
2017-11-07 08:22:27 +00:00
// From the line:
2020-07-11 02:27:26 +00:00
// expect(performFn('w', 99) == 99);
2018-01-25 09:10:11 +00:00
fn performFn(start_value: i32) i32 {
2017-11-07 08:22:27 +00:00
var result: i32 = start_value;
2023-11-17 13:16:07 +00:00
_ = &result;
2017-11-07 08:22:27 +00:00
return result;
2018-01-19 08:03:20 +00:00
}
2021-09-20 08:32:34 +00:00
{#end_syntax_block#}
2018-01-19 08:03:20 +00:00
< p >
2023-09-21 14:50:48 +00:00
Note that this happens even in a debug build.
This is not a way to write more optimized code, but it is a way to make sure that what < em > should< / em > happen
at compile-time, < em > does< / em > happen at compile-time. This catches more errors and allows expressiveness
that in other languages requires using macros, generated code, or a preprocessor to accomplish.
2017-11-07 08:22:27 +00:00
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Compile-Time Expressions#}
2017-11-07 08:22:27 +00:00
< p >
In Zig, it matters whether a given expression is known at compile-time or run-time. A programmer can
2018-09-14 14:35:03 +00:00
use a {#syntax#}comptime{#endsyntax#} expression to guarantee that the expression will be evaluated at compile-time.
2017-11-07 08:22:27 +00:00
If this cannot be accomplished, the compiler will emit an error. For example:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_call_extern_function.zig#}
2017-11-07 08:22:27 +00:00
< p >
2018-09-14 14:35:03 +00:00
It doesn't make sense that a program could call {#syntax#}exit(){#endsyntax#} (or any other external function)
at compile-time, so this is a compile error. However, a {#syntax#}comptime{#endsyntax#} expression does much
2017-11-07 08:22:27 +00:00
more than sometimes cause a compile error.
< / p >
< p >
2018-09-14 14:35:03 +00:00
Within a {#syntax#}comptime{#endsyntax#} expression:
2017-11-07 08:22:27 +00:00
< / p >
< ul >
2018-09-14 14:35:03 +00:00
< li > All variables are {#syntax#}comptime{#endsyntax#} variables.< / li >
< li > All {#syntax#}if{#endsyntax#}, {#syntax#}while{#endsyntax#}, {#syntax#}for{#endsyntax#}, and {#syntax#}switch{#endsyntax#}
2017-11-07 08:22:27 +00:00
expressions are evaluated at compile-time, or emit a compile error if this is not possible.< / li >
2023-04-15 00:19:37 +00:00
< li > All {#syntax#}return{#endsyntax#} and {#syntax#}try{#endsyntax#} expressions are invalid (unless the function itself is called at compile-time).< / li >
< li > All code with runtime side effects or depending on runtime values emits a compile error.< / li >
< li > All function calls cause the compiler to interpret the function at compile-time, emitting a
compile error if the function tries to do something that has global runtime side effects.< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< p >
This means that a programmer can create a function which is called both at compile-time and run-time, with
no modification to the function required.
< / p >
< p >
Let's look at an example:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_fibonacci_recursion.zig#}
2017-11-07 08:22:27 +00:00
< p >
Imagine if we had forgotten the base case of the recursive function and tried to run the tests:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_fibonacci_comptime_overflow.zig#}
2017-11-07 08:22:27 +00:00
< p >
The compiler produces an error which is a stack trace from trying to evaluate the
function at compile-time.
< / p >
< p >
Luckily, we used an unsigned integer, and so when we tried to subtract 1 from 0, it triggered
undefined behavior, which is always a compile error if the compiler knows it happened.
But what would have happened if we used a signed integer?
< / p >
2024-04-25 00:41:47 +00:00
{#code|fibonacci_comptime_infinite_recursion.zig#}
2017-11-07 08:22:27 +00:00
< p >
2022-12-01 03:23:44 +00:00
The compiler is supposed to notice that evaluating this function at
compile-time took more than 1000 branches, and thus emits an error and
gives up. If the programmer wants to increase the budget for compile-time
computation, they can use a built-in function called
{#link|@setEvalBranchQuota#} to change the default number 1000 to
something else.
< / p >
< p >
However, there is a < a href = "https://github.com/ziglang/zig/issues/13724" > design
flaw in the compiler< / a > causing it to stack overflow instead of having the proper
behavior here. I'm terribly sorry about that. I hope to get this resolved
before the next release.
2017-11-07 08:22:27 +00:00
< / p >
< p >
2022-12-01 03:23:44 +00:00
What if we fix the base case, but put the wrong value in the
{#syntax#}expect{#endsyntax#} line?
2017-11-07 08:22:27 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_fibonacci_comptime_unreachable.zig#}
2018-01-19 08:03:20 +00:00
2017-11-07 08:22:27 +00:00
< p >
2022-11-09 08:29:50 +00:00
At {#link|container|Containers#} level (outside of any function), all expressions are implicitly
2018-09-14 14:35:03 +00:00
{#syntax#}comptime{#endsyntax#} expressions. This means that we can use functions to
2017-11-07 08:22:27 +00:00
initialize complex static data. For example:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_container-level_comptime_expressions.zig#}
2017-11-07 08:22:27 +00:00
< p >
When we compile this program, Zig generates the constants
with the answer pre-computed. Here are the lines from the generated LLVM IR:
< / p >
2018-01-19 08:03:20 +00:00
< pre > < code class = "llvm" > @0 = internal unnamed_addr constant [25 x i32] [i32 2, i32 3, i32 5, i32 7, i32 11, i32 13, i32 17, i32 19, i32 23, i32 29, i32 31, i32 37, i32 41, i32 43, i32 47, i32 53, i32 59, i32 61, i32 67, i32 71, i32 73, i32 79, i32 83, i32 89, i32 97]
@1 = internal unnamed_addr constant i32 1060< / code > < / pre >
2017-11-07 08:22:27 +00:00
< p >
Note that we did not have to do anything special with the syntax of these functions. For example,
2018-09-14 14:35:03 +00:00
we could call the {#syntax#}sum{#endsyntax#} function as is with a slice of numbers whose length and values were
2017-11-07 08:22:27 +00:00
only known at run-time.
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_close#}
{#header_open|Generic Data Structures#}
2017-11-07 08:22:27 +00:00
< p >
2023-09-21 14:50:48 +00:00
Zig uses comptime capabilities to implement generic data structures without introducing any
special-case syntax.
2017-11-07 08:22:27 +00:00
< / p >
< p >
2022-02-04 19:27:50 +00:00
Here is an example of a generic {#syntax#}List{#endsyntax#} data structure.
2017-11-07 08:22:27 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|generic_data_structure.zig#}
2022-02-04 19:27:50 +00:00
2017-11-07 08:22:27 +00:00
< p >
2022-02-04 19:27:50 +00:00
That's it. It's a function that returns an anonymous {#syntax#}struct{#endsyntax#}.
For the purposes of error messages and debugging, Zig infers the name
{#syntax#}"List(i32)"{#endsyntax#} from the function name and parameters invoked when creating
2017-11-07 08:22:27 +00:00
the anonymous struct.
< / p >
< p >
2022-02-04 19:27:50 +00:00
To explicitly give a type a name, we assign it to a constant.
2017-11-07 08:22:27 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|anonymous_struct_name.zig#}
2022-02-04 19:27:50 +00:00
2017-11-07 08:22:27 +00:00
< p >
2022-02-04 19:27:50 +00:00
In this example, the {#syntax#}Node{#endsyntax#} struct refers to itself.
This works because all top level declarations are order-independent.
As long as the compiler can determine the size of the struct, it is free to refer to itself.
In this case, {#syntax#}Node{#endsyntax#} refers to itself as a pointer, which has a
well-defined size at compile time, so it works fine.
2017-11-07 08:22:27 +00:00
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
2021-09-20 08:32:34 +00:00
{#header_open|Case Study: print in Zig#}
2017-11-07 08:22:27 +00:00
< p >
2021-09-20 08:32:34 +00:00
Putting all of this together, let's see how {#syntax#}print{#endsyntax#} works in Zig.
2017-11-07 08:22:27 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|print.zig#}
2017-11-07 08:22:27 +00:00
< p >
Let's crack open the implementation of this and see how it works:
< / p >
2024-04-25 00:41:47 +00:00
{#code|poc_print_fn.zig#}
2021-09-20 08:32:34 +00:00
2017-11-07 08:22:27 +00:00
< p >
This is a proof of concept implementation; the actual function in the standard library has more
formatting capabilities.
< / p >
< p >
Note that this is not hard-coded into the Zig compiler; this is userland code in the standard library.
< / p >
< p >
When this function is analyzed from our example code above, Zig partially evaluates the function
and emits a function that actually looks like this:
< / p >
2021-09-20 08:32:34 +00:00
{#syntax_block|zig|Emitted print Function#}
pub fn print(self: *Writer, arg0: []const u8, arg1: i32) !void {
2018-01-07 21:51:46 +00:00
try self.write("here is a string: '");
try self.printValue(arg0);
try self.write("' here is a number: ");
try self.printValue(arg1);
try self.write("\n");
try self.flush();
2018-01-19 08:03:20 +00:00
}
2021-09-20 08:32:34 +00:00
{#end_syntax_block#}
2017-11-07 08:22:27 +00:00
< p >
2018-09-14 14:35:03 +00:00
{#syntax#}printValue{#endsyntax#} is a function that takes a parameter of any type, and does different things depending
2017-11-07 08:22:27 +00:00
on the type:
< / p >
2024-04-25 00:41:47 +00:00
{#code|poc_printValue_fn.zig#}
2021-09-20 08:32:34 +00:00
2017-11-07 08:22:27 +00:00
< p >
2021-09-20 08:32:34 +00:00
And now, what happens if we give too many arguments to {#syntax#}print{#endsyntax#}?
2017-11-07 08:22:27 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_print_too_many_args.zig#}
2018-01-19 08:03:20 +00:00
2017-11-07 08:22:27 +00:00
< p >
Zig gives programmers the tools needed to protect themselves against their own mistakes.
< / p >
< p >
Zig doesn't care whether the format argument is a string literal,
2019-11-08 20:56:21 +00:00
only that it is a compile-time known value that can be coerced to a {#syntax#}[]const u8{#endsyntax#}:
2017-11-07 08:22:27 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|print_comptime-known_format.zig#}
2017-11-07 08:22:27 +00:00
< p >
This works fine.
< / p >
< p >
Zig does not special case string formatting in the compiler and instead exposes enough power to accomplish this
task in userland. It does so without introducing another language on top of Zig, such as
a macro language or a preprocessor language. It's Zig all the way down.
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-06-19 21:21:08 +00:00
{#see_also|inline while|inline for#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Assembly#}
2019-03-20 23:00:23 +00:00
< p >
For some use cases, it may be necessary to directly control the machine code generated
by Zig programs, rather than relying on Zig's code generation. For these cases, one
can use inline assembly. Here is an example of implementing Hello, World on x86_64 Linux
using inline assembly:
< / p >
2024-04-25 00:41:47 +00:00
{#code|inline_assembly.zig#}
2019-03-20 23:00:23 +00:00
< p >
Dissecting the syntax:
< / p >
2024-04-25 00:41:47 +00:00
{#code|Assembly Syntax Explained.zig#}
2019-03-20 23:00:23 +00:00
< p >
2022-10-07 17:54:44 +00:00
For x86 and x86_64 targets, the syntax is AT& T syntax, rather than the more
2019-03-20 23:00:23 +00:00
popular Intel syntax. This is due to technical constraints; assembly parsing is
provided by LLVM and its support for Intel syntax is buggy and not well tested.
< / p >
< p >
Some day Zig may have its own assembler. This would allow it to integrate more seamlessly
into the language, as well as be compatible with the popular NASM syntax. This documentation
section will be updated before 1.0.0 is released, with a conclusive statement about the status
of AT& T vs Intel/NASM syntax.
< / p >
{#header_open|Output Constraints#}
< p >
2019-09-19 15:14:42 +00:00
Output constraints are still considered to be unstable in Zig, and
2019-03-20 23:00:23 +00:00
so
2019-10-02 06:11:46 +00:00
< a href = "http://releases.llvm.org/10.0.0/docs/LangRef.html#inline-asm-constraint-string" > LLVM documentation< / a >
2019-03-20 23:00:23 +00:00
and
< a href = "https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html" > GCC documentation< / a >
must be used to understand the semantics.
< / p >
< p >
Note that some breaking changes to output constraints are planned with
< a href = "https://github.com/ziglang/zig/issues/215" > issue #215< / a > .
< / p >
{#header_close#}
{#header_open|Input Constraints#}
< p >
2019-09-19 15:14:42 +00:00
Input constraints are still considered to be unstable in Zig, and
2019-03-20 23:00:23 +00:00
so
2019-10-02 06:11:46 +00:00
< a href = "http://releases.llvm.org/10.0.0/docs/LangRef.html#inline-asm-constraint-string" > LLVM documentation< / a >
2019-03-20 23:00:23 +00:00
and
< a href = "https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html" > GCC documentation< / a >
must be used to understand the semantics.
< / p >
< p >
Note that some breaking changes to input constraints are planned with
< a href = "https://github.com/ziglang/zig/issues/215" > issue #215< / a > .
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
2019-03-20 23:00:23 +00:00
{#header_open|Clobbers#}
< p >
Clobbers are the set of registers whose values will not be preserved by the execution of
the assembly code. These do not include output or input registers. The special clobber
value of {#syntax#}"memory"{#endsyntax#} means that the assembly causes writes to
arbitrary undeclared memory locations - not only the memory pointed to by a declared
2019-09-19 15:14:42 +00:00
indirect output.
2019-03-20 23:00:23 +00:00
< / p >
< p >
Failure to declare the full set of clobbers for a given inline assembly
expression is unchecked {#link|Undefined Behavior#}.
< / p >
{#header_close#}
{#header_open|Global Assembly#}
< p >
2022-11-09 08:29:50 +00:00
When an assembly expression occurs in a {#link|container|Containers#} level {#link|comptime#} block, this is
2019-03-20 23:00:23 +00:00
< strong > global assembly< / strong > .
< / p >
< p >
This kind of assembly has different rules than inline assembly. First, {#syntax#}volatile{#endsyntax#}
is not valid because all global assembly is unconditionally included.
Second, there are no inputs, outputs, or clobbers. All global assembly is concatenated
verbatim into one long string and assembled together. There are no template substitution rules regarding
< code > %< / code > as there are in inline assembly expressions.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_global_assembly.zig#}
2019-03-20 23:00:23 +00:00
{#header_close#}
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|Atomics#}
2017-11-07 08:22:27 +00:00
< p > TODO: @atomic rmw< / p >
< p > TODO: builtin atomic memory ordering enum< / p >
2023-09-21 14:50:48 +00:00
2024-10-04 22:21:27 +00:00
{#see_also|@atomicLoad|@atomicStore|@atomicRmw|@cmpxchgWeak|@cmpxchgStrong#}
2023-09-21 14:50:48 +00:00
2018-06-11 21:34:45 +00:00
{#header_close#}
2019-08-15 20:46:43 +00:00
2022-12-01 22:28:44 +00:00
{#header_open|Async Functions#}
2024-04-07 21:13:03 +00:00
< p > Async functions regressed with the release of 0.11.0. Their future in
the Zig language is unclear due to multiple unsolved problems:< / p >
< ul >
< li > LLVM's lack of ability to optimize them.< / li >
< li > Third-party debuggers' lack of ability to debug them.< / li >
< li > < a href = "https://github.com/ziglang/zig/issues/5913" > The cancellation problem< / a > .< / li >
< li > Async function pointers preventing the stack size from being known.< / li >
< / ul >
< p > These problems are surmountable, but it will take time. The Zig team
is currently focused on other priorities.< / p >
2018-06-11 21:34:45 +00:00
{#header_close#}
2019-02-22 15:56:49 +00:00
2022-12-01 22:28:44 +00:00
{#header_open|Builtin Functions|2col#}
2017-11-07 08:22:27 +00:00
< p >
Builtin functions are provided by the compiler and are prefixed with < code > @< / code > .
2018-09-14 14:35:03 +00:00
The {#syntax#}comptime{#endsyntax#} keyword on a parameter means that the parameter must be known
2017-11-07 08:22:27 +00:00
at compile time.
< / p >
2022-09-16 22:24:51 +00:00
{#header_open|@addrSpaceCast#}
2023-06-23 18:37:50 +00:00
< pre > {#syntax#}@addrSpaceCast(ptr: anytype) anytype{#endsyntax#}< / pre >
2022-09-16 22:24:51 +00:00
< p >
2023-06-23 18:37:50 +00:00
Converts a pointer from one address space to another. The new address space is inferred
based on the result type. Depending on the current target and address spaces, this cast
may be a no-op, a complex operation, or illegal. If the cast is legal, then the resulting
pointer points to the same memory location as the pointer operand. It is always valid to
cast a pointer between the same address spaces.
2022-09-16 22:24:51 +00:00
< / p >
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|@addWithOverflow#}
2022-12-21 14:40:30 +00:00
< pre > {#syntax#}@addWithOverflow(a: anytype, b: anytype) struct { @TypeOf(a, b), u1 }{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
2022-12-21 14:40:30 +00:00
Performs {#syntax#}a + b{#endsyntax#} and returns a tuple with the result and a possible overflow bit.
2017-11-07 08:22:27 +00:00
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-12-15 01:21:23 +00:00
{#header_open|@alignCast#}
2023-06-23 18:37:50 +00:00
< pre > {#syntax#}@alignCast(ptr: anytype) anytype{#endsyntax#}< / pre >
2018-12-15 01:21:23 +00:00
< p >
2022-09-23 09:08:30 +00:00
{#syntax#}ptr{#endsyntax#} can be {#syntax#}*T{#endsyntax#}, {#syntax#}?*T{#endsyntax#}, or {#syntax#}[]T{#endsyntax#}.
2023-06-23 18:37:50 +00:00
Changes the alignment of a pointer. The alignment to use is inferred based on the result type.
2018-12-15 01:21:23 +00:00
< / p >
< p > A {#link|pointer alignment safety check|Incorrect Pointer Alignment#} is added
to the generated code to make sure the pointer is aligned as promised.< / p >
{#header_close#}
{#header_open|@alignOf#}
< pre > {#syntax#}@alignOf(comptime T: type) comptime_int{#endsyntax#}< / pre >
< p >
This function returns the number of bytes that this type should be aligned to
for the current target to match the C ABI. When the child type of a pointer has
this alignment, the alignment can be omitted from the type.
< / p >
2022-03-31 07:18:34 +00:00
< pre > {#syntax#}const assert = @import("std").debug.assert;
2018-12-15 01:21:23 +00:00
comptime {
2021-05-08 07:45:19 +00:00
assert(*u32 == *align(@alignOf(u32)) u32);
2018-12-15 01:21:23 +00:00
}{#endsyntax#}< / pre >
< p >
The result is a target-specific compile time constant. It is guaranteed to be
less than or equal to {#link|@sizeOf(T)|@sizeOf#}.
< / p >
{#see_also|Alignment#}
{#header_close#}
2019-08-15 20:46:43 +00:00
2019-11-08 00:05:52 +00:00
{#header_open|@as#}
< pre > {#syntax#}@as(comptime T: type, expression) T{#endsyntax#}< / pre >
< p >
Performs {#link|Type Coercion#}. This cast is allowed when the conversion is unambiguous and safe,
and is the preferred way to convert between types, whenever possible.
< / p >
{#header_close#}
2018-04-15 22:12:00 +00:00
{#header_open|@atomicLoad#}
2024-01-22 03:39:50 +00:00
< pre > {#syntax#}@atomicLoad(comptime T: type, ptr: *const T, comptime ordering: AtomicOrder) T{#endsyntax#}< / pre >
2018-04-15 22:12:00 +00:00
< p >
2023-09-21 14:50:48 +00:00
This builtin function atomically dereferences a pointer to a {#syntax#}T{#endsyntax#} and returns the value.
2018-04-15 22:12:00 +00:00
< / p >
< p >
2020-09-04 18:41:34 +00:00
{#syntax#}T{#endsyntax#} must be a pointer, a {#syntax#}bool{#endsyntax#}, a float,
2020-03-11 11:29:17 +00:00
an integer or an enum.
2018-04-15 22:12:00 +00:00
< / p >
2024-01-22 03:39:50 +00:00
< p > {#syntax#}AtomicOrder{#endsyntax#} can be found with {#syntax#}@import("std").builtin.AtomicOrder{#endsyntax#}.< / p >
2024-10-04 22:21:27 +00:00
{#see_also|@atomicStore|@atomicRmw||@cmpxchgWeak|@cmpxchgStrong#}
2018-04-15 22:12:00 +00:00
{#header_close#}
2021-09-16 01:55:39 +00:00
2018-03-01 02:19:51 +00:00
{#header_open|@atomicRmw#}
2024-01-22 03:39:50 +00:00
< pre > {#syntax#}@atomicRmw(comptime T: type, ptr: *T, comptime op: AtomicRmwOp, operand: T, comptime ordering: AtomicOrder) T{#endsyntax#}< / pre >
2018-03-01 02:19:51 +00:00
< p >
2023-09-21 14:50:48 +00:00
This builtin function dereferences a pointer to a {#syntax#}T{#endsyntax#} and atomically
modifies the value and returns the previous value.
2018-03-01 02:19:51 +00:00
< / p >
< p >
2020-09-04 18:41:34 +00:00
{#syntax#}T{#endsyntax#} must be a pointer, a {#syntax#}bool{#endsyntax#}, a float,
2020-03-11 11:29:17 +00:00
an integer or an enum.
2018-03-01 02:19:51 +00:00
< / p >
2024-01-22 03:39:50 +00:00
< p > {#syntax#}AtomicOrder{#endsyntax#} can be found with {#syntax#}@import("std").builtin.AtomicOrder{#endsyntax#}.< / p >
< p > {#syntax#}AtomicRmwOp{#endsyntax#} can be found with {#syntax#}@import("std").builtin.AtomicRmwOp{#endsyntax#}.< / p >
2024-10-04 22:21:27 +00:00
{#see_also|@atomicStore|@atomicLoad|@cmpxchgWeak|@cmpxchgStrong#}
2018-03-01 02:19:51 +00:00
{#header_close#}
2021-09-16 01:55:39 +00:00
2019-11-12 22:25:44 +00:00
{#header_open|@atomicStore#}
2024-01-22 03:39:50 +00:00
< pre > {#syntax#}@atomicStore(comptime T: type, ptr: *T, value: T, comptime ordering: AtomicOrder) void{#endsyntax#}< / pre >
2019-11-12 22:25:44 +00:00
< p >
2023-09-21 14:50:48 +00:00
This builtin function dereferences a pointer to a {#syntax#}T{#endsyntax#} and atomically stores the given value.
2019-11-12 22:25:44 +00:00
< / p >
< p >
2020-09-04 18:41:34 +00:00
{#syntax#}T{#endsyntax#} must be a pointer, a {#syntax#}bool{#endsyntax#}, a float,
2020-03-11 11:29:17 +00:00
an integer or an enum.
2019-11-12 22:25:44 +00:00
< / p >
2024-01-22 03:39:50 +00:00
< p > {#syntax#}AtomicOrder{#endsyntax#} can be found with {#syntax#}@import("std").builtin.AtomicOrder{#endsyntax#}.< / p >
2024-10-04 22:21:27 +00:00
{#see_also|@atomicLoad|@atomicRmw|@cmpxchgWeak|@cmpxchgStrong#}
2019-11-12 22:25:44 +00:00
{#header_close#}
Add @select
@select(
comptime T: type,
pred: std.meta.Vector(len, bool),
a: std.meta.Vector(len, T),
b: std.meta.Vector(len, T)
) std.meta.Vector(len, T)
Constructs a vector from a & b, based on the values in the predicate vector. For indices where the predicate value is true, the corresponding
element from the a vector is selected, and otherwise from b.
2021-07-26 00:37:11 +00:00
2018-01-17 04:19:05 +00:00
{#header_open|@bitCast#}
2023-06-23 18:37:50 +00:00
< pre > {#syntax#}@bitCast(value: anytype) anytype{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
2023-06-23 18:37:50 +00:00
Converts a value of one type to another type. The return type is the
inferred result type.
2017-11-07 08:22:27 +00:00
< / p >
< p >
2019-12-09 20:59:42 +00:00
Asserts that {#syntax#}@sizeOf(@TypeOf(value)) == @sizeOf(DestType){#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / p >
< p >
2024-08-28 01:35:53 +00:00
Asserts that {#syntax#}@typeInfo(DestType) != .pointer{#endsyntax#}. Use {#syntax#}@ptrCast{#endsyntax#} or {#syntax#}@ptrFromInt{#endsyntax#} if you need this.
2017-11-07 08:22:27 +00:00
< / p >
< p >
Can be used for these things for example:
< / p >
< ul >
2018-09-14 14:35:03 +00:00
< li > Convert {#syntax#}f32{#endsyntax#} to {#syntax#}u32{#endsyntax#} bits< / li >
< li > Convert {#syntax#}i32{#endsyntax#} to {#syntax#}u32{#endsyntax#} preserving twos complement< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< p >
2022-06-02 18:29:09 +00:00
Works at compile-time if {#syntax#}value{#endsyntax#} is known at compile time. It's a compile error to bitcast a value of undefined layout; this means that, besides the restriction from types which possess dedicated casting builtins (enums, pointers, error sets), bare structs, error unions, slices, optionals, and any other type without a well-defined memory layout, also cannot be used in this operation.
2017-11-07 08:22:27 +00:00
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-12-15 01:21:23 +00:00
2018-09-21 18:15:58 +00:00
{#header_open|@bitOffsetOf#}
2019-06-09 23:24:24 +00:00
< pre > {#syntax#}@bitOffsetOf(comptime T: type, comptime field_name: []const u8) comptime_int{#endsyntax#}< / pre >
2018-09-21 18:15:58 +00:00
< p >
Returns the bit offset of a field relative to its containing struct.
< / p >
< p >
For non {#link|packed structs|packed struct#}, this will always be divisible by {#syntax#}8{#endsyntax#}.
For packed structs, non-byte-aligned fields will share a byte offset, but they will have different
bit offsets.
< / p >
2021-06-10 20:56:55 +00:00
{#see_also|@offsetOf#}
2018-09-21 18:15:58 +00:00
{#header_close#}
2018-06-18 07:07:16 +00:00
2020-01-16 15:12:43 +00:00
{#header_open|@bitSizeOf#}
< pre > {#syntax#}@bitSizeOf(comptime T: type) comptime_int{#endsyntax#}< / pre >
< p >
2021-07-07 18:39:44 +00:00
This function returns the number of bits it takes to store {#syntax#}T{#endsyntax#} in memory if the type
were a field in a packed struct/union.
2020-01-16 15:12:43 +00:00
The result is a target-specific compile time constant.
< / p >
< p >
This function measures the size at runtime. For types that are disallowed at runtime, such as
{#syntax#}comptime_int{#endsyntax#} and {#syntax#}type{#endsyntax#}, the result is {#syntax#}0{#endsyntax#}.
< / p >
{#see_also|@sizeOf|@typeInfo#}
{#header_close#}
2024-08-24 15:16:53 +00:00
{#header_open|@branchHint#}
< pre > {#syntax#}@branchHint(hint: BranchHint) void{#endsyntax#}< / pre >
< p > Hints to the optimizer how likely a given branch of control flow is to be reached.< / p >
< p > {#syntax#}BranchHint{#endsyntax#} can be found with {#syntax#}@import("std").builtin.BranchHint{#endsyntax#}.< / p >
< p > This function is only valid as the first statement in a control flow branch, or the first statement in a function.< / p >
{#header_close#}
2018-12-15 01:21:23 +00:00
{#header_open|@breakpoint#}
2023-03-03 15:16:08 +00:00
< pre > {#syntax#}@breakpoint() void{#endsyntax#}< / pre >
2018-12-15 01:21:23 +00:00
< p >
This function inserts a platform-specific debug trap instruction which causes
debuggers to break there.
2023-03-03 17:35:03 +00:00
Unlike for {#syntax#}@trap(){#endsyntax#}, execution may continue after this point if the program is resumed.
2018-12-15 01:21:23 +00:00
< / p >
< p >
This function is only valid within function scope.
< / p >
2023-03-03 17:35:03 +00:00
{#see_also|@trap#}
2019-06-18 22:28:49 +00:00
{#header_close#}
2023-03-03 17:35:03 +00:00
2019-06-18 22:28:49 +00:00
{#header_open|@mulAdd#}
< pre > {#syntax#}@mulAdd(comptime T: type, a: T, b: T, c: T) T{#endsyntax#}< / pre >
< p >
2022-10-03 17:52:39 +00:00
Fused multiply-add, similar to {#syntax#}(a * b) + c{#endsyntax#}, except
2019-06-18 22:28:49 +00:00
only rounds once, and is thus more accurate.
< / p >
2020-01-08 16:57:20 +00:00
< p >
2022-04-27 23:45:23 +00:00
Supports {#link|Floats#} and {#link|Vectors#} of floats.
2020-01-08 16:57:20 +00:00
< / p >
2018-12-15 01:21:23 +00:00
{#header_close#}
2019-04-03 14:00:39 +00:00
{#header_open|@byteSwap#}
2022-08-21 14:24:04 +00:00
< pre > {#syntax#}@byteSwap(operand: anytype) T{#endsyntax#}< / pre >
< p > {#syntax#}@TypeOf(operand){#endsyntax#} must be an integer type or an integer vector type with bit count evenly divisible by 8.< / p >
2019-09-19 04:59:04 +00:00
< p > {#syntax#}operand{#endsyntax#} may be an {#link|integer|Integers#} or {#link|vector|Vectors#}.< / p >
2018-12-13 01:19:46 +00:00
< p >
Swaps the byte order of the integer. This converts a big endian integer to a little endian integer,
and converts a little endian integer to a big endian integer.
< / p >
2019-09-19 04:59:04 +00:00
< p >
Note that for the purposes of memory layout with respect to endianness, the integer type should be
related to the number of bytes reported by {#link|@sizeOf#} bytes. This is demonstrated with
{#syntax#}u24{#endsyntax#}. {#syntax#}@sizeOf(u24) == 4{#endsyntax#}, which means that a
{#syntax#}u24{#endsyntax#} stored in memory takes 4 bytes, and those 4 bytes are what are swapped on
a little vs big endian system. On the other hand, if {#syntax#}T{#endsyntax#} is specified to
be {#syntax#}u24{#endsyntax#}, then only 3 bytes are reversed.
< / p >
2018-12-13 01:19:46 +00:00
{#header_close#}
2019-04-03 14:00:39 +00:00
{#header_open|@bitReverse#}
2022-08-21 14:24:04 +00:00
< pre > {#syntax#}@bitReverse(integer: anytype) T{#endsyntax#}< / pre >
< p > {#syntax#}@TypeOf(anytype){#endsyntax#} accepts any integer type or integer vector type.< / p >
2019-01-02 21:47:47 +00:00
< p >
Reverses the bitpattern of an integer value, including the sign bit if applicable.
< / p >
< p >
For example 0b10110110 ({#syntax#}u8 = 182{#endsyntax#}, {#syntax#}i8 = -74{#endsyntax#})
becomes 0b01101101 ({#syntax#}u8 = 109{#endsyntax#}, {#syntax#}i8 = 109{#endsyntax#}).
< / p >
{#header_close#}
2021-06-10 20:56:55 +00:00
{#header_open|@offsetOf#}
< pre > {#syntax#}@offsetOf(comptime T: type, comptime field_name: []const u8) comptime_int{#endsyntax#}< / pre >
2018-12-15 01:21:23 +00:00
< p >
Returns the byte offset of a field relative to its containing struct.
< / p >
{#see_also|@bitOffsetOf#}
{#header_close#}
2019-12-05 22:37:29 +00:00
{#header_open|@call#}
2022-12-12 13:32:37 +00:00
< pre > {#syntax#}@call(modifier: std.builtin.CallModifier, function: anytype, args: anytype) anytype{#endsyntax#}< / pre >
2019-12-05 22:37:29 +00:00
< p >
Calls a function, in the same way that invoking an expression with parentheses does:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_call_builtin.zig#}
2019-12-05 22:37:29 +00:00
< p >
2024-04-25 00:41:47 +00:00
{#syntax#}@call{#endsyntax#} allows more flexibility than normal function call syntax does. The
{#syntax#}CallModifier{#endsyntax#} enum is reproduced here:
< / p >
{#code|builtin.CallModifier struct.zig#}
2019-12-05 22:37:29 +00:00
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|@cDefine#}
2023-07-21 21:03:26 +00:00
< pre > {#syntax#}@cDefine(comptime name: []const u8, value) void{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
2018-09-14 14:35:03 +00:00
This function can only occur inside {#syntax#}@cImport{#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / p >
< p >
2018-09-14 14:35:03 +00:00
This appends < code > #define $name $value< / code > to the {#syntax#}@cImport{#endsyntax#}
2017-11-07 08:22:27 +00:00
temporary buffer.
< / p >
< p >
To define without a value, like this:
< / p >
< pre > < code class = "c" > #define _GNU_SOURCE< / code > < / pre >
< p >
Use the void value, like this:
< / p >
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}@cDefine("_GNU_SOURCE", {}){#endsyntax#}< / pre >
2018-01-17 05:22:33 +00:00
{#see_also|Import from C Header File|@cInclude|@cImport|@cUndef|void#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|@cImport#}
2019-03-13 17:04:55 +00:00
< pre > {#syntax#}@cImport(expression) type{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
2019-03-13 17:04:55 +00:00
This function parses C code and imports the functions, types, variables,
and compatible macro definitions into a new empty struct type, and then
returns that type.
2017-11-07 08:22:27 +00:00
< / p >
< p >
2018-09-14 14:35:03 +00:00
{#syntax#}expression{#endsyntax#} is interpreted at compile time. The builtin functions
{#syntax#}@cInclude{#endsyntax#}, {#syntax#}@cDefine{#endsyntax#}, and {#syntax#}@cUndef{#endsyntax#} work
2017-11-07 08:22:27 +00:00
within this expression, appending to a temporary buffer which is then parsed as C code.
< / p >
2018-02-01 01:18:47 +00:00
< p >
2018-09-14 14:35:03 +00:00
Usually you should only have one {#syntax#}@cImport{#endsyntax#} in your entire application, because it saves the compiler
2018-02-01 01:18:47 +00:00
from invoking clang multiple times, and prevents inline functions from being duplicated.
< / p >
< p >
2018-09-14 14:35:03 +00:00
Reasons for having multiple {#syntax#}@cImport{#endsyntax#} expressions would be:
2018-02-01 01:18:47 +00:00
< / p >
< ul >
2018-09-14 14:35:03 +00:00
< li > To avoid a symbol collision, for example if foo.h and bar.h both < code > #define CONNECTION_COUNT< / code > < / li >
2018-02-01 01:18:47 +00:00
< li > To analyze the C code with different preprocessor defines< / li >
< / ul >
2018-01-17 05:22:33 +00:00
{#see_also|Import from C Header File|@cInclude|@cDefine|@cUndef#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|@cInclude#}
2023-07-21 21:03:26 +00:00
< pre > {#syntax#}@cInclude(comptime path: []const u8) void{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
2018-09-14 14:35:03 +00:00
This function can only occur inside {#syntax#}@cImport{#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / p >
< p >
2019-12-05 21:38:59 +00:00
This appends < code > #include < $path> \n< / code > to the {#syntax#}c_import{#endsyntax#}
2017-11-07 08:22:27 +00:00
temporary buffer.
< / p >
2018-01-17 05:22:33 +00:00
{#see_also|Import from C Header File|@cImport|@cDefine|@cUndef#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-12-15 01:21:23 +00:00
2018-01-17 04:19:05 +00:00
{#header_open|@clz#}
2023-03-03 15:16:08 +00:00
< pre > {#syntax#}@clz(operand: anytype) anytype{#endsyntax#}< / pre >
2022-08-21 14:24:04 +00:00
< p > {#syntax#}@TypeOf(operand){#endsyntax#} must be an integer type or an integer vector type.< / p >
2021-07-26 02:58:58 +00:00
< p > {#syntax#}operand{#endsyntax#} may be an {#link|integer|Integers#} or {#link|vector|Vectors#}.< / p >
2017-11-07 08:22:27 +00:00
< p >
2023-04-23 18:06:21 +00:00
Counts the number of most-significant (leading in a big-endian sense) zeroes in an integer - "count leading zeroes".
2017-11-07 08:22:27 +00:00
< / p >
< p >
2021-07-26 02:58:58 +00:00
If {#syntax#}operand{#endsyntax#} is a {#link|comptime#}-known integer,
2019-05-16 20:32:24 +00:00
the return type is {#syntax#}comptime_int{#endsyntax#}.
2021-07-26 02:58:58 +00:00
Otherwise, the return type is an unsigned integer or vector of unsigned integers with the minimum number
2019-04-03 14:00:39 +00:00
of bits that can represent the bit count of the integer type.
2017-11-07 08:22:27 +00:00
< / p >
< p >
2021-07-26 02:58:58 +00:00
If {#syntax#}operand{#endsyntax#} is zero, {#syntax#}@clz{#endsyntax#} returns the bit width
2019-05-16 20:32:24 +00:00
of integer type {#syntax#}T{#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / p >
2018-07-07 04:25:32 +00:00
{#see_also|@ctz|@popCount#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-12-15 01:21:23 +00:00
2018-04-18 16:16:42 +00:00
{#header_open|@cmpxchgStrong#}
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}@cmpxchgStrong(comptime T: type, ptr: *T, expected_value: T, new_value: T, success_order: AtomicOrder, fail_order: AtomicOrder) ?T{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
2023-09-21 14:50:48 +00:00
This function performs a strong atomic compare-and-exchange operation, returning {#syntax#}null{#endsyntax#}
if the current value is not the given expected value. It's the equivalent of this code,
2018-04-18 16:16:42 +00:00
except atomic:
< / p >
2024-04-25 00:41:47 +00:00
{#code|not_atomic_cmpxchgStrong.zig#}
2018-04-18 16:16:42 +00:00
< p >
2023-09-21 14:50:48 +00:00
If you are using cmpxchg in a retry loop, {#link|@cmpxchgWeak#} is the better choice, because it can be implemented
2018-04-18 16:16:42 +00:00
more efficiently in machine instructions.
2017-11-07 08:22:27 +00:00
< / p >
< p >
2020-09-04 18:41:34 +00:00
{#syntax#}T{#endsyntax#} must be a pointer, a {#syntax#}bool{#endsyntax#}, a float,
2020-03-11 11:29:17 +00:00
an integer or an enum.
2017-11-07 08:22:27 +00:00
< / p >
2024-08-28 01:35:53 +00:00
< p > {#syntax#}@typeInfo(@TypeOf(ptr)).pointer.alignment{#endsyntax#} must be {#syntax#}>= @sizeOf(T).{#endsyntax#}< / p >
2024-01-22 03:39:50 +00:00
< p > {#syntax#}AtomicOrder{#endsyntax#} can be found with {#syntax#}@import("std").builtin.AtomicOrder{#endsyntax#}.< / p >
2024-10-04 22:21:27 +00:00
{#see_also|@atomicStore|@atomicLoad|@atomicRmw|@cmpxchgWeak#}
2018-04-18 16:16:42 +00:00
{#header_close#}
2021-09-16 01:55:39 +00:00
2018-04-18 16:16:42 +00:00
{#header_open|@cmpxchgWeak#}
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}@cmpxchgWeak(comptime T: type, ptr: *T, expected_value: T, new_value: T, success_order: AtomicOrder, fail_order: AtomicOrder) ?T{#endsyntax#}< / pre >
2018-04-18 16:16:42 +00:00
< p >
2023-09-21 14:50:48 +00:00
This function performs a weak atomic compare-and-exchange operation, returning {#syntax#}null{#endsyntax#}
if the current value is not the given expected value. It's the equivalent of this code,
2018-04-18 16:16:42 +00:00
except atomic:
< / p >
2021-09-20 08:32:34 +00:00
{#syntax_block|zig|cmpxchgWeakButNotAtomic#}
2018-05-31 14:56:59 +00:00
fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_value: T) ?T {
2018-05-18 03:21:44 +00:00
const old_value = ptr.*;
2018-04-18 16:16:42 +00:00
if (old_value == expected_value and usuallyTrueButSometimesFalse()) {
2018-05-18 03:21:44 +00:00
ptr.* = new_value;
2018-04-18 16:16:42 +00:00
return null;
} else {
return old_value;
}
}
2021-09-20 08:32:34 +00:00
{#end_syntax_block#}
2018-04-18 16:16:42 +00:00
< p >
2023-09-21 14:50:48 +00:00
If you are using cmpxchg in a retry loop, the sporadic failure will be no problem, and {#syntax#}cmpxchgWeak{#endsyntax#}
2018-04-18 16:16:42 +00:00
is the better choice, because it can be implemented more efficiently in machine instructions.
However if you need a stronger guarantee, use {#link|@cmpxchgStrong#}.
< / p >
< p >
2020-09-04 18:41:34 +00:00
{#syntax#}T{#endsyntax#} must be a pointer, a {#syntax#}bool{#endsyntax#}, a float,
2020-03-11 11:29:17 +00:00
an integer or an enum.
2018-04-18 16:16:42 +00:00
< / p >
2024-08-28 01:35:53 +00:00
< p > {#syntax#}@typeInfo(@TypeOf(ptr)).pointer.alignment{#endsyntax#} must be {#syntax#}>= @sizeOf(T).{#endsyntax#}< / p >
2024-01-22 03:39:50 +00:00
< p > {#syntax#}AtomicOrder{#endsyntax#} can be found with {#syntax#}@import("std").builtin.AtomicOrder{#endsyntax#}.< / p >
2024-10-04 22:21:27 +00:00
{#see_also|@atomicStore|@atomicLoad|@atomicRmw|@cmpxchgStrong#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-12-15 01:21:23 +00:00
2018-01-17 04:19:05 +00:00
{#header_open|@compileError#}
2023-07-21 21:03:26 +00:00
< pre > {#syntax#}@compileError(comptime msg: []const u8) noreturn{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
This function, when semantically analyzed, causes a compile error with the
2018-09-14 14:35:03 +00:00
message {#syntax#}msg{#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / p >
< p >
There are several ways that code avoids being semantically checked, such as
2018-09-14 14:35:03 +00:00
using {#syntax#}if{#endsyntax#} or {#syntax#}switch{#endsyntax#} with compile time constants,
and {#syntax#}comptime{#endsyntax#} functions.
2017-11-07 08:22:27 +00:00
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-12-15 01:21:23 +00:00
2018-01-17 04:19:05 +00:00
{#header_open|@compileLog#}
2024-10-03 11:37:36 +00:00
< pre > {#syntax#}@compileLog(...) void{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
This function prints the arguments passed to it at compile-time.
< / p >
< p >
To prevent accidentally leaving compile log statements in a codebase,
a compilation error is added to the build, pointing to the compile
log statement. This error prevents code from being generated, but
does not otherwise interfere with analysis.
< / p >
< p >
This function can be used to do "printf debugging" on
compile-time executing code.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_compileLog_builtin.zig#}
2017-11-07 08:22:27 +00:00
2018-01-19 08:03:20 +00:00
{#header_close#}
2018-12-15 01:21:23 +00:00
2023-02-13 14:19:17 +00:00
{#header_open|@constCast#}
< pre > {#syntax#}@constCast(value: anytype) DestType{#endsyntax#}< / pre >
< p >
Remove {#syntax#}const{#endsyntax#} qualifier from a pointer.
< / p >
{#header_close#}
2018-01-19 08:03:20 +00:00
{#header_open|@ctz#}
2023-03-03 15:16:08 +00:00
< pre > {#syntax#}@ctz(operand: anytype) anytype{#endsyntax#}< / pre >
2022-08-21 14:24:04 +00:00
< p > {#syntax#}@TypeOf(operand){#endsyntax#} must be an integer type or an integer vector type.< / p >
2021-07-26 02:58:58 +00:00
< p > {#syntax#}operand{#endsyntax#} may be an {#link|integer|Integers#} or {#link|vector|Vectors#}.< / p >
2017-11-07 08:22:27 +00:00
< p >
2023-04-23 18:06:21 +00:00
Counts the number of least-significant (trailing in a big-endian sense) zeroes in an integer - "count trailing zeroes".
2017-11-07 08:22:27 +00:00
< / p >
< p >
2021-07-26 02:58:58 +00:00
If {#syntax#}operand{#endsyntax#} is a {#link|comptime#}-known integer,
2019-05-16 20:32:24 +00:00
the return type is {#syntax#}comptime_int{#endsyntax#}.
2021-07-26 02:58:58 +00:00
Otherwise, the return type is an unsigned integer or vector of unsigned integers with the minimum number
2019-04-03 14:00:39 +00:00
of bits that can represent the bit count of the integer type.
2017-11-07 08:22:27 +00:00
< / p >
< p >
2021-07-26 02:58:58 +00:00
If {#syntax#}operand{#endsyntax#} is zero, {#syntax#}@ctz{#endsyntax#} returns
2019-05-16 20:32:24 +00:00
the bit width of integer type {#syntax#}T{#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / p >
2018-07-07 04:25:32 +00:00
{#see_also|@clz|@popCount#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-12-15 01:21:23 +00:00
{#header_open|@cUndef#}
2023-07-21 21:03:26 +00:00
< pre > {#syntax#}@cUndef(comptime name: []const u8) void{#endsyntax#}< / pre >
2018-12-15 01:21:23 +00:00
< p >
This function can only occur inside {#syntax#}@cImport{#endsyntax#}.
< / p >
< p >
This appends < code > #undef $name< / code > to the {#syntax#}@cImport{#endsyntax#}
temporary buffer.
< / p >
{#see_also|Import from C Header File|@cImport|@cDefine|@cInclude#}
{#header_close#}
2022-12-12 22:14:54 +00:00
{#header_open|@cVaArg#}
< pre > {#syntax#}@cVaArg(operand: *std.builtin.VaList, comptime T: type) T{#endsyntax#}< / pre >
< p >
Implements the C macro {#syntax#}va_arg{#endsyntax#}.
< / p >
{#see_also|@cVaCopy|@cVaEnd|@cVaStart#}
{#header_close#}
{#header_open|@cVaCopy#}
< pre > {#syntax#}@cVaCopy(src: *std.builtin.VaList) std.builtin.VaList{#endsyntax#}< / pre >
< p >
Implements the C macro {#syntax#}va_copy{#endsyntax#}.
< / p >
{#see_also|@cVaArg|@cVaEnd|@cVaStart#}
{#header_close#}
{#header_open|@cVaEnd#}
< pre > {#syntax#}@cVaEnd(src: *std.builtin.VaList) void{#endsyntax#}< / pre >
< p >
Implements the C macro {#syntax#}va_end{#endsyntax#}.
< / p >
{#see_also|@cVaArg|@cVaCopy|@cVaStart#}
{#header_close#}
{#header_open|@cVaStart#}
< pre > {#syntax#}@cVaStart() std.builtin.VaList{#endsyntax#}< / pre >
< p >
Implements the C macro {#syntax#}va_start{#endsyntax#}. Only valid inside a variadic function.
< / p >
{#see_also|@cVaArg|@cVaCopy|@cVaEnd#}
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|@divExact#}
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}@divExact(numerator: T, denominator: T) T{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
2018-09-14 14:35:03 +00:00
Exact division. Caller guarantees {#syntax#}denominator != 0{#endsyntax#} and
{#syntax#}@divTrunc(numerator, denominator) * denominator == numerator{#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / p >
< ul >
2018-09-14 14:35:03 +00:00
< li > {#syntax#}@divExact(6, 3) == 2{#endsyntax#}< / li >
< li > {#syntax#}@divExact(a, b) * b == a{#endsyntax#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
2018-09-14 14:35:03 +00:00
< p > For a function that returns a possible error code, use {#syntax#}@import("std").math.divExact{#endsyntax#}.< / p >
2018-01-17 05:22:33 +00:00
{#see_also|@divTrunc|@divFloor#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|@divFloor#}
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}@divFloor(numerator: T, denominator: T) T{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
Floored division. Rounds toward negative infinity. For unsigned integers it is
2018-09-14 14:35:03 +00:00
the same as {#syntax#}numerator / denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator != 0{#endsyntax#} and
2024-08-28 01:35:53 +00:00
{#syntax#}!(@typeInfo(T) == .int and T.is_signed and numerator == std.math.minInt(T) and denominator == -1){#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / p >
< ul >
2018-09-14 14:35:03 +00:00
< li > {#syntax#}@divFloor(-5, 3) == -2{#endsyntax#}< / li >
2020-10-19 02:43:34 +00:00
< li > {#syntax#}(@divFloor(a, b) * b) + @mod(a, b) == a{#endsyntax#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
2018-09-14 14:35:03 +00:00
< p > For a function that returns a possible error code, use {#syntax#}@import("std").math.divFloor{#endsyntax#}.< / p >
2018-01-17 05:22:33 +00:00
{#see_also|@divTrunc|@divExact#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|@divTrunc#}
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}@divTrunc(numerator: T, denominator: T) T{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
Truncated division. Rounds toward zero. For unsigned integers it is
2018-09-14 14:35:03 +00:00
the same as {#syntax#}numerator / denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator != 0{#endsyntax#} and
2024-08-28 01:35:53 +00:00
{#syntax#}!(@typeInfo(T) == .int and T.is_signed and numerator == std.math.minInt(T) and denominator == -1){#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / p >
< ul >
2018-09-14 14:35:03 +00:00
< li > {#syntax#}@divTrunc(-5, 3) == -1{#endsyntax#}< / li >
2020-10-19 02:43:34 +00:00
< li > {#syntax#}(@divTrunc(a, b) * b) + @rem(a, b) == a{#endsyntax#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
2018-09-14 14:35:03 +00:00
< p > For a function that returns a possible error code, use {#syntax#}@import("std").math.divTrunc{#endsyntax#}.< / p >
2018-01-17 05:22:33 +00:00
{#see_also|@divFloor|@divExact#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2023-06-28 09:22:32 +00:00
2018-01-17 04:19:05 +00:00
{#header_open|@embedFile#}
2021-04-28 00:26:03 +00:00
< pre > {#syntax#}@embedFile(comptime path: []const u8) *const [N:0]u8{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
2020-03-06 21:01:26 +00:00
This function returns a compile time constant pointer to null-terminated,
fixed-size array with length equal to the byte count of the file given by
{#syntax#}path{#endsyntax#}. The contents of the array are the contents of the file.
2021-02-24 13:26:13 +00:00
This is equivalent to a {#link|string literal|String Literals and Unicode Code Point Literals#}
2020-03-06 21:01:26 +00:00
with the file contents.
2017-11-07 08:22:27 +00:00
< / p >
< p >
2018-09-14 14:35:03 +00:00
{#syntax#}path{#endsyntax#} is absolute or relative to the current file, just like {#syntax#}@import{#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / p >
2018-01-17 05:22:33 +00:00
{#see_also|@import#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-06-18 19:01:42 +00:00
2023-06-28 09:22:32 +00:00
{#header_open|@enumFromInt#}
< pre > {#syntax#}@enumFromInt(integer: anytype) anytype{#endsyntax#}< / pre >
2018-06-19 07:50:38 +00:00
< p >
2023-06-28 09:22:32 +00:00
Converts an integer into an {#link|enum#} value. The return type is the inferred result type.
2018-06-19 07:50:38 +00:00
< / p >
2018-09-13 17:29:43 +00:00
< p >
2024-10-04 20:50:25 +00:00
Attempting to convert an integer with no corresponding value in the enum invokes
2023-06-28 09:22:32 +00:00
safety-checked {#link|Undefined Behavior#}.
2024-07-21 09:41:43 +00:00
Note that a {#link|non-exhaustive enum|Non-exhaustive enum#} has corresponding values for all
2024-10-04 20:50:25 +00:00
integers in the enum's integer tag type: the {#syntax#}_{#endsyntax#} value represents all
2024-07-21 09:41:43 +00:00
the remaining unnamed integers in the enum's tag type.
2018-09-13 17:29:43 +00:00
< / p >
2023-06-28 09:22:32 +00:00
{#see_also|@intFromEnum#}
{#header_close#}
{#header_open|@errorFromInt#}
2023-10-21 09:56:05 +00:00
< pre > {#syntax#}@errorFromInt(value: std.meta.Int(.unsigned, @bitSizeOf(anyerror))) anyerror{#endsyntax#}< / pre >
2023-06-28 09:22:32 +00:00
< p >
Converts from the integer representation of an error into {#link|The Global Error Set#} type.
< / p >
< p >
It is generally recommended to avoid this
cast, as the integer representation of an error is not stable across source code changes.
< / p >
< p >
Attempting to convert an integer that does not correspond to any error results in
safety-protected {#link|Undefined Behavior#}.
< / p >
{#see_also|@intFromError#}
2018-06-19 07:50:38 +00:00
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|@errorName#}
2021-04-28 00:00:32 +00:00
< pre > {#syntax#}@errorName(err: anyerror) [:0]const u8{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
2018-11-19 18:20:39 +00:00
This function returns the string representation of an error. The string representation
of {#syntax#}error.OutOfMem{#endsyntax#} is {#syntax#}"OutOfMem"{#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / p >
< p >
2018-09-14 14:35:03 +00:00
If there are no calls to {#syntax#}@errorName{#endsyntax#} in an entire application,
2018-11-19 18:20:39 +00:00
or all calls have a compile-time known value for {#syntax#}err{#endsyntax#}, then no
2017-11-07 08:22:27 +00:00
error name table will be generated.
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-06-18 22:48:29 +00:00
2018-01-17 04:19:05 +00:00
{#header_open|@errorReturnTrace#}
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}@errorReturnTrace() ?*builtin.StackTrace{#endsyntax#}< / pre >
2018-01-15 05:01:02 +00:00
< p >
If the binary is built with error return tracing, and this function is invoked in a
function that calls a function with an error or error union return type, returns a
2020-11-03 17:37:28 +00:00
stack trace object. Otherwise returns {#link|null#}.
2018-01-15 05:01:02 +00:00
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-06-18 19:01:42 +00:00
2023-10-01 10:16:02 +00:00
{#header_open|@errorCast#}
< pre > {#syntax#}@errorCast(value: anytype) anytype{#endsyntax#}< / pre >
2018-12-15 01:21:23 +00:00
< p >
2023-10-01 10:16:02 +00:00
Converts an error set or error union value from one error set to another error set. The return type is the
2023-06-23 18:37:50 +00:00
inferred result type. Attempting to convert an error which is not in the destination error
set results in safety-protected {#link|Undefined Behavior#}.
2018-12-15 01:21:23 +00:00
< / p >
{#header_close#}
2018-06-18 19:01:42 +00:00
{#header_open|@export#}
2024-08-26 02:49:36 +00:00
< pre > {#syntax#}@export(comptime ptr: *const anyopaque, comptime options: std.builtin.ExportOptions) void{#endsyntax#}< / pre >
< p > Creates a symbol in the output object file which refers to the target of < code > ptr< / code > .< / p >
< p > < code > ptr< / code > must point to a global variable or a comptime-known constant.< / p >
2021-06-15 21:08:57 +00:00
< p >
This builtin can be called from a {#link|comptime#} block to conditionally export symbols.
2024-08-26 02:49:36 +00:00
When < code > ptr< / code > points to a function with the C calling convention and
{#syntax#}options.linkage{#endsyntax#} is {#syntax#}.Strong{#endsyntax#}, this is equivalent to
2019-03-13 15:54:56 +00:00
the {#syntax#}export{#endsyntax#} keyword used on a function:
< / p >
2024-04-25 00:41:47 +00:00
{#code|export_builtin.zig#}
2019-03-13 15:54:56 +00:00
< p > This is equivalent to:< / p >
2024-04-25 00:41:47 +00:00
{#code|export_builtin_equivalent_code.zig#}
2022-02-04 19:27:50 +00:00
< p > Note that even when using {#syntax#}export{#endsyntax#}, the {#syntax#}@"foo"{#endsyntax#} syntax for
{#link|identifiers|Identifiers#} can be used to choose any string for the symbol name:< / p >
2024-04-25 00:41:47 +00:00
{#code|export_any_symbol_name.zig#}
2019-03-13 15:54:56 +00:00
< p >
When looking at the resulting object, you can see the symbol is used verbatim:
< / p >
2020-07-27 14:10:55 +00:00
< pre > < code > 00000000000001f0 T A function name that is a complete sentence.< / code > < / pre >
2019-03-13 15:54:56 +00:00
{#see_also|Exporting a C Library#}
2018-06-18 19:01:42 +00:00
{#header_close#}
2021-04-22 23:24:14 +00:00
{#header_open|@extern#}
2023-07-08 13:11:58 +00:00
< pre > {#syntax#}@extern(T: type, comptime options: std.builtin.ExternOptions) T{#endsyntax#}< / pre >
2021-04-22 23:24:14 +00:00
< p >
Creates a reference to an external symbol in the output object file.
2023-07-08 13:11:58 +00:00
T must be a pointer type.
2021-04-22 23:24:14 +00:00
< / p >
{#see_also|@export#}
{#header_close#}
2018-06-18 07:07:16 +00:00
{#header_open|@field#}
2020-07-11 11:08:20 +00:00
< pre > {#syntax#}@field(lhs: anytype, comptime field_name: []const u8) (field){#endsyntax#}< / pre >
2021-02-01 17:11:06 +00:00
< p > Performs field access by a compile-time string. Works on both fields and declarations.
2018-09-14 14:35:03 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_field_builtin.zig#}
2019-09-04 17:02:31 +00:00
2018-06-18 07:07:16 +00:00
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|@fieldParentPtr#}
2024-03-19 11:46:38 +00:00
< pre > {#syntax#}@fieldParentPtr(comptime field_name: []const u8, field_ptr: *T) anytype{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
Given a pointer to a field, returns the base pointer of a struct.
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-06-18 07:07:16 +00:00
2024-10-17 09:19:14 +00:00
{#header_open|@FieldType#}
< pre > {#syntax#}@FieldType(comptime Type: type, comptime field_name: []const u8) type{#endsyntax#}< / pre >
< p >
Given a type and the name of one of its fields, returns the type of that field.
< / p >
{#header_close#}
2018-06-18 07:07:16 +00:00
{#header_open|@floatCast#}
2023-06-23 18:37:50 +00:00
< pre > {#syntax#}@floatCast(value: anytype) anytype{#endsyntax#}< / pre >
2018-06-18 07:07:16 +00:00
< p >
Convert from one float type to another. This cast is safe, but may cause the
2023-06-23 18:37:50 +00:00
numeric value to lose precision. The return type is the inferred result type.
2018-06-18 07:07:16 +00:00
< / p >
{#header_close#}
2023-06-28 09:22:32 +00:00
{#header_open|@floatFromInt#}
< pre > {#syntax#}@floatFromInt(int: anytype) anytype{#endsyntax#}< / pre >
2018-06-19 20:06:10 +00:00
< p >
2023-06-28 09:22:32 +00:00
Converts an integer to the closest floating point representation. The return type is the inferred result type.
2024-01-19 03:58:35 +00:00
To convert the other way, use {#link|@intFromFloat#}. This operation is legal
for all values of all integer types.
2018-06-19 20:06:10 +00:00
< / p >
2018-06-18 07:07:16 +00:00
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|@frameAddress#}
2019-03-02 20:34:58 +00:00
< pre > {#syntax#}@frameAddress() usize{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
This function returns the base pointer of the current stack frame.
< / p >
< p >
2022-09-24 18:42:00 +00:00
The implications of this are target-specific and not consistent across all
2017-11-07 08:22:27 +00:00
platforms. The frame address may not be available in release mode due to
aggressive optimizations.
< / p >
< p >
This function is only valid within function scope.
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-12-15 01:21:23 +00:00
2019-05-26 20:21:03 +00:00
{#header_open|@hasDecl#}
2019-09-29 21:28:30 +00:00
< pre > {#syntax#}@hasDecl(comptime Container: type, comptime name: []const u8) bool{#endsyntax#}< / pre >
2019-05-26 20:21:03 +00:00
< p >
2022-11-09 08:29:50 +00:00
Returns whether or not a {#link|container|Containers#} has a declaration
2019-05-26 20:21:03 +00:00
matching {#syntax#}name{#endsyntax#}.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_hasDecl_builtin.zig#}
2019-05-26 20:21:03 +00:00
2019-07-02 20:52:55 +00:00
{#see_also|@hasField#}
{#header_close#}
{#header_open|@hasField#}
2019-09-29 21:28:30 +00:00
< pre > {#syntax#}@hasField(comptime Container: type, comptime name: []const u8) bool{#endsyntax#}< / pre >
2019-07-02 20:52:55 +00:00
< p > Returns whether the field name of a struct, union, or enum exists.< / p >
< p >
The result is a compile time constant.
< / p >
< p >
It does not include functions, variables, or constants.
< / p >
{#see_also|@hasDecl#}
2019-05-26 20:21:03 +00:00
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|@import#}
2023-07-21 21:03:26 +00:00
< pre > {#syntax#}@import(comptime path: []const u8) type{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
2019-03-13 17:04:55 +00:00
This function finds a zig file corresponding to {#syntax#}path{#endsyntax#} and adds it to the build,
if it is not already added.
< / p >
< p >
Zig source files are implicitly structs, with a name equal to the file's basename with the extension
truncated. {#syntax#}@import{#endsyntax#} returns the struct type corresponding to the file.
< / p >
< p >
Declarations which have the {#syntax#}pub{#endsyntax#} keyword may be referenced from a different
source file than the one they are declared in.
2017-11-07 08:22:27 +00:00
< / p >
< p >
2020-07-01 19:44:18 +00:00
{#syntax#}path{#endsyntax#} can be a relative path or it can be the name of a package.
2019-03-13 17:04:55 +00:00
If it is a relative path, it is relative to the file that contains the {#syntax#}@import{#endsyntax#}
2017-11-07 08:22:27 +00:00
function call.
< / p >
< p >
The following packages are always available:
< / p >
< ul >
2018-09-14 14:35:03 +00:00
< li > {#syntax#}@import("std"){#endsyntax#} - Zig Standard Library< / li >
2023-04-23 18:06:21 +00:00
< li > {#syntax#}@import("builtin"){#endsyntax#} - Target-specific information
2021-05-17 22:25:27 +00:00
The command < code > zig build-exe --show-builtin< / code > outputs the source to stdout for reference.
2019-03-13 17:04:55 +00:00
< / li >
2023-04-23 18:06:21 +00:00
< li > {#syntax#}@import("root"){#endsyntax#} - Root source file
This is usually < code > src/main.zig< / code > but depends on what file is built.
2022-01-07 18:50:21 +00:00
< / li >
2017-11-07 08:22:27 +00:00
< / ul >
2018-01-17 05:22:33 +00:00
{#see_also|Compile Variables|@embedFile#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-12-15 01:21:23 +00:00
2023-04-14 21:32:31 +00:00
{#header_open|@inComptime#}
< pre > {#syntax#}@inComptime() bool{#endsyntax#}< / pre >
< p >
Returns whether the builtin was run in a {#syntax#}comptime{#endsyntax#} context. The result is a compile-time constant.
< / p >
< p >
This can be used to provide alternative, comptime-friendly implementations of functions. It should not be used, for instance, to exclude certain functions from being evaluated at comptime.
< / p >
{#see_also|comptime#}
{#header_close#}
2018-06-18 07:07:16 +00:00
{#header_open|@intCast#}
2023-06-23 18:37:50 +00:00
< pre > {#syntax#}@intCast(int: anytype) anytype{#endsyntax#}< / pre >
2018-06-18 07:07:16 +00:00
< p >
Converts an integer to another integer while keeping the same numerical value.
2023-06-23 18:37:50 +00:00
The return type is the inferred result type.
2018-06-18 07:07:16 +00:00
Attempting to convert a number which is out of range of the destination type results in
2018-06-18 22:48:29 +00:00
safety-protected {#link|Undefined Behavior#}.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_intCast_builtin.zig#}
2021-11-28 11:28:47 +00:00
< p >
To truncate the significant bits of a number out of range of the destination type, use {#link|@truncate#}.
< / p >
2019-11-29 17:41:07 +00:00
< p >
If {#syntax#}T{#endsyntax#} is {#syntax#}comptime_int{#endsyntax#},
then this is semantically equivalent to {#link|Type Coercion#}.
< / p >
2018-06-18 22:48:29 +00:00
{#header_close#}
2023-06-28 09:22:32 +00:00
{#header_open|@intFromBool#}
< pre > {#syntax#}@intFromBool(value: bool) u1{#endsyntax#}< / pre >
2018-06-19 07:50:38 +00:00
< p >
2023-06-28 09:22:32 +00:00
Converts {#syntax#}true{#endsyntax#} to {#syntax#}@as(u1, 1){#endsyntax#} and {#syntax#}false{#endsyntax#} to
{#syntax#}@as(u1, 0){#endsyntax#}.
2018-06-19 07:50:38 +00:00
< / p >
2023-06-28 09:22:32 +00:00
{#header_close#}
{#header_open|@intFromEnum#}
< pre > {#syntax#}@intFromEnum(enum_or_tagged_union: anytype) anytype{#endsyntax#}< / pre >
2018-06-19 07:50:38 +00:00
< p >
2023-06-28 09:22:32 +00:00
Converts an enumeration value into its integer tag type. When a tagged union is passed,
the tag value is used as the enumeration value.
2018-06-19 07:50:38 +00:00
< / p >
2023-06-28 09:22:32 +00:00
< p >
If there is only one possible enum value, the result is a {#syntax#}comptime_int{#endsyntax#}
known at {#link|comptime#}.
< / p >
{#see_also|@enumFromInt#}
2018-06-19 07:50:38 +00:00
{#header_close#}
2023-06-28 09:22:32 +00:00
{#header_open|@intFromError#}
2023-10-21 09:56:05 +00:00
< pre > {#syntax#}@intFromError(err: anytype) std.meta.Int(.unsigned, @bitSizeOf(anyerror)){#endsyntax#}< / pre >
2018-06-18 22:48:29 +00:00
< p >
2023-06-28 09:22:32 +00:00
Supports the following types:
2018-06-18 22:48:29 +00:00
< / p >
2023-06-28 09:22:32 +00:00
< ul >
< li > {#link|The Global Error Set#}< / li >
< li > {#link|Error Set Type#}< / li >
< li > {#link|Error Union Type#}< / li >
< / ul >
2018-06-18 22:48:29 +00:00
< p >
2023-06-28 09:22:32 +00:00
Converts an error to the integer representation of an error.
2018-06-18 22:48:29 +00:00
< / p >
< p >
2023-06-28 09:22:32 +00:00
It is generally recommended to avoid this
cast, as the integer representation of an error is not stable across source code changes.
2018-06-18 07:07:16 +00:00
< / p >
2023-06-28 09:22:32 +00:00
{#see_also|@errorFromInt#}
2018-06-18 07:07:16 +00:00
{#header_close#}
2023-06-28 09:22:32 +00:00
{#header_open|@intFromFloat#}
< pre > {#syntax#}@intFromFloat(float: anytype) anytype{#endsyntax#}< / pre >
2018-06-18 07:07:16 +00:00
< p >
2023-06-28 09:22:32 +00:00
Converts the integer part of a floating point number to the inferred result type.
2018-06-18 07:07:16 +00:00
< / p >
2019-03-25 16:55:45 +00:00
< p >
2023-06-28 09:22:32 +00:00
If the integer part of the floating point number cannot fit in the destination type,
it invokes safety-checked {#link|Undefined Behavior#}.
2019-03-25 16:55:45 +00:00
< / p >
2023-06-28 09:22:32 +00:00
{#see_also|@floatFromInt#}
{#header_close#}
{#header_open|@intFromPtr#}
< pre > {#syntax#}@intFromPtr(value: anytype) usize{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
2023-06-28 09:22:32 +00:00
Converts {#syntax#}value{#endsyntax#} to a {#syntax#}usize{#endsyntax#} which is the address of the pointer.
{#syntax#}value{#endsyntax#} can be {#syntax#}*T{#endsyntax#} or {#syntax#}?*T{#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / p >
2023-06-28 09:22:32 +00:00
< p > To convert the other way, use {#link|@ptrFromInt#}< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-06-18 07:07:16 +00:00
2022-10-03 16:34:30 +00:00
{#header_open|@max#}
2024-10-03 11:37:36 +00:00
< pre > {#syntax#}@max(...) T{#endsyntax#}< / pre >
2021-07-25 03:34:11 +00:00
< p >
2024-10-03 11:37:36 +00:00
Takes two or more arguments and returns the biggest value included (the maximum). This builtin accepts integers, floats, and vectors of either. In the latter case, the operation is performed element wise.
2021-07-25 03:34:11 +00:00
< / p >
< p >
2024-10-03 11:37:36 +00:00
NaNs are handled as follows: return the biggest non-NaN value included. If all operands are NaN, return NaN.
2021-07-25 03:34:11 +00:00
< / p >
2022-10-03 16:34:30 +00:00
{#see_also|@min|Vectors#}
2021-07-25 03:34:11 +00:00
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|@memcpy#}
2023-04-14 04:44:40 +00:00
< pre > {#syntax#}@memcpy(noalias dest, noalias source) void{#endsyntax#}< / pre >
< p > This function copies bytes from one region of memory to another.< / p >
2023-04-22 01:03:33 +00:00
< p > {#syntax#}dest{#endsyntax#} must be a mutable slice, a mutable pointer to an array, or
2023-04-24 19:56:24 +00:00
a mutable many-item {#link|pointer|Pointers#}. It may have any
2023-04-22 01:03:33 +00:00
alignment, and it may have any element type.< / p >
2023-05-13 22:02:41 +00:00
< p > {#syntax#}source{#endsyntax#} must be a slice, a pointer to
an array, or a many-item {#link|pointer|Pointers#}. It may
have any alignment, and it may have any element type.< / p >
2023-04-22 01:03:33 +00:00
< p > The {#syntax#}source{#endsyntax#} element type must support {#link|Type Coercion#}
into the {#syntax#}dest{#endsyntax#} element type. The element types may have
different ABI size, however, that may incur a performance penalty.< / p >
< p > Similar to {#link|for#} loops, at least one of {#syntax#}source{#endsyntax#} and
{#syntax#}dest{#endsyntax#} must provide a length, and if two lengths are provided,
they must be equal.< / p >
< p > Finally, the two memory regions must not overlap.< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-12-15 01:21:23 +00:00
2018-01-17 04:19:05 +00:00
{#header_open|@memset#}
2023-04-14 04:44:40 +00:00
< pre > {#syntax#}@memset(dest, elem) void{#endsyntax#}< / pre >
< p > This function sets all the elements of a memory region to {#syntax#}elem{#endsyntax#}.< / p >
< p > {#syntax#}dest{#endsyntax#} must be a mutable slice or a mutable pointer to an array.
It may have any alignment, and it may have any element type.< / p >
< p > {#syntax#}elem{#endsyntax#} is coerced to the element type of {#syntax#}dest{#endsyntax#}.< / p >
2023-07-17 21:16:41 +00:00
< p > For securely zeroing out sensitive contents from memory, you should use
2024-08-09 21:04:02 +00:00
{#syntax#}std.crypto.secureZero{#endsyntax#}< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-12-15 01:21:23 +00:00
2022-10-03 16:34:30 +00:00
{#header_open|@min#}
2024-10-03 11:37:36 +00:00
< pre > {#syntax#}@min(...) T{#endsyntax#}< / pre >
2021-07-25 03:34:11 +00:00
< p >
2024-10-03 11:37:36 +00:00
Takes two or more arguments and returns the smallest value included (the minimum). This builtin accepts integers, floats, and vectors of either. In the latter case, the operation is performed element wise.
2021-07-25 03:34:11 +00:00
< / p >
< p >
2024-10-03 11:37:36 +00:00
NaNs are handled as follows: return the smallest non-NaN value included. If all operands are NaN, return NaN.
2021-07-25 03:34:11 +00:00
< / p >
2022-10-03 16:34:30 +00:00
{#see_also|@max|Vectors#}
2021-07-25 03:34:11 +00:00
{#header_close#}
2020-06-05 07:27:52 +00:00
{#header_open|@wasmMemorySize#}
2024-05-20 11:38:59 +00:00
< pre > {#syntax#}@wasmMemorySize(index: u32) usize{#endsyntax#}< / pre >
2020-06-05 07:27:52 +00:00
< p >
This function returns the size of the Wasm memory identified by {#syntax#}index{#endsyntax#} as
an unsigned value in units of Wasm pages. Note that each Wasm page is 64KB in size.
< / p >
< p >
This function is a low level intrinsic with no safety mechanisms usually useful for allocator
designers targeting Wasm. So unless you are writing a new allocator from scratch, you should use
2020-06-05 08:16:41 +00:00
something like {#syntax#}@import("std").heap.WasmPageAllocator{#endsyntax#}.
2020-06-05 07:27:52 +00:00
< / p >
{#see_also|@wasmMemoryGrow#}
{#header_close#}
{#header_open|@wasmMemoryGrow#}
2024-05-20 11:38:59 +00:00
< pre > {#syntax#}@wasmMemoryGrow(index: u32, delta: usize) isize{#endsyntax#}< / pre >
2020-06-05 07:27:52 +00:00
< p >
This function increases the size of the Wasm memory identified by {#syntax#}index{#endsyntax#} by
{#syntax#}delta{#endsyntax#} in units of unsigned number of Wasm pages. Note that each Wasm page
is 64KB in size. On success, returns previous memory size; on failure, if the allocation fails,
returns -1.
< / p >
< p >
This function is a low level intrinsic with no safety mechanisms usually useful for allocator
designers targeting Wasm. So unless you are writing a new allocator from scratch, you should use
2020-06-05 08:16:41 +00:00
something like {#syntax#}@import("std").heap.WasmPageAllocator{#endsyntax#}.
2020-06-05 07:27:52 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_wasmMemoryGrow_builtin.zig#}
2020-06-05 07:27:52 +00:00
{#see_also|@wasmMemorySize#}
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|@mod#}
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}@mod(numerator: T, denominator: T) T{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
Modulus division. For unsigned integers this is the same as
2022-02-04 19:27:50 +00:00
{#syntax#}numerator % denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator > 0{#endsyntax#}, otherwise the
operation will result in a {#link|Remainder Division by Zero#} when runtime safety checks are enabled.
2017-11-07 08:22:27 +00:00
< / p >
< ul >
2018-09-14 14:35:03 +00:00
< li > {#syntax#}@mod(-5, 3) == 1{#endsyntax#}< / li >
2020-10-19 02:43:34 +00:00
< li > {#syntax#}(@divFloor(a, b) * b) + @mod(a, b) == a{#endsyntax#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
2018-09-14 14:35:03 +00:00
< p > For a function that returns an error code, see {#syntax#}@import("std").math.mod{#endsyntax#}.< / p >
2018-01-17 05:22:33 +00:00
{#see_also|@rem#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-12-15 01:21:23 +00:00
2018-01-17 04:19:05 +00:00
{#header_open|@mulWithOverflow#}
2022-12-21 14:40:30 +00:00
< pre > {#syntax#}@mulWithOverflow(a: anytype, b: anytype) struct { @TypeOf(a, b), u1 }{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
2022-12-21 14:40:30 +00:00
Performs {#syntax#}a * b{#endsyntax#} and returns a tuple with the result and a possible overflow bit.
2017-11-07 08:22:27 +00:00
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
2022-02-04 19:27:50 +00:00
2018-01-17 04:19:05 +00:00
{#header_open|@panic#}
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}@panic(message: []const u8) noreturn{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
Invokes the panic handler function. By default the panic handler function
2018-09-14 14:35:03 +00:00
calls the public {#syntax#}panic{#endsyntax#} function exposed in the root source file, or
2020-04-12 09:41:00 +00:00
if there is not one specified, the {#syntax#}std.builtin.default_panic{#endsyntax#}
function from {#syntax#}std/builtin.zig{#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / p >
2018-09-14 14:35:03 +00:00
< p > Generally it is better to use {#syntax#}@import("std").debug.panic{#endsyntax#}.
However, {#syntax#}@panic{#endsyntax#} can be useful for 2 scenarios:
2017-11-07 08:22:27 +00:00
< / p >
< ul >
< li > From library code, calling the programmer's panic function if they exposed one in the root source file.< / li >
< li > When mixing C and Zig code, calling the canonical panic implementation across multiple .o files.< / li >
< / ul >
2018-01-17 05:22:33 +00:00
{#see_also|Root Source File#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-12-15 01:21:23 +00:00
2018-07-07 04:25:32 +00:00
{#header_open|@popCount#}
2023-03-03 15:16:08 +00:00
< pre > {#syntax#}@popCount(operand: anytype) anytype{#endsyntax#}< / pre >
2022-08-21 14:24:04 +00:00
< p > {#syntax#}@TypeOf(operand){#endsyntax#} must be an integer type.< / p >
2021-07-26 00:35:55 +00:00
< p > {#syntax#}operand{#endsyntax#} may be an {#link|integer|Integers#} or {#link|vector|Vectors#}.< / p >
2023-04-23 18:06:21 +00:00
< p >
Counts the number of bits set in an integer - "population count".
< / p >
2018-07-07 04:25:32 +00:00
< p >
2021-07-26 00:35:55 +00:00
If {#syntax#}operand{#endsyntax#} is a {#link|comptime#}-known integer,
2019-05-16 20:32:24 +00:00
the return type is {#syntax#}comptime_int{#endsyntax#}.
2021-07-26 00:35:55 +00:00
Otherwise, the return type is an unsigned integer or vector of unsigned integers with the minimum number
2018-07-07 04:25:32 +00:00
of bits that can represent the bit count of the integer type.
< / p >
{#see_also|@ctz|@clz#}
{#header_close#}
2018-12-15 01:21:23 +00:00
2021-12-10 22:06:08 +00:00
{#header_open|@prefetch#}
2024-01-22 03:39:50 +00:00
< pre > {#syntax#}@prefetch(ptr: anytype, comptime options: PrefetchOptions) void{#endsyntax#}< / pre >
2021-12-10 22:06:08 +00:00
< p >
This builtin tells the compiler to emit a prefetch instruction if supported by the
target CPU. If the target CPU does not support the requested prefetch instruction,
2022-09-24 18:42:00 +00:00
this builtin is a no-op. This function has no effect on the behavior of the program,
2021-12-10 22:06:08 +00:00
only on the performance characteristics.
< / p >
< p >
The {#syntax#}ptr{#endsyntax#} argument may be any pointer type and determines the memory
address to prefetch. This function does not dereference the pointer, it is perfectly legal
to pass a pointer to invalid memory to this function and no illegal behavior will result.
< / p >
2024-01-22 03:39:50 +00:00
< p > {#syntax#}PrefetchOptions{#endsyntax#} can be found with {#syntax#}@import("std").builtin.PrefetchOptions{#endsyntax#}.< / p >
2021-12-10 22:06:08 +00:00
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|@ptrCast#}
2023-06-23 18:37:50 +00:00
< pre > {#syntax#}@ptrCast(value: anytype) anytype{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
2023-06-23 18:37:50 +00:00
Converts a pointer of one type to a pointer of another type. The return type is the inferred result type.
2017-11-07 08:22:27 +00:00
< / p >
2019-02-14 23:59:20 +00:00
< p >
{#link|Optional Pointers#} are allowed. Casting an optional pointer which is {#link|null#}
to a non-optional pointer invokes safety-checked {#link|Undefined Behavior#}.
< / p >
2023-01-27 14:03:51 +00:00
< p >
{#syntax#}@ptrCast{#endsyntax#} cannot be used for:
< / p >
< ul >
2023-02-13 14:19:17 +00:00
< li > Removing {#syntax#}const{#endsyntax#} qualifier, use {#link|@constCast#}.< / li >
< li > Removing {#syntax#}volatile{#endsyntax#} qualifier, use {#link|@volatileCast#}.< / li >
2023-01-27 18:25:48 +00:00
< li > Changing pointer address space, use {#link|@addrSpaceCast#}.< / li >
< li > Increasing pointer alignment, use {#link|@alignCast#}.< / li >
< li > Casting a non-slice pointer to a slice, use slicing syntax {#syntax#}ptr[start..end]{#endsyntax#}.< / li >
2023-01-27 14:03:51 +00:00
< / ul >
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-12-15 01:21:23 +00:00
2023-06-28 09:22:32 +00:00
{#header_open|@ptrFromInt#}
< pre > {#syntax#}@ptrFromInt(address: usize) anytype{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
2023-06-28 09:22:32 +00:00
Converts an integer to a {#link|pointer|Pointers#}. The return type is the inferred result type.
To convert the other way, use {#link|@intFromPtr#}. Casting an address of 0 to a destination type
which in not {#link|optional|Optional Pointers#} and does not have the {#syntax#}allowzero{#endsyntax#} attribute will result in a
{#link|Pointer Cast Invalid Null#} panic when runtime safety checks are enabled.
< / p >
< p >
If the destination pointer type does not allow address zero and {#syntax#}address{#endsyntax#}
is zero, this invokes safety-checked {#link|Undefined Behavior#}.
2017-11-07 08:22:27 +00:00
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-12-15 01:21:23 +00:00
2018-01-17 04:19:05 +00:00
{#header_open|@rem#}
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}@rem(numerator: T, denominator: T) T{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
Remainder division. For unsigned integers this is the same as
2022-02-04 19:27:50 +00:00
{#syntax#}numerator % denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator > 0{#endsyntax#}, otherwise the
operation will result in a {#link|Remainder Division by Zero#} when runtime safety checks are enabled.
2017-11-07 08:22:27 +00:00
< / p >
< ul >
2018-09-14 14:35:03 +00:00
< li > {#syntax#}@rem(-5, 3) == -2{#endsyntax#}< / li >
2020-10-19 02:43:34 +00:00
< li > {#syntax#}(@divTrunc(a, b) * b) + @rem(a, b) == a{#endsyntax#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
2018-09-14 14:35:03 +00:00
< p > For a function that returns an error code, see {#syntax#}@import("std").math.rem{#endsyntax#}.< / p >
2018-01-17 05:22:33 +00:00
{#see_also|@mod#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-12-15 01:21:23 +00:00
2018-01-17 04:19:05 +00:00
{#header_open|@returnAddress#}
2019-03-02 20:34:58 +00:00
< pre > {#syntax#}@returnAddress() usize{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
2019-03-02 20:34:58 +00:00
This function returns the address of the next machine code instruction that will be executed
when the current function returns.
2017-11-07 08:22:27 +00:00
< / p >
< p >
2022-09-24 18:42:00 +00:00
The implications of this are target-specific and not consistent across
2017-11-07 08:22:27 +00:00
all platforms.
< / p >
< p >
2019-03-02 20:34:58 +00:00
This function is only valid within function scope. If the function gets inlined into
a calling function, the returned address will apply to the calling function.
2017-11-07 08:22:27 +00:00
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
Add @select
@select(
comptime T: type,
pred: std.meta.Vector(len, bool),
a: std.meta.Vector(len, T),
b: std.meta.Vector(len, T)
) std.meta.Vector(len, T)
Constructs a vector from a & b, based on the values in the predicate vector. For indices where the predicate value is true, the corresponding
element from the a vector is selected, and otherwise from b.
2021-07-26 00:37:11 +00:00
{#header_open|@select#}
2022-03-14 05:21:16 +00:00
< pre > {#syntax#}@select(comptime T: type, pred: @Vector(len, bool), a: @Vector(len, T), b: @Vector(len, T)) @Vector(len, T){#endsyntax#}< / pre >
Add @select
@select(
comptime T: type,
pred: std.meta.Vector(len, bool),
a: std.meta.Vector(len, T),
b: std.meta.Vector(len, T)
) std.meta.Vector(len, T)
Constructs a vector from a & b, based on the values in the predicate vector. For indices where the predicate value is true, the corresponding
element from the a vector is selected, and otherwise from b.
2021-07-26 00:37:11 +00:00
< p >
Selects values element-wise from {#syntax#}a{#endsyntax#} or {#syntax#}b{#endsyntax#} based on {#syntax#}pred{#endsyntax#}. If {#syntax#}pred[i]{#endsyntax#} is {#syntax#}true{#endsyntax#}, the corresponding element in the result will be {#syntax#}a[i]{#endsyntax#} and otherwise {#syntax#}b[i]{#endsyntax#}.
< / p >
2021-12-19 04:40:57 +00:00
{#see_also|Vectors#}
Add @select
@select(
comptime T: type,
pred: std.meta.Vector(len, bool),
a: std.meta.Vector(len, T),
b: std.meta.Vector(len, T)
) std.meta.Vector(len, T)
Constructs a vector from a & b, based on the values in the predicate vector. For indices where the predicate value is true, the corresponding
element from the a vector is selected, and otherwise from b.
2021-07-26 00:37:11 +00:00
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|@setEvalBranchQuota#}
2023-03-03 15:16:08 +00:00
< pre > {#syntax#}@setEvalBranchQuota(comptime new_quota: u32) void{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
2023-09-21 14:50:48 +00:00
Increase the maximum number of backwards branches that compile-time code
2017-11-07 08:22:27 +00:00
execution can use before giving up and making a compile error.
< / p >
< p >
2018-09-14 14:35:03 +00:00
If the {#syntax#}new_quota{#endsyntax#} is smaller than the default quota ({#syntax#}1000{#endsyntax#}) or
2017-11-07 08:22:27 +00:00
a previously explicitly set quota, it is ignored.
< / p >
< p >
Example:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_without_setEvalBranchQuota_builtin.zig#}
2018-09-14 14:35:03 +00:00
< p > Now we use {#syntax#}@setEvalBranchQuota{#endsyntax#}:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_setEvalBranchQuota_builtin.zig#}
2017-11-07 08:22:27 +00:00
2018-01-17 05:22:33 +00:00
{#see_also|comptime#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-12-15 01:21:23 +00:00
2018-01-17 04:19:05 +00:00
{#header_open|@setFloatMode#}
2024-01-22 03:39:50 +00:00
< pre > {#syntax#}@setFloatMode(comptime mode: FloatMode) void{#endsyntax#}< / pre >
< p > Changes the current scope's rules about how floating point operations are defined.< / p >
2017-11-07 08:22:27 +00:00
< ul >
2018-09-13 19:46:34 +00:00
< li >
2018-09-14 14:35:03 +00:00
{#syntax#}Strict{#endsyntax#} (default) - Floating point operations follow strict IEEE compliance.
2018-09-13 19:46:34 +00:00
< / li >
2017-11-07 08:22:27 +00:00
< li >
2018-09-14 14:35:03 +00:00
{#syntax#}Optimized{#endsyntax#} - Floating point operations may do all of the following:
2017-11-07 08:22:27 +00:00
< ul >
< li > Assume the arguments and result are not NaN. Optimizations are required to retain defined behavior over NaNs, but the value of the result is undefined.< / li >
< li > Assume the arguments and result are not +/-Inf. Optimizations are required to retain defined behavior over +/-Inf, but the value of the result is undefined.< / li >
< li > Treat the sign of a zero argument or result as insignificant.< / li >
< li > Use the reciprocal of an argument rather than perform division.< / li >
2022-10-03 17:52:39 +00:00
< li > Perform floating-point contraction (e.g. fusing a multiply followed by an addition into a fused multiply-add).< / li >
2017-11-07 08:22:27 +00:00
< li > Perform algebraically equivalent transformations that may change results in floating point (e.g. reassociate).< / li >
< / ul >
This is equivalent to < code > -ffast-math< / code > in GCC.
< / li >
< / ul >
2018-09-13 19:46:34 +00:00
< p >
The floating point mode is inherited by child scopes, and can be overridden in any scope.
You can set the floating point mode in a struct or module scope by using a comptime block.
< / p >
2024-01-22 03:39:50 +00:00
< p > {#syntax#}FloatMode{#endsyntax#} can be found with {#syntax#}@import("std").builtin.FloatMode{#endsyntax#}.< / p >
2018-01-17 05:22:33 +00:00
{#see_also|Floating Point Operations#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-12-15 01:21:23 +00:00
{#header_open|@setRuntimeSafety#}
2022-02-04 19:27:50 +00:00
< pre > {#syntax#}@setRuntimeSafety(comptime safety_on: bool) void{#endsyntax#}< / pre >
2018-12-15 01:21:23 +00:00
< p >
2019-03-13 18:46:53 +00:00
Sets whether runtime safety checks are enabled for the scope that contains the function call.
2018-12-15 01:21:23 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_setRuntimeSafety_builtin.zig#}
2019-05-01 19:09:03 +00:00
< p > Note: it is < a href = "https://github.com/ziglang/zig/issues/978" > planned< / a > to replace
{#syntax#}@setRuntimeSafety{#endsyntax#} with < code > @optimizeFor< / code > < / p >
2018-12-15 01:21:23 +00:00
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|@shlExact#}
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}@shlExact(value: T, shift_amt: Log2T) T{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
2021-09-29 01:55:43 +00:00
Performs the left shift operation ({#syntax#}< < {#endsyntax#}).
For unsigned integers, the result is {#link|undefined#} if any 1 bits
are shifted out. For signed integers, the result is {#link|undefined#} if
any bits that disagree with the resultant sign bit are shifted out.
2017-11-07 08:22:27 +00:00
< / p >
< p >
2024-08-28 01:35:53 +00:00
The type of {#syntax#}shift_amt{#endsyntax#} is an unsigned integer with {#syntax#}log2(@typeInfo(T).int.bits){#endsyntax#} bits.
This is because {#syntax#}shift_amt >= @typeInfo(T).int.bits{#endsyntax#} is undefined behavior.
2017-11-07 08:22:27 +00:00
< / p >
2024-01-04 07:44:44 +00:00
< p >
{#syntax#}comptime_int{#endsyntax#} is modeled as an integer with an infinite number of bits,
meaning that in such case, {#syntax#}@shlExact{#endsyntax#} always produces a result and
cannot produce a compile error.
< / p >
2021-09-29 01:55:43 +00:00
{#see_also|@shrExact|@shlWithOverflow#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-12-15 01:21:23 +00:00
2018-01-17 04:19:05 +00:00
{#header_open|@shlWithOverflow#}
2022-12-21 14:40:30 +00:00
< pre > {#syntax#}@shlWithOverflow(a: anytype, shift_amt: Log2T) struct { @TypeOf(a), u1 }{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
2022-12-21 14:40:30 +00:00
Performs {#syntax#}a < < b { # endsyntax # } and returns a tuple with the result and a possible overflow bit .
2017-11-07 08:22:27 +00:00
< / p >
< p >
2024-08-28 01:35:53 +00:00
The type of {#syntax#}shift_amt{#endsyntax#} is an unsigned integer with {#syntax#}log2(@typeInfo(@TypeOf(a)).int.bits){#endsyntax#} bits.
This is because {#syntax#}shift_amt >= @typeInfo(@TypeOf(a)).int.bits{#endsyntax#} is undefined behavior.
2017-11-07 08:22:27 +00:00
< / p >
2021-09-29 01:55:43 +00:00
{#see_also|@shlExact|@shrExact#}
2021-09-01 18:17:45 +00:00
{#header_close#}
2022-02-04 19:27:50 +00:00
2018-01-17 04:19:05 +00:00
{#header_open|@shrExact#}
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}@shrExact(value: T, shift_amt: Log2T) T{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
2018-09-14 14:35:03 +00:00
Performs the right shift operation ({#syntax#}>>{#endsyntax#}). Caller guarantees
2017-11-07 08:22:27 +00:00
that the shift will not shift any 1 bits out.
< / p >
< p >
2024-08-28 01:35:53 +00:00
The type of {#syntax#}shift_amt{#endsyntax#} is an unsigned integer with {#syntax#}log2(@typeInfo(T).int.bits){#endsyntax#} bits.
This is because {#syntax#}shift_amt >= @typeInfo(T).int.bits{#endsyntax#} is undefined behavior.
2017-11-07 08:22:27 +00:00
< / p >
2021-09-29 01:55:43 +00:00
{#see_also|@shlExact|@shlWithOverflow#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-06-18 21:25:29 +00:00
2019-09-18 19:41:56 +00:00
{#header_open|@shuffle#}
2022-03-14 05:21:16 +00:00
< pre > {#syntax#}@shuffle(comptime E: type, a: @Vector(a_len, E), b: @Vector(b_len, E), comptime mask: @Vector(mask_len, i32)) @Vector(mask_len, E){#endsyntax#}< / pre >
2019-09-18 19:41:56 +00:00
< p >
Constructs a new {#link|vector|Vectors#} by selecting elements from {#syntax#}a{#endsyntax#} and
{#syntax#}b{#endsyntax#} based on {#syntax#}mask{#endsyntax#}.
< / p >
< p >
Each element in {#syntax#}mask{#endsyntax#} selects an element from either {#syntax#}a{#endsyntax#} or
{#syntax#}b{#endsyntax#}. Positive numbers select from {#syntax#}a{#endsyntax#} starting at 0.
Negative values select from {#syntax#}b{#endsyntax#}, starting at {#syntax#}-1{#endsyntax#} and going down.
2022-09-24 18:42:00 +00:00
It is recommended to use the {#syntax#}~{#endsyntax#} operator for indexes from {#syntax#}b{#endsyntax#}
2019-12-03 10:56:50 +00:00
so that both indexes can start from {#syntax#}0{#endsyntax#} (i.e. {#syntax#}~@as(i32, 0){#endsyntax#} is
2019-09-18 19:41:56 +00:00
{#syntax#}-1{#endsyntax#}).
< / p >
< p >
For each element of {#syntax#}mask{#endsyntax#}, if it or the selected value from
{#syntax#}a{#endsyntax#} or {#syntax#}b{#endsyntax#} is {#syntax#}undefined{#endsyntax#},
then the resulting element is {#syntax#}undefined{#endsyntax#}.
< / p >
< p >
{#syntax#}a_len{#endsyntax#} and {#syntax#}b_len{#endsyntax#} may differ in length. Out-of-bounds element
indexes in {#syntax#}mask{#endsyntax#} result in compile errors.
< / p >
< p >
If {#syntax#}a{#endsyntax#} or {#syntax#}b{#endsyntax#} is {#syntax#}undefined{#endsyntax#}, it
is equivalent to a vector of all {#syntax#}undefined{#endsyntax#} with the same length as the other vector.
If both vectors are {#syntax#}undefined{#endsyntax#}, {#syntax#}@shuffle{#endsyntax#} returns
a vector with all elements {#syntax#}undefined{#endsyntax#}.
< / p >
< p >
{#syntax#}E{#endsyntax#} must be an {#link|integer|Integers#}, {#link|float|Floats#},
{#link|pointer|Pointers#}, or {#syntax#}bool{#endsyntax#}. The mask may be any vector length, and its
length determines the result length.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_shuffle_builtin.zig#}
2021-12-19 04:40:57 +00:00
{#see_also|Vectors#}
2019-09-18 19:41:56 +00:00
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|@sizeOf#}
2018-09-14 14:35:03 +00:00
< pre > {#syntax#}@sizeOf(comptime T: type) comptime_int{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
2018-09-14 14:35:03 +00:00
This function returns the number of bytes it takes to store {#syntax#}T{#endsyntax#} in memory.
2019-02-15 23:05:50 +00:00
The result is a target-specific compile time constant.
2017-11-07 08:22:27 +00:00
< / p >
< p >
2023-09-21 14:50:48 +00:00
This size may contain padding bytes. If there were two consecutive T in memory, the padding would be the offset
2019-02-15 23:05:50 +00:00
in bytes between element at index 0 and the element at index 1. For {#link|integer|Integers#},
consider whether you want to use {#syntax#}@sizeOf(T){#endsyntax#} or
2024-08-28 01:35:53 +00:00
{#syntax#}@typeInfo(T).int.bits{#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / p >
2019-04-25 02:31:53 +00:00
< p >
This function measures the size at runtime. For types that are disallowed at runtime, such as
{#syntax#}comptime_int{#endsyntax#} and {#syntax#}type{#endsyntax#}, the result is {#syntax#}0{#endsyntax#}.
< / p >
2020-01-16 15:12:43 +00:00
{#see_also|@bitSizeOf|@typeInfo#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-06-18 21:25:29 +00:00
2019-09-19 15:14:42 +00:00
{#header_open|@splat#}
2023-07-07 19:52:43 +00:00
< pre > {#syntax#}@splat(scalar: anytype) anytype{#endsyntax#}< / pre >
2019-09-19 15:14:42 +00:00
< p >
2023-09-21 14:50:48 +00:00
Produces a vector where each element is the value {#syntax#}scalar{#endsyntax#}.
2023-07-07 19:52:43 +00:00
The return type and thus the length of the vector is inferred.
2019-09-19 15:14:42 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_splat_builtin.zig#}
2019-09-19 15:14:42 +00:00
< p >
{#syntax#}scalar{#endsyntax#} must be an {#link|integer|Integers#}, {#link|bool|Primitive Types#},
{#link|float|Floats#}, or {#link|pointer|Pointers#}.
< / p >
{#see_also|Vectors|@shuffle#}
{#header_close#}
2020-11-01 18:51:59 +00:00
{#header_open|@reduce#}
2022-03-18 00:22:57 +00:00
< pre > {#syntax#}@reduce(comptime op: std.builtin.ReduceOp, value: anytype) E{#endsyntax#}< / pre >
2020-11-01 18:51:59 +00:00
< p >
2022-03-18 00:22:57 +00:00
Transforms a {#link|vector|Vectors#} into a scalar value (of type < code > E< / code > )
by performing a sequential horizontal reduction of its elements using the
specified operator {#syntax#}op{#endsyntax#}.
2020-11-01 18:51:59 +00:00
< / p >
< p >
Not every operator is available for every vector element type:
2020-11-01 21:39:45 +00:00
< / p >
2020-11-01 18:51:59 +00:00
< ul >
2022-03-18 00:22:57 +00:00
< li > Every operator is available for {#link|integer|Integers#} vectors.< / li >
2020-11-01 18:51:59 +00:00
< li > {#syntax#}.And{#endsyntax#}, {#syntax#}.Or{#endsyntax#},
2022-03-18 00:22:57 +00:00
{#syntax#}.Xor{#endsyntax#} are additionally available for
2020-11-01 18:51:59 +00:00
{#syntax#}bool{#endsyntax#} vectors,< / li >
< li > {#syntax#}.Min{#endsyntax#}, {#syntax#}.Max{#endsyntax#},
{#syntax#}.Add{#endsyntax#}, {#syntax#}.Mul{#endsyntax#} are
2022-03-18 00:22:57 +00:00
additionally available for {#link|floating point|Floats#} vectors,< / li >
2020-11-01 18:51:59 +00:00
< / ul >
< p >
Note that {#syntax#}.Add{#endsyntax#} and {#syntax#}.Mul{#endsyntax#}
reductions on integral types are wrapping; when applied on floating point
types the operation associativity is preserved, unless the float mode is
set to {#syntax#}Optimized{#endsyntax#}.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_reduce_builtin.zig#}
2020-11-01 18:51:59 +00:00
{#see_also|Vectors|@setFloatMode#}
{#header_close#}
2020-07-01 01:52:02 +00:00
{#header_open|@src#}
< pre > {#syntax#}@src() std.builtin.SourceLocation{#endsyntax#}< / pre >
< p >
Returns a {#syntax#}SourceLocation{#endsyntax#} struct representing the function's name and location in the source code. This must be called in a function.
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_src_builtin.zig#}
2019-09-19 15:14:42 +00:00
2020-07-01 01:52:02 +00:00
{#header_close#}
2018-04-15 17:21:52 +00:00
{#header_open|@sqrt#}
2020-07-11 11:08:20 +00:00
< pre > {#syntax#}@sqrt(value: anytype) @TypeOf(value){#endsyntax#}< / pre >
2018-04-15 17:21:52 +00:00
< p >
Performs the square root of a floating point number. Uses a dedicated hardware instruction
2020-01-02 04:27:43 +00:00
when available.
< / p >
< p >
2023-02-02 14:11:13 +00:00
Supports {#link|Floats#} and {#link|Vectors#} of floats.
2018-04-15 17:21:52 +00:00
< / p >
2019-06-21 21:18:59 +00:00
{#header_close#}
{#header_open|@sin#}
2020-07-11 11:08:20 +00:00
< pre > {#syntax#}@sin(value: anytype) @TypeOf(value){#endsyntax#}< / pre >
2019-06-21 21:18:59 +00:00
< p >
2023-06-13 20:37:26 +00:00
Sine trigonometric function on a floating point number in radians. Uses a dedicated hardware instruction
2020-01-02 04:27:43 +00:00
when available.
< / p >
< p >
2023-02-02 14:11:13 +00:00
Supports {#link|Floats#} and {#link|Vectors#} of floats.
2018-04-15 17:21:52 +00:00
< / p >
2019-06-21 21:18:59 +00:00
{#header_close#}
2022-04-27 23:45:23 +00:00
2019-06-21 21:18:59 +00:00
{#header_open|@cos#}
2020-07-11 11:08:20 +00:00
< pre > {#syntax#}@cos(value: anytype) @TypeOf(value){#endsyntax#}< / pre >
2019-06-21 21:18:59 +00:00
< p >
2023-06-13 20:37:26 +00:00
Cosine trigonometric function on a floating point number in radians. Uses a dedicated hardware instruction
2020-01-02 04:27:43 +00:00
when available.
< / p >
< p >
2023-02-02 14:11:13 +00:00
Supports {#link|Floats#} and {#link|Vectors#} of floats.
2019-06-21 21:18:59 +00:00
< / p >
{#header_close#}
2022-04-27 23:45:23 +00:00
{#header_open|@tan#}
< pre > {#syntax#}@tan(value: anytype) @TypeOf(value){#endsyntax#}< / pre >
< p >
2023-06-13 20:37:26 +00:00
Tangent trigonometric function on a floating point number in radians.
2022-04-27 23:45:23 +00:00
Uses a dedicated hardware instruction when available.
< / p >
< p >
2023-02-02 14:11:13 +00:00
Supports {#link|Floats#} and {#link|Vectors#} of floats.
2022-04-27 23:45:23 +00:00
< / p >
{#header_close#}
2019-06-21 21:18:59 +00:00
{#header_open|@exp#}
2020-07-11 11:08:20 +00:00
< pre > {#syntax#}@exp(value: anytype) @TypeOf(value){#endsyntax#}< / pre >
2019-06-21 21:18:59 +00:00
< p >
Base-e exponential function on a floating point number. Uses a dedicated hardware instruction
2020-01-02 04:27:43 +00:00
when available.
< / p >
< p >
2023-02-02 14:11:13 +00:00
Supports {#link|Floats#} and {#link|Vectors#} of floats.
2019-06-21 21:18:59 +00:00
< / p >
{#header_close#}
{#header_open|@exp2#}
2020-07-11 11:08:20 +00:00
< pre > {#syntax#}@exp2(value: anytype) @TypeOf(value){#endsyntax#}< / pre >
2019-06-21 21:18:59 +00:00
< p >
Base-2 exponential function on a floating point number. Uses a dedicated hardware instruction
2020-01-02 04:27:43 +00:00
when available.
< / p >
< p >
2023-02-02 14:11:13 +00:00
Supports {#link|Floats#} and {#link|Vectors#} of floats.
2019-06-21 21:18:59 +00:00
< / p >
{#header_close#}
2020-01-02 19:01:48 +00:00
{#header_open|@log#}
2020-07-11 11:08:20 +00:00
< pre > {#syntax#}@log(value: anytype) @TypeOf(value){#endsyntax#}< / pre >
2019-06-21 21:18:59 +00:00
< p >
Returns the natural logarithm of a floating point number. Uses a dedicated hardware instruction
2020-01-02 04:27:43 +00:00
when available.
< / p >
< p >
2023-02-02 14:11:13 +00:00
Supports {#link|Floats#} and {#link|Vectors#} of floats.
2019-06-21 21:18:59 +00:00
< / p >
{#header_close#}
{#header_open|@log2#}
2020-07-11 11:08:20 +00:00
< pre > {#syntax#}@log2(value: anytype) @TypeOf(value){#endsyntax#}< / pre >
2019-06-21 21:18:59 +00:00
< p >
Returns the logarithm to the base 2 of a floating point number. Uses a dedicated hardware instruction
2020-01-02 04:27:43 +00:00
when available.
< / p >
< p >
2023-02-02 14:11:13 +00:00
Supports {#link|Floats#} and {#link|Vectors#} of floats.
2019-06-21 21:18:59 +00:00
< / p >
{#header_close#}
{#header_open|@log10#}
2020-07-11 11:08:20 +00:00
< pre > {#syntax#}@log10(value: anytype) @TypeOf(value){#endsyntax#}< / pre >
2019-06-21 21:18:59 +00:00
< p >
Returns the logarithm to the base 10 of a floating point number. Uses a dedicated hardware instruction
2020-01-02 04:27:43 +00:00
when available.
< / p >
< p >
2023-02-02 14:11:13 +00:00
Supports {#link|Floats#} and {#link|Vectors#} of floats.
2019-06-21 21:18:59 +00:00
< / p >
{#header_close#}
2023-07-18 00:19:03 +00:00
{#header_open|@abs#}
< pre > {#syntax#}@abs(value: anytype) anytype{#endsyntax#}< / pre >
2019-06-21 21:18:59 +00:00
< p >
2023-07-18 00:19:03 +00:00
Returns the absolute value of an integer or a floating point number. Uses a dedicated hardware instruction
2020-01-02 04:27:43 +00:00
when available.
2023-07-18 00:19:03 +00:00
The return type is always an unsigned integer of the same bit width as the operand if the operand is an integer.
Unsigned integer operands are supported. The builtin cannot overflow for signed integer operands.
2020-01-02 04:27:43 +00:00
< / p >
< p >
2023-07-18 00:19:03 +00:00
Supports {#link|Floats#}, {#link|Integers#} and {#link|Vectors#} of floats or integers.
2019-06-21 21:18:59 +00:00
< / p >
{#header_close#}
{#header_open|@floor#}
2020-07-11 11:08:20 +00:00
< pre > {#syntax#}@floor(value: anytype) @TypeOf(value){#endsyntax#}< / pre >
2020-01-02 04:27:43 +00:00
< p >
Returns the largest integral value not greater than the given floating point number.
Uses a dedicated hardware instruction when available.
< / p >
2019-06-21 21:18:59 +00:00
< p >
2023-02-02 14:11:13 +00:00
Supports {#link|Floats#} and {#link|Vectors#} of floats.
2019-06-21 21:18:59 +00:00
< / p >
{#header_close#}
{#header_open|@ceil#}
2020-07-11 11:08:20 +00:00
< pre > {#syntax#}@ceil(value: anytype) @TypeOf(value){#endsyntax#}< / pre >
2019-06-21 21:18:59 +00:00
< p >
2021-09-30 04:19:23 +00:00
Returns the smallest integral value not less than the given floating point number.
2020-01-02 04:27:43 +00:00
Uses a dedicated hardware instruction when available.
< / p >
< p >
2023-02-02 14:11:13 +00:00
Supports {#link|Floats#} and {#link|Vectors#} of floats.
2019-06-21 21:18:59 +00:00
< / p >
{#header_close#}
{#header_open|@trunc#}
2020-07-11 11:08:20 +00:00
< pre > {#syntax#}@trunc(value: anytype) @TypeOf(value){#endsyntax#}< / pre >
2020-01-02 04:27:43 +00:00
< p >
Rounds the given floating point number to an integer, towards zero.
Uses a dedicated hardware instruction when available.
< / p >
2019-06-21 21:18:59 +00:00
< p >
2023-02-02 14:11:13 +00:00
Supports {#link|Floats#} and {#link|Vectors#} of floats.
2019-06-21 21:18:59 +00:00
< / p >
{#header_close#}
{#header_open|@round#}
2020-07-11 11:08:20 +00:00
< pre > {#syntax#}@round(value: anytype) @TypeOf(value){#endsyntax#}< / pre >
2018-04-15 17:21:52 +00:00
< p >
2024-08-14 17:29:45 +00:00
Rounds the given floating point number to the nearest integer. If two integers are equally close, rounds away from zero.
Uses a dedicated hardware instruction when available.
2020-01-02 04:27:43 +00:00
< / p >
2024-08-14 17:29:45 +00:00
{#code|test_round_builtin.zig#}
2020-01-02 04:27:43 +00:00
< p >
2023-02-02 14:11:13 +00:00
Supports {#link|Floats#} and {#link|Vectors#} of floats.
2018-04-15 17:21:52 +00:00
< / p >
{#header_close#}
2018-12-15 01:21:23 +00:00
2018-01-17 04:19:05 +00:00
{#header_open|@subWithOverflow#}
2022-12-21 14:40:30 +00:00
< pre > {#syntax#}@subWithOverflow(a: anytype, b: anytype) struct { @TypeOf(a, b), u1 }{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
2022-12-21 14:40:30 +00:00
Performs {#syntax#}a - b{#endsyntax#} and returns a tuple with the result and a possible overflow bit.
2017-11-07 08:22:27 +00:00
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
2022-02-04 19:27:50 +00:00
2018-06-18 07:07:16 +00:00
{#header_open|@tagName#}
2021-04-27 22:57:28 +00:00
< pre > {#syntax#}@tagName(value: anytype) [:0]const u8{#endsyntax#}< / pre >
2018-06-18 07:07:16 +00:00
< p >
2021-04-27 22:57:28 +00:00
Converts an enum value or union value to a string literal representing the name.< / p > < p > If the enum is non-exhaustive and the tag value does not map to a name, it invokes safety-checked {#link|Undefined Behavior#}.
2018-06-18 07:07:16 +00:00
< / p >
{#header_close#}
2018-12-15 01:21:23 +00:00
2018-09-15 14:14:50 +00:00
{#header_open|@This#}
< pre > {#syntax#}@This() type{#endsyntax#}< / pre >
< p >
2020-06-20 11:16:57 +00:00
Returns the innermost struct, enum, or union that this function call is inside.
2018-09-15 14:14:50 +00:00
This can be useful for an anonymous struct that needs to refer to itself:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_this_builtin.zig#}
2018-09-15 14:14:50 +00:00
< p >
2021-06-23 05:45:36 +00:00
When {#syntax#}@This(){#endsyntax#} is used at file scope, it returns a reference to the
2020-05-05 23:13:35 +00:00
struct that corresponds to the current file.
2018-09-15 14:14:50 +00:00
< / p >
{#header_close#}
2018-12-15 01:21:23 +00:00
2023-03-03 17:35:03 +00:00
{#header_open|@trap#}
< pre > {#syntax#}@trap() noreturn{#endsyntax#}< / pre >
< p >
This function inserts a platform-specific trap/jam instruction which can be used to exit the program abnormally.
This may be implemented by explicitly emitting an invalid instruction which may cause an illegal instruction exception of some sort.
Unlike for {#syntax#}@breakpoint(){#endsyntax#}, execution does not continue after this point.
< / p >
< p >
2023-03-04 21:22:46 +00:00
Outside function scope, this builtin causes a compile error.
2023-03-03 17:35:03 +00:00
< / p >
{#see_also|@breakpoint#}
{#header_close#}
2018-12-15 01:21:23 +00:00
2018-01-17 04:19:05 +00:00
{#header_open|@truncate#}
2023-06-23 18:37:50 +00:00
< pre > {#syntax#}@truncate(integer: anytype) anytype{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
This function truncates bits from an integer type, resulting in a smaller
2023-06-23 18:37:50 +00:00
or same-sized integer type. The return type is the inferred result type.
2017-11-07 08:22:27 +00:00
< / p >
< p >
2021-11-28 11:28:47 +00:00
This function always truncates the significant bits of the integer, regardless
of endianness on the target platform.
2017-11-07 08:22:27 +00:00
< / p >
< p >
2021-11-28 11:28:47 +00:00
Calling {#syntax#}@truncate{#endsyntax#} on a number out of range of the destination type is well defined and working code:
2017-11-07 08:22:27 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_truncate_builtin.zig#}
2019-04-11 02:58:42 +00:00
2017-11-07 08:22:27 +00:00
< p >
2021-11-28 11:28:47 +00:00
Use {#link|@intCast#} to convert numbers guaranteed to fit the destination type.
2017-11-07 08:22:27 +00:00
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-12-15 01:21:23 +00:00
2019-09-04 15:08:28 +00:00
{#header_open|@Type#}
2022-06-25 08:59:17 +00:00
< pre > {#syntax#}@Type(comptime info: std.builtin.Type) type{#endsyntax#}< / pre >
2019-09-04 15:08:28 +00:00
< p >
This function is the inverse of {#link|@typeInfo#}. It reifies type information
into a {#syntax#}type{#endsyntax#}.
< / p >
< p >
It is available for the following types:
< / p >
< ul >
< li > {#syntax#}type{#endsyntax#}< / li >
< li > {#syntax#}noreturn{#endsyntax#}< / li >
< li > {#syntax#}void{#endsyntax#}< / li >
< li > {#syntax#}bool{#endsyntax#}< / li >
2019-12-05 21:38:59 +00:00
< li > {#link|Integers#} - The maximum bit count for an integer type is {#syntax#}65535{#endsyntax#}.< / li >
2019-09-04 15:08:28 +00:00
< li > {#link|Floats#}< / li >
< li > {#link|Pointers#}< / li >
< li > {#syntax#}comptime_int{#endsyntax#}< / li >
< li > {#syntax#}comptime_float{#endsyntax#}< / li >
2019-12-09 20:59:42 +00:00
< li > {#syntax#}@TypeOf(undefined){#endsyntax#}< / li >
< li > {#syntax#}@TypeOf(null){#endsyntax#}< / li >
2020-04-28 05:23:13 +00:00
< li > {#link|Arrays#}< / li >
< li > {#link|Optionals#}< / li >
2020-09-23 19:31:57 +00:00
< li > {#link|Error Set Type#}< / li >
2020-04-28 05:23:13 +00:00
< li > {#link|Error Union Type#}< / li >
< li > {#link|Vectors#}< / li >
2020-09-26 15:05:11 +00:00
< li > {#link|opaque#}< / li >
2020-09-23 19:31:57 +00:00
< li > {#syntax#}anyframe{#endsyntax#}< / li >
< li > {#link|struct#}< / li >
< li > {#link|enum#}< / li >
< li > {#link|Enum Literals#}< / li >
< li > {#link|union#}< / li >
2023-01-25 09:28:14 +00:00
< li > {#link|Functions#}< / li >
2019-09-04 15:08:28 +00:00
< / ul >
{#header_close#}
2018-05-01 10:42:20 +00:00
{#header_open|@typeInfo#}
2022-06-25 08:59:17 +00:00
< pre > {#syntax#}@typeInfo(comptime T: type) std.builtin.Type{#endsyntax#}< / pre >
2018-05-01 10:42:20 +00:00
< p >
2019-12-16 17:07:05 +00:00
Provides type reflection.
2018-05-01 10:42:20 +00:00
< / p >
2019-04-05 16:37:49 +00:00
< p >
2022-02-14 19:26:55 +00:00
Type information of {#link|structs|struct#}, {#link|unions|union#}, {#link|enums|enum#}, and
2022-09-24 18:42:00 +00:00
{#link|error sets|Error Set Type#} has fields which are guaranteed to be in the same
2022-02-14 19:26:55 +00:00
order as appearance in the source file.
< / p >
< p >
Type information of {#link|structs|struct#}, {#link|unions|union#}, {#link|enums|enum#}, and
{#link|opaques|opaque#} has declarations, which are also guaranteed to be in the same
order as appearance in the source file.
2019-04-05 16:37:49 +00:00
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-12-15 01:21:23 +00:00
2018-01-17 04:19:05 +00:00
{#header_open|@typeName#}
2021-04-28 00:13:47 +00:00
< pre > {#syntax#}@typeName(T: type) *const [N:0]u8{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
2019-01-30 02:47:26 +00:00
This function returns the string representation of a type, as
an array. It is equivalent to a string literal of the type name.
2022-03-18 07:12:22 +00:00
The returned type name is fully qualified with the parent namespace included
as part of the type name with a series of dots.
2017-11-07 08:22:27 +00:00
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-12-15 01:21:23 +00:00
2019-12-09 20:59:42 +00:00
{#header_open|@TypeOf#}
2020-03-04 23:05:14 +00:00
< pre > {#syntax#}@TypeOf(...) type{#endsyntax#}< / pre >
2017-11-07 08:22:27 +00:00
< p >
2024-10-03 11:37:36 +00:00
{#syntax#}@TypeOf{#endsyntax#} is a special builtin function that takes any (non-zero) number of expressions
2020-03-04 23:05:14 +00:00
as parameters and returns the type of the result, using {#link|Peer Type Resolution#}.
< / p >
< p >
The expressions are evaluated, however they are guaranteed to have no < em > runtime< / em > side-effects:
2017-11-07 08:22:27 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_TypeOf_builtin.zig#}
2017-11-07 08:22:27 +00:00
2019-01-31 04:36:52 +00:00
{#header_close#}
2019-07-03 17:40:40 +00:00
{#header_open|@unionInit#}
< pre > {#syntax#}@unionInit(comptime Union: type, comptime active_field_name: []const u8, init_expr) Union{#endsyntax#}< / pre >
< p >
This is the same thing as {#link|union#} initialization syntax, except that the field name is a
{#link|comptime#}-known value rather than an identifier token.
< / p >
< p >
{#syntax#}@unionInit{#endsyntax#} forwards its {#link|result location|Result Location Semantics#} to {#syntax#}init_expr{#endsyntax#}.
< / p >
{#header_close#}
2022-03-14 05:21:16 +00:00
{#header_open|@Vector#}
< pre > {#syntax#}@Vector(len: comptime_int, Element: type) type{#endsyntax#}< / pre >
< p > Creates {#link|Vectors#}.< / p >
{#header_close#}
2023-02-13 14:19:17 +00:00
{#header_open|@volatileCast#}
< pre > {#syntax#}@volatileCast(value: anytype) DestType{#endsyntax#}< / pre >
< p >
Remove {#syntax#}volatile{#endsyntax#} qualifier from a pointer.
< / p >
{#header_close#}
2023-03-18 14:59:56 +00:00
{#header_open|@workGroupId#}
< pre > {#syntax#}@workGroupId(comptime dimension: u32) u32{#endsyntax#}< / pre >
< p >
Returns the index of the work group in the current kernel invocation in dimension {#syntax#}dimension{#endsyntax#}.
< / p >
{#header_close#}
{#header_open|@workGroupSize#}
< pre > {#syntax#}@workGroupSize(comptime dimension: u32) u32{#endsyntax#}< / pre >
< p >
Returns the number of work items that a work group has in dimension {#syntax#}dimension{#endsyntax#}.
< / p >
{#header_close#}
{#header_open|@workItemId#}
< pre > {#syntax#}@workItemId(comptime dimension: u32) u32{#endsyntax#}< / pre >
< p >
Returns the index of the work item in the work group in dimension {#syntax#}dimension{#endsyntax#}. This function returns values between {#syntax#}0{#endsyntax#} (inclusive) and {#syntax#}@workGroupSize(dimension){#endsyntax#} (exclusive).
< / p >
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-12-15 01:21:23 +00:00
2018-01-17 04:19:05 +00:00
{#header_open|Build Mode#}
2017-11-07 08:22:27 +00:00
< p >
2018-06-14 22:12:05 +00:00
Zig has four build modes:
2017-11-07 08:22:27 +00:00
< / p >
< ul >
2018-01-23 04:06:07 +00:00
< li > {#link|Debug#} (default)< / li >
< li > {#link|ReleaseFast#}< / li >
< li > {#link|ReleaseSafe#}< / li >
2018-06-14 22:12:05 +00:00
< li > {#link|ReleaseSmall#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< p >
2021-08-11 09:01:03 +00:00
To add standard build options to a < code class = "file" > build.zig< / code > file:
2017-11-07 08:22:27 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|build.zig#}
2017-11-07 08:22:27 +00:00
< p >
This causes these options to be available:
< / p >
2021-08-11 09:01:03 +00:00
< dl >
2023-03-04 09:10:07 +00:00
< dt > < kbd > -Doptimize=Debug< / kbd > < / dt > < dd > Optimizations off and safety on (default)< / dd >
< dt > < kbd > -Doptimize=ReleaseSafe< / kbd > < / dt > < dd > Optimizations on and safety on< / dd >
< dt > < kbd > -Doptimize=ReleaseFast< / kbd > < / dt > < dd > Optimizations on and safety off< / dd >
< dt > < kbd > -Doptimize=ReleaseSmall< / kbd > < / dt > < dd > Size optimizations on and safety off< / dd >
2021-08-11 09:01:03 +00:00
< / dl >
2018-01-17 04:19:05 +00:00
{#header_open|Debug#}
2021-08-11 09:01:03 +00:00
{#shell_samp#}$ zig build-exe example.zig{#end_shell_samp#}
2017-11-07 08:22:27 +00:00
< ul >
< li > Fast compilation speed< / li >
< li > Safety checks enabled< / li >
< li > Slow runtime performance< / li >
2018-06-14 22:12:05 +00:00
< li > Large binary size< / li >
2018-09-22 14:22:01 +00:00
< li > No reproducible build requirement< / li >
2017-11-07 08:22:27 +00:00
< / ul >
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|ReleaseFast#}
2021-08-11 09:01:03 +00:00
{#shell_samp#}$ zig build-exe example.zig -O ReleaseFast{#end_shell_samp#}
2017-11-07 08:22:27 +00:00
< ul >
< li > Fast runtime performance< / li >
< li > Safety checks disabled< / li >
< li > Slow compilation speed< / li >
2018-06-14 22:12:05 +00:00
< li > Large binary size< / li >
2018-09-22 14:22:01 +00:00
< li > Reproducible build< / li >
2017-11-07 08:22:27 +00:00
< / ul >
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|ReleaseSafe#}
2021-08-11 09:01:03 +00:00
{#shell_samp#}$ zig build-exe example.zig -O ReleaseSafe{#end_shell_samp#}
2017-11-07 08:22:27 +00:00
< ul >
< li > Medium runtime performance< / li >
< li > Safety checks enabled< / li >
< li > Slow compilation speed< / li >
2018-06-14 22:12:05 +00:00
< li > Large binary size< / li >
2018-09-22 14:22:01 +00:00
< li > Reproducible build< / li >
2018-06-14 22:12:05 +00:00
< / ul >
{#header_close#}
{#header_open|ReleaseSmall#}
2021-08-11 09:01:03 +00:00
{#shell_samp#}$ zig build-exe example.zig -O ReleaseSmall{#end_shell_samp#}
2018-06-14 22:12:05 +00:00
< ul >
< li > Medium runtime performance< / li >
< li > Safety checks disabled< / li >
< li > Slow compilation speed< / li >
< li > Small binary size< / li >
2018-09-22 14:22:01 +00:00
< li > Reproducible build< / li >
2017-11-07 08:22:27 +00:00
< / ul >
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-06-14 22:12:05 +00:00
{#see_also|Compile Variables|Zig Build System|Undefined Behavior#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2019-02-01 22:49:29 +00:00
{#header_open|Single Threaded Builds#}
2023-02-04 18:08:01 +00:00
< p > Zig has a compile option < kbd > -fsingle-threaded< / kbd > which has the following effects:< / p >
2019-02-01 22:49:29 +00:00
< ul >
2021-06-23 05:45:36 +00:00
< li > All {#link|Thread Local Variables#} are treated as regular {#link|Container Level Variables#}.< / li >
2019-08-13 18:14:19 +00:00
< li > The overhead of {#link|Async Functions#} becomes equivalent to function call overhead.< / li >
2019-02-01 22:49:29 +00:00
< li > The {#syntax#}@import("builtin").single_threaded{#endsyntax#} becomes {#syntax#}true{#endsyntax#}
and therefore various userland APIs which read this variable become more efficient.
For example {#syntax#}std.Mutex{#endsyntax#} becomes
an empty data structure and all of its functions become no-ops.< / li >
< / ul >
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|Undefined Behavior#}
2017-11-07 08:22:27 +00:00
< p >
Zig has many instances of undefined behavior. If undefined behavior is
2018-06-18 19:01:42 +00:00
detected at compile-time, Zig emits a compile error and refuses to continue.
Most undefined behavior that cannot be detected at compile-time can be detected
at runtime. In these cases, Zig has safety checks. Safety checks can be disabled
2019-09-26 16:05:49 +00:00
on a per-block basis with {#link|@setRuntimeSafety#}. The {#link|ReleaseFast#}
and {#link|ReleaseSmall#} build modes disable all safety checks (except where overridden
by {#link|@setRuntimeSafety#}) in order to facilitate optimizations.
2017-11-07 08:22:27 +00:00
< / p >
< p >
When a safety check fails, Zig crashes with a stack trace, like this:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_undefined_behavior.zig#}
2018-01-17 04:19:05 +00:00
{#header_open|Reaching Unreachable Code#}
2017-11-07 08:22:27 +00:00
< p > At compile-time:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_reaching_unreachable.zig#}
2018-07-02 19:49:49 +00:00
< p > At runtime:< / p >
2024-04-25 00:41:47 +00:00
{#code|runtime_reaching_unreachable.zig#}
2018-07-02 19:49:49 +00:00
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Index out of Bounds#}
2017-11-07 08:22:27 +00:00
< p > At compile-time:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_index_out_of_bounds.zig#}
2018-07-02 19:49:49 +00:00
< p > At runtime:< / p >
2024-04-25 00:41:47 +00:00
{#code|runtime_index_out_of_bounds.zig#}
2018-07-02 19:49:49 +00:00
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Cast Negative Number to Unsigned Integer#}
2017-11-07 08:22:27 +00:00
< p > At compile-time:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_invalid_cast.zig#}
2018-07-02 19:49:49 +00:00
< p > At runtime:< / p >
2024-04-25 00:41:47 +00:00
{#code|runtime_invalid_cast.zig#}
2017-11-07 08:22:27 +00:00
< p >
2018-10-26 18:59:58 +00:00
To obtain the maximum value of an unsigned integer, use {#syntax#}std.math.maxInt{#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Cast Truncates Data#}
2017-11-07 08:22:27 +00:00
< p > At compile-time:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_invalid_cast_truncate.zig#}
2018-07-02 19:49:49 +00:00
< p > At runtime:< / p >
2024-04-25 00:41:47 +00:00
{#code|runtime_invalid_cast_truncate.zig#}
2017-11-07 08:22:27 +00:00
< p >
2018-07-02 19:49:49 +00:00
To truncate bits, use {#link|@truncate#}.
2017-11-07 08:22:27 +00:00
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Integer Overflow#}
{#header_open|Default Operations#}
2017-11-07 08:22:27 +00:00
< p > The following operators can cause integer overflow:< / p >
< ul >
2018-09-14 14:35:03 +00:00
< li > {#syntax#}+{#endsyntax#} (addition)< / li >
< li > {#syntax#}-{#endsyntax#} (subtraction)< / li >
< li > {#syntax#}-{#endsyntax#} (negation)< / li >
< li > {#syntax#}*{#endsyntax#} (multiplication)< / li >
< li > {#syntax#}/{#endsyntax#} (division)< / li >
2018-07-02 19:49:49 +00:00
< li > {#link|@divTrunc#} (division)< / li >
< li > {#link|@divFloor#} (division)< / li >
< li > {#link|@divExact#} (division)< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< p > Example with addition at compile-time:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_overflow.zig#}
2018-07-02 19:49:49 +00:00
< p > At runtime:< / p >
2024-04-25 00:41:47 +00:00
{#code|runtime_overflow.zig#}
2018-07-02 19:49:49 +00:00
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Standard Library Math Functions#}
2017-11-07 08:22:27 +00:00
< p > These functions provided by the standard library return possible errors.< / p >
< ul >
2018-09-14 14:35:03 +00:00
< li > {#syntax#}@import("std").math.add{#endsyntax#}< / li >
< li > {#syntax#}@import("std").math.sub{#endsyntax#}< / li >
< li > {#syntax#}@import("std").math.mul{#endsyntax#}< / li >
< li > {#syntax#}@import("std").math.divTrunc{#endsyntax#}< / li >
< li > {#syntax#}@import("std").math.divFloor{#endsyntax#}< / li >
< li > {#syntax#}@import("std").math.divExact{#endsyntax#}< / li >
< li > {#syntax#}@import("std").math.shl{#endsyntax#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< p > Example of catching an overflow for addition:< / p >
2024-04-25 00:41:47 +00:00
{#code|math_add.zig#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Builtin Overflow Functions#}
2017-11-07 08:22:27 +00:00
< p >
2023-03-13 14:47:20 +00:00
These builtins return a tuple containing whether there was an overflow
(as a {#syntax#}u1{#endsyntax#}) and the possibly overflowed bits of the operation:
2017-11-07 08:22:27 +00:00
< / p >
< ul >
2018-07-02 19:49:49 +00:00
< li > {#link|@addWithOverflow#}< / li >
< li > {#link|@subWithOverflow#}< / li >
< li > {#link|@mulWithOverflow#}< / li >
< li > {#link|@shlWithOverflow#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
< p >
2018-07-02 19:49:49 +00:00
Example of {#link|@addWithOverflow#}:
2017-11-07 08:22:27 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|addWithOverflow_builtin.zig#}
2017-11-07 08:22:27 +00:00
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Wrapping Operations#}
2017-11-07 08:22:27 +00:00
< p >
These operations have guaranteed wraparound semantics.
< / p >
< ul >
2018-09-14 14:35:03 +00:00
< li > {#syntax#}+%{#endsyntax#} (wraparound addition)< / li >
< li > {#syntax#}-%{#endsyntax#} (wraparound subtraction)< / li >
< li > {#syntax#}-%{#endsyntax#} (wraparound negation)< / li >
< li > {#syntax#}*%{#endsyntax#} (wraparound multiplication)< / li >
2017-11-07 08:22:27 +00:00
< / ul >
2024-04-25 00:41:47 +00:00
{#code|test_wraparound_semantics.zig#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_close#}
{#header_open|Exact Left Shift Overflow#}
2017-11-07 08:22:27 +00:00
< p > At compile-time:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_shlExact_overwlow.zig#}
2018-07-02 19:49:49 +00:00
< p > At runtime:< / p >
2024-04-25 00:41:47 +00:00
{#code|runtime_shlExact_overflow.zig#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Exact Right Shift Overflow#}
2017-11-07 08:22:27 +00:00
< p > At compile-time:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_shrExact_overflow.zig#}
2018-07-02 19:49:49 +00:00
< p > At runtime:< / p >
2024-04-25 00:41:47 +00:00
{#code|runtime_shrExact_overflow.zig#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Division by Zero#}
2017-11-07 08:22:27 +00:00
< p > At compile-time:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_division_by_zero.zig#}
2018-07-02 19:49:49 +00:00
< p > At runtime:< / p >
2024-04-25 00:41:47 +00:00
{#code|runtime_division_by_zero.zig#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Remainder Division by Zero#}
2017-11-07 08:22:27 +00:00
< p > At compile-time:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_remainder_division_by_zero.zig#}
2018-07-02 19:49:49 +00:00
< p > At runtime:< / p >
2024-04-25 00:41:47 +00:00
{#code|runtime_remainder_division_by_zero.zig#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Exact Division Remainder#}
2018-07-02 19:49:49 +00:00
< p > At compile-time:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_divExact_remainder.zig#}
2018-07-02 19:49:49 +00:00
< p > At runtime:< / p >
2024-04-25 00:41:47 +00:00
{#code|runtime_divExact_remainder.zig#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Attempt to Unwrap Null#}
2017-11-07 08:22:27 +00:00
< p > At compile-time:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_unwrap_null.zig#}
2018-07-02 19:49:49 +00:00
< p > At runtime:< / p >
2024-04-25 00:41:47 +00:00
{#code|runtime_unwrap_null.zig#}
2017-11-07 08:22:27 +00:00
< p > One way to avoid this crash is to test for null instead of assuming non-null, with
2018-09-14 14:35:03 +00:00
the {#syntax#}if{#endsyntax#} expression:< / p >
2024-04-25 00:41:47 +00:00
{#code|testing_null_with_if.zig#}
2017-11-07 08:22:27 +00:00
2018-07-02 19:49:49 +00:00
{#see_also|Optionals#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Attempt to Unwrap Error#}
2017-11-07 08:22:27 +00:00
< p > At compile-time:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_unwrap_error.zig#}
2017-11-07 08:22:27 +00:00
2018-07-02 19:49:49 +00:00
< p > At runtime:< / p >
2024-04-25 00:41:47 +00:00
{#code|runtime_unwrap_error.zig#}
2018-07-02 19:49:49 +00:00
2017-11-07 08:22:27 +00:00
< p > One way to avoid this crash is to test for an error instead of assuming a successful result, with
2018-09-14 14:35:03 +00:00
the {#syntax#}if{#endsyntax#} expression:< / p >
2024-04-25 00:41:47 +00:00
{#code|testing_error_with_if.zig#}
2017-11-07 08:22:27 +00:00
2018-07-02 19:49:49 +00:00
{#see_also|Errors#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Invalid Error Code#}
2017-11-07 08:22:27 +00:00
< p > At compile-time:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_invalid_error_code.zig#}
2018-07-02 19:49:49 +00:00
< p > At runtime:< / p >
2024-04-25 00:41:47 +00:00
{#code|runtime_invalid_error_code.zig#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Invalid Enum Cast#}
2018-07-02 19:49:49 +00:00
< p > At compile-time:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_invalid_enum_cast.zig#}
2018-07-02 19:49:49 +00:00
2024-04-25 00:41:47 +00:00
< p > At runtime:< / p >
{#code|runtime_invalid_enum_cast.zig#}
2017-11-07 08:22:27 +00:00
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-06-18 19:01:42 +00:00
{#header_open|Invalid Error Set Cast#}
2018-07-09 15:13:29 +00:00
< p > At compile-time:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_invalid_error_set_cast.zig#}
2018-07-09 15:13:29 +00:00
< p > At runtime:< / p >
2024-04-25 00:41:47 +00:00
{#code|runtime_invalid_error_set_cast.zig#}
2018-07-10 14:37:58 +00:00
2018-06-18 19:01:42 +00:00
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|Incorrect Pointer Alignment#}
2018-07-09 15:13:29 +00:00
< p > At compile-time:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_incorrect_pointer_alignment.zig#}
2018-07-09 15:13:29 +00:00
< p > At runtime:< / p >
2024-04-25 00:41:47 +00:00
{#code|runtime_incorrect_pointer_alignment.zig#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Wrong Union Field Access#}
2018-07-10 14:37:58 +00:00
< p > At compile-time:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_wrong_union_field_access.zig#}
2018-07-10 14:37:58 +00:00
< p > At runtime:< / p >
2024-04-25 00:41:47 +00:00
{#code|runtime_wrong_union_field_access.zig#}
2018-07-10 14:37:58 +00:00
< p >
2018-09-14 14:35:03 +00:00
This safety is not available for {#syntax#}extern{#endsyntax#} or {#syntax#}packed{#endsyntax#} unions.
2018-07-10 14:37:58 +00:00
< / p >
< p >
To change the active field of a union, assign the entire union, like this:
< / p >
2024-04-25 00:41:47 +00:00
{#code|change_active_union_field.zig#}
2018-07-10 14:37:58 +00:00
< p >
To change the active field of a union when a meaningful value for the field is not known,
use {#link|undefined#}, like this:
< / p >
2024-04-25 00:41:47 +00:00
{#code|undefined_active_union_field.zig#}
2018-07-10 14:37:58 +00:00
2019-02-27 03:46:35 +00:00
{#see_also|union|extern union#}
2018-06-19 20:06:10 +00:00
{#header_close#}
2017-11-17 03:13:20 +00:00
2019-02-14 23:59:20 +00:00
{#header_open|Out of Bounds Float to Integer Cast#}
2023-07-21 13:17:58 +00:00
< p >
This happens when casting a float to an integer where the float has a value outside the
integer type's range.
< / p >
< p > At compile-time:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_out_of_bounds_float_to_integer_cast.zig#}
2023-07-21 13:17:58 +00:00
< p > At runtime:< / p >
2024-04-25 00:41:47 +00:00
{#code|runtime_out_of_bounds_float_to_integer_cast.zig#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-06-18 19:01:42 +00:00
2019-02-14 23:59:20 +00:00
{#header_open|Pointer Cast Invalid Null#}
2019-03-25 16:55:45 +00:00
< p >
This happens when casting a pointer with the address 0 to a pointer which may not have the address 0.
For example, {#link|C Pointers#}, {#link|Optional Pointers#}, and {#link|allowzero#} pointers
allow address zero, but normal {#link|Pointers#} do not.
< / p >
2019-02-14 23:59:20 +00:00
< p > At compile-time:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_comptime_invalid_null_pointer_cast.zig#}
2019-02-14 23:59:20 +00:00
< p > At runtime:< / p >
2024-04-25 00:41:47 +00:00
{#code|runtime_invalid_null_pointer_cast.zig#}
2019-02-14 23:59:20 +00:00
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Memory#}
2019-03-19 01:40:24 +00:00
< p >
The Zig language performs no memory management on behalf of the programmer. This is
why Zig has no runtime, and why Zig code works seamlessly in so many environments,
including real-time software, operating system kernels, embedded devices, and
low latency servers. As a consequence, Zig programmers must always be able to answer
the question:
< / p >
< p > {#link|Where are the bytes?#}< / p >
< p >
Like Zig, the C programming language has manual memory management. However, unlike Zig,
C has a default allocator - < code > malloc< / code > , < code > realloc< / code > , and < code > free< / code > .
When linking against libc, Zig exposes this allocator with {#syntax#}std.heap.c_allocator{#endsyntax#}.
However, by convention, there is no default allocator in Zig. Instead, functions which need to
2021-10-29 01:03:15 +00:00
allocate accept an {#syntax#}Allocator{#endsyntax#} parameter. Likewise, data structures such as
{#syntax#}std.ArrayList{#endsyntax#} accept an {#syntax#}Allocator{#endsyntax#} parameter in
2019-03-19 01:40:24 +00:00
their initialization functions:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_allocator.zig#}
2019-03-19 01:40:24 +00:00
< p >
In the above example, 100 bytes of stack memory are used to initialize a
{#syntax#}FixedBufferAllocator{#endsyntax#}, which is then passed to a function.
As a convenience there is a global {#syntax#}FixedBufferAllocator{#endsyntax#}
2020-01-30 03:39:08 +00:00
available for quick tests at {#syntax#}std.testing.allocator{#endsyntax#},
2022-09-24 18:42:00 +00:00
which will also perform basic leak detection.
2019-03-19 01:40:24 +00:00
< / p >
< p >
2020-08-12 08:56:34 +00:00
Zig has a general purpose allocator available to be imported
with {#syntax#}std.heap.GeneralPurposeAllocator{#endsyntax#}. However, it is still recommended to
2019-03-19 01:40:24 +00:00
follow the {#link|Choosing an Allocator#} guide.
< / p >
{#header_open|Choosing an Allocator#}
< p > What allocator to use depends on a number of factors. Here is a flow chart to help you decide:
< / p >
< ol >
< li >
2021-10-29 01:03:15 +00:00
Are you making a library? In this case, best to accept an {#syntax#}Allocator{#endsyntax#}
2019-03-19 01:40:24 +00:00
as a parameter and allow your library's users to decide what allocator to use.
< / li >
< li > Are you linking libc? In this case, {#syntax#}std.heap.c_allocator{#endsyntax#} is likely
the right choice, at least for your main allocator.< / li >
2024-10-04 20:50:25 +00:00
< li >
Need to use the same allocator in multiple threads? Use one of your choice
wrapped around {#syntax#}std.heap.ThreadSafeAllocator{#endsyntax#}
< / li >
2019-03-19 01:40:24 +00:00
< li >
Is the maximum number of bytes that you will need bounded by a number known at
2024-10-04 20:50:25 +00:00
{#link|comptime#}? In this case, use {#syntax#}std.heap.FixedBufferAllocator{#endsyntax#}.
2019-03-19 01:40:24 +00:00
< / li >
< li >
Is your program a command line application which runs from start to end without any fundamental
cyclical pattern (such as a video game main loop, or a web server request handler),
such that it would make sense to free everything at once at the end?
In this case, it is recommended to follow this pattern:
2024-04-25 00:41:47 +00:00
{#code|cli_allocation.zig#}
2019-03-19 01:40:24 +00:00
When using this kind of allocator, there is no need to free anything manually. Everything
gets freed at once with the call to {#syntax#}arena.deinit(){#endsyntax#}.
< / li >
< li >
Are the allocations part of a cyclical pattern such as a video game main loop, or a web
server request handler? If the allocations can all be freed at once, at the end of the cycle,
for example once the video game frame has been fully rendered, or the web server request has
been served, then {#syntax#}std.heap.ArenaAllocator{#endsyntax#} is a great candidate. As
demonstrated in the previous bullet point, this allows you to free entire arenas at once.
Note also that if an upper bound of memory can be established, then
{#syntax#}std.heap.FixedBufferAllocator{#endsyntax#} can be used as a further optimization.
< / li >
< li >
Are you writing a test, and you want to make sure {#syntax#}error.OutOfMemory{#endsyntax#}
2020-01-30 03:39:08 +00:00
is handled correctly? In this case, use {#syntax#}std.testing.FailingAllocator{#endsyntax#}.
2019-03-19 01:40:24 +00:00
< / li >
< li >
2020-08-08 09:15:34 +00:00
Are you writing a test? In this case, use {#syntax#}std.testing.allocator{#endsyntax#}.
< / li >
< li >
Finally, if none of the above apply, you need a general purpose allocator.
Zig's general purpose allocator is available as a function that takes a {#link|comptime#}
{#link|struct#} of configuration options and returns a type.
2020-08-08 09:15:46 +00:00
Generally, you will set up one {#syntax#}std.heap.GeneralPurposeAllocator{#endsyntax#} in
2020-08-08 09:15:34 +00:00
your main function, and then pass it or sub-allocators around to various parts of your
application.
< / li >
< li >
2019-03-19 01:40:24 +00:00
You can also consider {#link|Implementing an Allocator#}.
< / li >
< / ol >
{#header_close#}
{#header_open|Where are the bytes?#}
2023-12-05 00:40:07 +00:00
< p > String literals such as {#syntax#}"hello"{#endsyntax#} are in the global constant data section.
2019-03-19 01:40:24 +00:00
This is why it is an error to pass a string literal to a mutable slice, like this:
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_string_literal_to_slice.zig#}
2019-03-19 01:40:24 +00:00
< p > However if you make the slice constant, then it works:< / p >
2024-04-25 00:41:47 +00:00
{#code|test_string_literal_to_const_slice.zig#}
2019-03-19 01:40:24 +00:00
< p >
2020-11-03 17:37:28 +00:00
Just like string literals, {#syntax#}const{#endsyntax#} declarations, when the value is known at {#link|comptime#},
2019-03-19 01:40:24 +00:00
are stored in the global constant data section. Also {#link|Compile Time Variables#} are stored
in the global constant data section.
< / p >
< p >
2020-11-03 17:37:28 +00:00
{#syntax#}var{#endsyntax#} declarations inside functions are stored in the function's stack frame. Once a function returns,
2019-03-19 01:40:24 +00:00
any {#link|Pointers#} to variables in the function's stack frame become invalid references, and
dereferencing them becomes unchecked {#link|Undefined Behavior#}.
< / p >
< p >
2020-11-03 17:37:28 +00:00
{#syntax#}var{#endsyntax#} declarations at the top level or in {#link|struct#} declarations are stored in the global
2019-03-19 01:40:24 +00:00
data section.
< / p >
< p >
The location of memory allocated with {#syntax#}allocator.alloc{#endsyntax#} or
{#syntax#}allocator.create{#endsyntax#} is determined by the allocator's implementation.
< / p >
2019-12-05 21:38:59 +00:00
< p > TODO: thread local variables< / p >
2019-03-19 01:40:24 +00:00
{#header_close#}
{#header_open|Implementing an Allocator#}
< p > Zig programmers can implement their own allocators by fulfilling the Allocator interface.
In order to do this one must read carefully the documentation comments in std/mem.zig and
2021-10-31 21:45:27 +00:00
then supply a {#syntax#}allocFn{#endsyntax#} and a {#syntax#}resizeFn{#endsyntax#}.
2019-03-19 01:40:24 +00:00
< / p >
< p >
There are many example allocators to look at for inspiration. Look at std/heap.zig and
2021-07-03 23:00:32 +00:00
{#syntax#}std.heap.GeneralPurposeAllocator{#endsyntax#}.
2019-03-19 01:40:24 +00:00
< / p >
{#header_close#}
{#header_open|Heap Allocation Failure#}
< p >
Many programming languages choose to handle the possibility of heap allocation failure by
unconditionally crashing. By convention, Zig programmers do not consider this to be a
satisfactory solution. Instead, {#syntax#}error.OutOfMemory{#endsyntax#} represents
heap allocation failure, and Zig libraries return this error code whenever heap allocation
failure prevented an operation from completing successfully.
< / p >
< p >
Some have argued that because some operating systems such as Linux have memory overcommit enabled by
default, it is pointless to handle heap allocation failure. There are many problems with this reasoning:
< / p >
< ul >
< li > Only some operating systems have an overcommit feature.
< ul >
< li > Linux has it enabled by default, but it is configurable.< / li >
< li > Windows does not overcommit.< / li >
< li > Embedded systems do not have overcommit.< / li >
< li > Hobby operating systems may or may not have overcommit.< / li >
< / ul >
< / li >
< li >
For real-time systems, not only is there no overcommit, but typically the maximum amount
of memory per application is determined ahead of time.
< / li >
< li >
When writing a library, one of the main goals is code reuse. By making code handle
allocation failure correctly, a library becomes eligible to be reused in
more contexts.
< / li >
< li >
Although some software has grown to depend on overcommit being enabled, its existence
is the source of countless user experience disasters. When a system with overcommit enabled,
such as Linux on default settings, comes close to memory exhaustion, the system locks up
and becomes unusable. At this point, the OOM Killer selects an application to kill
based on heuristics. This non-deterministic decision often results in an important process
being killed, and often fails to return the system back to working order.
< / li >
< / ul >
{#header_close#}
{#header_open|Recursion#}
< p >
Recursion is a fundamental tool in modeling software. However it has an often-overlooked problem:
unbounded memory allocation.
< / p >
< p >
Recursion is an area of active experimentation in Zig and so the documentation here is not final.
You can read a
< a href = "https://ziglang.org/download/0.3.0/release-notes.html#recursion" > summary of recursion status in the 0.3.0 release notes< / a > .
< / p >
< p >
The short summary is that currently recursion works normally as you would expect. Although Zig code
is not yet protected from stack overflow, it is planned that a future version of Zig will provide
such protection, with some degree of cooperation from Zig code required.
< / p >
{#header_close#}
{#header_open|Lifetime and Ownership#}
< p >
It is the Zig programmer's responsibility to ensure that a {#link|pointer|Pointers#} is not
accessed when the memory pointed to is no longer available. Note that a {#link|slice|Slices#}
is a form of pointer, in that it references other memory.
< / p >
< p >
In order to prevent bugs, there are some helpful conventions to follow when dealing with pointers.
In general, when a function returns a pointer, the documentation for the function should explain
who "owns" the pointer. This concept helps the programmer decide when it is appropriate, if ever,
to free the pointer.
< / p >
< p >
For example, the function's documentation may say "caller owns the returned memory", in which case
the code that calls the function must have a plan for when to free that memory. Probably in this situation,
2021-10-29 01:03:15 +00:00
the function will accept an {#syntax#}Allocator{#endsyntax#} parameter.
2019-03-19 01:40:24 +00:00
< / p >
< p >
2020-06-12 15:58:43 +00:00
Sometimes the lifetime of a pointer may be more complicated. For example, the
{#syntax#}std.ArrayList(T).items{#endsyntax#} slice has a lifetime that remains
2019-03-19 01:40:24 +00:00
valid until the next time the list is resized, such as by appending new elements.
< / p >
< p >
The API documentation for functions and data structures should take great care to explain
the ownership and lifetime semantics of pointers. Ownership determines whose responsibility it
is to free the memory referenced by the pointer, and lifetime determines the point at which
the memory becomes inaccessible (lest {#link|Undefined Behavior#} occur).
< / p >
{#header_close#}
2017-11-07 08:22:27 +00:00
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Compile Variables#}
2017-11-07 08:22:27 +00:00
< p >
2018-09-14 14:35:03 +00:00
Compile variables are accessible by importing the {#syntax#}"builtin"{#endsyntax#} package,
2017-11-07 08:22:27 +00:00
which the compiler makes available to every Zig source file. It contains
compile-time constants such as the current target, endianness, and release mode.
< / p >
2024-04-25 00:41:47 +00:00
{#code|compile_variables.zig#}
2017-11-07 08:22:27 +00:00
< p >
2018-09-14 14:35:03 +00:00
Example of what is imported with {#syntax#}@import("builtin"){#endsyntax#}:
2017-11-07 08:22:27 +00:00
< / p >
2018-06-07 23:10:45 +00:00
{#builtin#}
2018-01-17 05:22:33 +00:00
{#see_also|Build Mode#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Root Source File#}
2017-11-07 08:22:27 +00:00
< p > TODO: explain how root source file finds other files< / p >
< p > TODO: pub fn main< / p >
< p > TODO: pub fn panic< / p >
< p > TODO: if linking with libc you can use export fn main< / p >
< p > TODO: order independent top level declarations< / p >
< p > TODO: lazy analysis< / p >
< p > TODO: using comptime { _ = @import() }< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
2020-10-17 04:29:33 +00:00
{#header_open|Zig Build System#}
< p >
The Zig Build System provides a cross-platform, dependency-free way to declare
the logic required to build a project. With this system, the logic to build
a project is written in a build.zig file, using the Zig Build System API to
declare and configure build artifacts and other tasks.
< / p >
< p >
Some examples of tasks the build system can help with:
< / p >
< ul >
2024-01-19 04:14:44 +00:00
< li > Performing tasks in parallel and caching the results.< / li >
< li > Depending on other projects.< / li >
< li > Providing a package for other projects to depend on.< / li >
2020-10-17 04:29:33 +00:00
< li > Creating build artifacts by executing the Zig compiler. This includes
building Zig source code as well as C and C++ source code.< / li >
< li > Capturing user-configured options and using those options to configure
the build.< / li >
< li > Surfacing build configuration as {#link|comptime#} values by providing a
file that can be {#link|imported|@import#} by Zig code.< / li >
< li > Caching build artifacts to avoid unnecessarily repeating steps.< / li >
< li > Executing build artifacts or system-installed tools.< / li >
< li > Running tests and verifying the output of executing a build artifact matches
the expected value.< / li >
< li > Running < code > zig fmt< / code > on a codebase or a subset of it.< / li >
< li > Custom tasks.< / li >
< / ul >
< p >
2021-08-11 09:01:03 +00:00
To use the build system, run < kbd > zig build --help< / kbd >
2020-10-17 04:29:33 +00:00
to see a command-line usage help menu. This will include project-specific
options that were declared in the build.zig script.
< / p >
2024-01-19 04:14:44 +00:00
< p >
For the time being, the build system documentation is hosted externally:
< a href = "https://ziglang.org/learn/build-system/" > Build System Documentation< / a >
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|C#}
2017-11-07 08:22:27 +00:00
< p >
Although Zig is independent of C, and, unlike most other languages, does not depend on libc,
Zig acknowledges the importance of interacting with existing C code.
< / p >
< p >
There are a few ways that Zig facilitates C interop.
< / p >
2018-01-17 04:19:05 +00:00
{#header_open|C Type Primitives#}
2017-11-07 08:22:27 +00:00
< p >
These have guaranteed C ABI compatibility and can be used like any other type.
< / p >
< ul >
2023-06-28 18:58:45 +00:00
< li > {#syntax#}c_char{#endsyntax#}< / li >
2018-09-14 14:35:03 +00:00
< li > {#syntax#}c_short{#endsyntax#}< / li >
< li > {#syntax#}c_ushort{#endsyntax#}< / li >
< li > {#syntax#}c_int{#endsyntax#}< / li >
< li > {#syntax#}c_uint{#endsyntax#}< / li >
< li > {#syntax#}c_long{#endsyntax#}< / li >
< li > {#syntax#}c_ulong{#endsyntax#}< / li >
< li > {#syntax#}c_longlong{#endsyntax#}< / li >
< li > {#syntax#}c_ulonglong{#endsyntax#}< / li >
< li > {#syntax#}c_longdouble{#endsyntax#}< / li >
2017-11-07 08:22:27 +00:00
< / ul >
2021-12-21 13:13:32 +00:00
< p >
To interop with the C {#syntax#}void{#endsyntax#} type, use {#syntax#}anyopaque{#endsyntax#}.
< / p >
2018-01-17 05:22:33 +00:00
{#see_also|Primitive Types#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Import from C Header File#}
2017-11-07 08:22:27 +00:00
< p >
2018-09-14 14:35:03 +00:00
The {#syntax#}@cImport{#endsyntax#} builtin function can be used
2021-08-11 09:01:03 +00:00
to directly import symbols from < code class = "file" > .h< / code > files:
2017-11-07 08:22:27 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|cImport_builtin.zig#}
2017-11-07 08:22:27 +00:00
< p >
2018-09-14 14:35:03 +00:00
The {#syntax#}@cImport{#endsyntax#} function takes an expression as a parameter.
2017-11-07 08:22:27 +00:00
This expression is evaluated at compile-time and is used to control
2021-08-11 09:01:03 +00:00
preprocessor directives and include multiple < code class = "file" > .h< / code > files:
2017-11-07 08:22:27 +00:00
< / p >
2021-09-20 08:32:34 +00:00
{#syntax_block|zig|@cImport Expression#}
2018-01-19 08:03:20 +00:00
const builtin = @import("builtin");
2017-11-07 08:22:27 +00:00
const c = @cImport({
2020-03-23 23:00:52 +00:00
@cDefine("NDEBUG", builtin.mode == .ReleaseFast);
2017-11-07 08:22:27 +00:00
if (something) {
@cDefine("_GNU_SOURCE", {});
}
2018-01-19 08:03:20 +00:00
@cInclude("stdlib.h");
2017-11-07 08:22:27 +00:00
if (something) {
@cUndef("_GNU_SOURCE");
}
@cInclude("soundio.h");
2018-01-19 08:03:20 +00:00
});
2021-09-20 08:32:34 +00:00
{#end_syntax_block#}
2018-01-17 05:22:33 +00:00
{#see_also|@cImport|@cInclude|@cDefine|@cUndef|@import#}
2018-08-27 21:44:58 +00:00
{#header_close#}
2019-02-14 23:59:20 +00:00
2021-06-27 09:25:32 +00:00
{#header_open|C Translation CLI#}
2023-01-25 08:43:26 +00:00
< p >
2021-08-11 09:01:03 +00:00
Zig's C translation capability is available as a CLI tool via < kbd > zig translate-c< / kbd > .
2021-06-27 09:25:32 +00:00
It requires a single filename as an argument. It may also take a set of optional flags that are
forwarded to clang. It writes the translated file to stdout.
2023-01-25 08:43:26 +00:00
< / p >
2021-06-27 09:25:32 +00:00
{#header_open|Command line flags#}
< ul >
< li >
2021-08-11 09:01:03 +00:00
< kbd > -I< / kbd > :
2021-06-27 09:25:32 +00:00
Specify a search directory for include files. May be used multiple times. Equivalent to
< a href = "https://releases.llvm.org/12.0.0/tools/clang/docs/ClangCommandLineReference.html#cmdoption-clang-i-dir" >
2021-08-11 09:01:03 +00:00
clang's < kbd > -I< / kbd > flag< / a > . The current directory is < em > not< / em > included by default;
use < kbd > -I.< / kbd > to include it.
2021-06-27 09:25:32 +00:00
< / li >
< li >
2021-08-11 09:01:03 +00:00
< kbd > -D< / kbd > : Define a preprocessor macro. Equivalent to
2021-06-27 09:25:32 +00:00
< a href = "https://releases.llvm.org/12.0.0/tools/clang/docs/ClangCommandLineReference.html#cmdoption-clang-d-macro" >
2021-08-11 09:01:03 +00:00
clang's < kbd > -D< / kbd > flag< / a > .
2021-06-27 09:25:32 +00:00
< / li >
< li >
2021-08-11 09:01:03 +00:00
< kbd > -cflags [flags] --< / kbd > : Pass arbitrary additional
2021-06-27 09:25:32 +00:00
< a href = "https://releases.llvm.org/12.0.0/tools/clang/docs/ClangCommandLineReference.html" > command line
2021-08-11 09:01:03 +00:00
flags< / a > to clang. Note: the list of flags must end with < kbd > --< / kbd >
2021-06-27 09:25:32 +00:00
< / li >
< li >
2021-08-11 09:01:03 +00:00
< kbd > -target< / kbd > : The {#link|target triple|Targets#} for the translated Zig code.
2021-06-27 09:25:32 +00:00
If no target is specified, the current host target will be used.
< / li >
< / ul >
{#header_close#}
{#header_open|Using -target and -cflags#}
< p >
2021-08-11 09:01:03 +00:00
< strong > Important!< / strong > When translating C code with < kbd > zig translate-c< / kbd > ,
you < strong > must< / strong > use the same < kbd > -target< / kbd > triple that you will use when compiling
the translated code. In addition, you < strong > must< / strong > ensure that the < kbd > -cflags< / kbd > used,
if any, match the cflags used by code on the target system. Using the incorrect < kbd > -target< / kbd >
or < kbd > -cflags< / kbd > could result in clang or Zig parse failures, or subtle ABI incompatibilities
2021-06-27 09:25:32 +00:00
when linking with C code.
< / p >
2023-01-19 11:33:00 +00:00
{#syntax_block|c|varytarget.h#}
long FOO = __LONG_MAX__;
{#end_syntax_block#}
2022-12-09 19:52:01 +00:00
{#shell_samp#}$ zig translate-c -target thumb-freestanding-gnueabihf varytarget.h|grep FOO
pub export var FOO: c_long = 2147483647;
$ zig translate-c -target x86_64-macos-gnu varytarget.h|grep FOO
pub export var FOO: c_long = 9223372036854775807;{#end_shell_samp#}
2023-01-19 11:33:00 +00:00
{#syntax_block|c|varycflags.h#}
enum FOO { BAR };
2021-08-11 09:01:03 +00:00
int do_something(enum FOO foo);
{#end_syntax_block#}
{#shell_samp#}$ zig translate-c varycflags.h|grep -B1 do_something
2022-12-09 19:52:01 +00:00
pub const enum_FOO = c_uint;
2021-08-11 09:01:03 +00:00
pub extern fn do_something(foo: enum_FOO) c_int;
2022-12-09 19:52:01 +00:00
$ zig translate-c -cflags -fshort-enums -- varycflags.h|grep -B1 do_something
pub const enum_FOO = u8;
2021-08-11 09:01:03 +00:00
pub extern fn do_something(foo: enum_FOO) c_int;{#end_shell_samp#}
2021-06-27 09:25:32 +00:00
{#header_close#}
{#header_open|@cImport vs translate-c#}
2021-08-11 09:01:03 +00:00
< p > {#syntax#}@cImport{#endsyntax#} and < kbd > zig translate-c< / kbd > use the same underlying
2021-06-27 09:25:32 +00:00
C translation functionality, so on a technical level they are equivalent. In practice,
{#syntax#}@cImport{#endsyntax#} is useful as a way to quickly and easily access numeric constants, typedefs,
and record types without needing any extra setup. If you need to pass {#link|cflags|Using -target and -cflags#}
to clang, or if you would like to edit the translated code, it is recommended to use
2021-08-11 09:01:03 +00:00
< kbd > zig translate-c< / kbd > and save the results to a file. Common reasons for editing
2021-06-27 09:25:32 +00:00
the generated code include: changing {#syntax#}anytype{#endsyntax#} parameters in function-like macros to more
specific types; changing {#syntax#}[*c]T{#endsyntax#} pointers to {#syntax#}[*]T{#endsyntax#} or
{#syntax#}*T{#endsyntax#} pointers for improved type safety; and
{#link|enabling or disabling runtime safety|@setRuntimeSafety#} within specific functions.
< / p >
{#header_close#}
{#see_also|Targets|C Type Primitives|Pointers|C Pointers|Import from C Header File|@cInclude|@cImport|@setRuntimeSafety#}
{#header_close#}
{#header_open|C Translation Caching#}
< p >
2021-08-11 09:01:03 +00:00
The C translation feature (whether used via < kbd > zig translate-c< / kbd > or
2021-06-27 09:25:32 +00:00
{#syntax#}@cImport{#endsyntax#}) integrates with the Zig caching system. Subsequent runs with
the same source file, target, and cflags will use the cache instead of repeatedly translating
the same code.
< / p >
< p >
To see where the cached files are stored when compiling code that uses {#syntax#}@cImport{#endsyntax#},
2021-08-11 09:01:03 +00:00
use the < kbd > --verbose-cimport< / kbd > flag:
2021-06-27 09:25:32 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|verbose_cimport_flag.zig#}
2021-06-27 09:25:32 +00:00
< p >
2021-08-11 09:01:03 +00:00
< code class = "file" > cimport.h< / code > contains the file to translate (constructed from calls to
2021-06-27 09:25:32 +00:00
{#syntax#}@cInclude{#endsyntax#}, {#syntax#}@cDefine{#endsyntax#}, and {#syntax#}@cUndef{#endsyntax#}),
2021-08-11 09:01:03 +00:00
< code class = "file" > cimport.h.d< / code > is the list of file dependencies, and
< code class = "file" > cimport.zig< / code > contains the translated output.
2021-06-27 09:25:32 +00:00
< / p >
{#see_also|Import from C Header File|C Translation CLI|@cInclude|@cImport#}
{#header_close#}
{#header_open|Translation failures#}
< p >
Some C constructs cannot be translated to Zig - for example, < em > goto< / em > ,
structs with bitfields, and token-pasting macros. Zig employs < em > demotion< / em > to allow translation
2021-09-24 17:39:20 +00:00
to continue in the face of non-translatable entities.
2021-06-27 09:25:32 +00:00
< / p >
< p >
Demotion comes in three varieties - {#link|opaque#}, < em > extern< / em > , and
{#syntax#}@compileError{#endsyntax#}.
C structs and unions that cannot be translated correctly will be translated as {#syntax#}opaque{}{#endsyntax#}.
Functions that contain opaque types or code constructs that cannot be translated will be demoted
to {#syntax#}extern{#endsyntax#} declarations.
2021-09-24 17:39:20 +00:00
Thus, non-translatable types can still be used as pointers, and non-translatable functions
2021-06-27 09:25:32 +00:00
can be called so long as the linker is aware of the compiled function.
< / p >
< p >
{#syntax#}@compileError{#endsyntax#} is used when top-level definitions (global variables,
function prototypes, macros) cannot be translated or demoted. Since Zig uses lazy analysis for
2021-09-24 17:39:20 +00:00
top-level declarations, untranslatable entities will not cause a compile error in your code unless
2021-06-27 09:25:32 +00:00
you actually use them.
< / p >
{#see_also|opaque|extern|@compileError#}
{#header_close#}
{#header_open|C Macros#}
< p >
C Translation makes a best-effort attempt to translate function-like macros into equivalent
Zig functions. Since C macros operate at the level of lexical tokens, not all C macros
2022-09-24 18:42:00 +00:00
can be translated to Zig. Macros that cannot be translated will be demoted to
2021-06-27 09:25:32 +00:00
{#syntax#}@compileError{#endsyntax#}. Note that C code which < em > uses< / em > macros will be
translated without any additional issues (since Zig operates on the pre-processed source
2021-09-24 17:39:20 +00:00
with macros expanded). It is merely the macros themselves which may not be translatable to
2021-06-27 09:25:32 +00:00
Zig.
< / p >
< p > Consider the following example:< / p >
2023-01-19 11:33:00 +00:00
{#syntax_block|c|macro.c#}
#define MAKELOCAL(NAME, INIT) int NAME = INIT
2021-06-27 09:25:32 +00:00
int foo(void) {
MAKELOCAL(a, 1);
MAKELOCAL(b, 2);
return a + b;
2021-08-11 09:01:03 +00:00
}
{#end_syntax_block#}
{#shell_samp#}$ zig translate-c macro.c > macro.zig{#end_shell_samp#}
2024-04-25 00:41:47 +00:00
{#code|macro.zig#}
2021-09-24 17:39:20 +00:00
< p > Note that {#syntax#}foo{#endsyntax#} was translated correctly despite using a non-translatable
2021-06-27 09:25:32 +00:00
macro. {#syntax#}MAKELOCAL{#endsyntax#} was demoted to {#syntax#}@compileError{#endsyntax#} since
it cannot be expressed as a Zig function; this simply means that you cannot directly use
{#syntax#}MAKELOCAL{#endsyntax#} from Zig.
< / p >
{#see_also|@compileError#}
{#header_close#}
2019-02-14 23:59:20 +00:00
{#header_open|C Pointers#}
< p >
This type is to be avoided whenever possible. The only valid reason for using a C pointer is in
auto-generated code from translating C code.
< / p >
< p >
When importing C header files, it is ambiguous whether pointers should be translated as
2021-03-13 01:10:55 +00:00
single-item pointers ({#syntax#}*T{#endsyntax#}) or many-item pointers ({#syntax#}[*]T{#endsyntax#}).
2019-02-14 23:59:20 +00:00
C pointers are a compromise so that Zig code can utilize translated header files directly.
< / p >
< p > {#syntax#}[*c]T{#endsyntax#} - C pointer.< / p >
< ul >
2023-04-23 18:06:21 +00:00
< li > Supports all the syntax of the other two pointer types ({#syntax#}*T{#endsyntax#}) and ({#syntax#}[*]T{#endsyntax#}).< / li >
2019-11-08 20:56:21 +00:00
< li > Coerces to other pointer types, as well as {#link|Optional Pointers#}.
When a C pointer is coerced to a non-optional pointer, safety-checked
2019-02-14 23:59:20 +00:00
{#link|Undefined Behavior#} occurs if the address is 0.
< / li >
< li > Allows address 0. On non-freestanding targets, dereferencing address 0 is safety-checked
2019-02-22 15:56:49 +00:00
{#link|Undefined Behavior#}. Optional C pointers introduce another bit to keep track of
2019-02-14 23:59:20 +00:00
null, just like {#syntax#}?usize{#endsyntax#}. Note that creating an optional C pointer
is unnecessary as one can use normal {#link|Optional Pointers#}.
< / li >
2019-11-08 20:56:21 +00:00
< li > Supports {#link|Type Coercion#} to and from integers.< / li >
2019-02-14 23:59:20 +00:00
< li > Supports comparison with integers.< / li >
< li > Does not support Zig-only pointer attributes such as alignment. Use normal {#link|Pointers#}
please!< / li >
< / ul >
2020-09-16 02:47:27 +00:00
< p > When a C pointer is pointing to a single struct (not an array), dereference the C pointer to
2022-09-24 18:42:00 +00:00
access the struct's fields or member data. That syntax looks like
2019-08-19 20:47:16 +00:00
this: < / p >
< p > {#syntax#}ptr_to_struct.*.struct_member{#endsyntax#}< / p >
< p > This is comparable to doing {#syntax#}->{#endsyntax#} in C.< / p >
< p > When a C pointer is pointing to an array of structs, the syntax reverts to this:< / p >
< p > {#syntax#}ptr_to_struct_array[index].struct_member{#endsyntax#}< / p >
2019-02-14 23:59:20 +00:00
{#header_close#}
2022-12-12 10:32:48 +00:00
{#header_open|C Variadic Functions#}
< p > Zig supports extern variadic functions.< / p >
2024-04-25 00:41:47 +00:00
{#code|test_variadic_function.zig#}
2022-12-12 10:32:48 +00:00
< p >
2023-01-25 15:58:50 +00:00
Variadic functions can be implemented using {#link|@cVaStart#}, {#link|@cVaEnd#}, {#link|@cVaArg#} and {#link|@cVaCopy#}.
2022-12-12 10:32:48 +00:00
< / p >
2024-04-25 00:41:47 +00:00
{#code|test_defining_variadic_function.zig#}
2022-12-12 22:14:54 +00:00
2022-12-12 10:32:48 +00:00
{#header_close#}
2018-08-27 21:44:58 +00:00
{#header_open|Exporting a C Library#}
< p >
One of the primary use cases for Zig is exporting a library with the C ABI for other programming languages
2018-09-14 14:35:03 +00:00
to call into. The {#syntax#}export{#endsyntax#} keyword in front of functions, variables, and types causes them to
2018-08-27 21:44:58 +00:00
be part of the library API:
< / p >
2024-04-25 00:41:47 +00:00
{#code|mathtest.zig#}
2019-05-02 15:59:49 +00:00
< p > To make a static library:< / p >
2021-08-11 09:01:03 +00:00
{#shell_samp#}$ zig build-lib mathtest.zig{#end_shell_samp#}
2019-05-02 15:59:49 +00:00
< p > To make a shared library:< / p >
2021-08-11 09:01:03 +00:00
{#shell_samp#}$ zig build-lib mathtest.zig -dynamic{#end_shell_samp#}
2018-08-27 21:44:58 +00:00
< p > Here is an example with the {#link|Zig Build System#}:< / p >
2023-01-19 11:33:00 +00:00
{#syntax_block|c|test.c#}
// This header is generated by zig from mathtest.zig
2018-08-27 21:44:58 +00:00
#include "mathtest.h"
2021-08-11 09:01:03 +00:00
#include < stdio.h >
2018-08-27 21:44:58 +00:00
int main(int argc, char **argv) {
2019-05-02 15:59:49 +00:00
int32_t result = add(42, 1337);
printf("%d\n", result);
2018-08-27 21:44:58 +00:00
return 0;
2021-08-11 09:01:03 +00:00
}
{#end_syntax_block#}
2024-04-25 00:41:47 +00:00
{#code|build_c.zig#}
2021-08-11 09:01:03 +00:00
{#shell_samp#}$ zig build test
1379{#end_shell_samp#}
2019-03-13 15:54:56 +00:00
{#see_also|export#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Mixing Object Files#}
2017-11-07 08:22:27 +00:00
< p >
You can mix Zig object files with any other object files that respect the C ABI. Example:
< / p >
2024-04-25 00:41:47 +00:00
{#code|base64.zig#}
2023-01-19 11:33:00 +00:00
{#syntax_block|c|test.c#}
// This header is generated by zig from base64.zig
2017-11-07 08:22:27 +00:00
#include "base64.h"
2021-08-11 09:01:03 +00:00
#include < string.h >
#include < stdio.h >
2017-11-07 08:22:27 +00:00
int main(int argc, char **argv) {
const char *encoded = "YWxsIHlvdXIgYmFzZSBhcmUgYmVsb25nIHRvIHVz";
char buf[200];
size_t len = decode_base_64(buf, 200, encoded, strlen(encoded));
buf[len] = 0;
puts(buf);
return 0;
2021-08-11 09:01:03 +00:00
}
{#end_syntax_block#}
2024-04-25 00:41:47 +00:00
{#code|build_object.zig#}
2021-08-11 09:01:03 +00:00
{#shell_samp#}$ zig build
2021-04-30 22:41:59 +00:00
$ ./zig-out/bin/test
2021-08-11 09:01:03 +00:00
all your base are belong to us{#end_shell_samp#}
2018-01-17 05:22:33 +00:00
{#see_also|Targets|Zig Build System#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_close#}
2019-05-01 22:48:26 +00:00
{#header_open|WebAssembly#}
2019-11-26 00:30:41 +00:00
< p > Zig supports building for WebAssembly out of the box.< / p >
2019-05-01 22:48:26 +00:00
{#header_open|Freestanding#}
2023-11-01 14:56:30 +00:00
< p > For host environments like the web browser and nodejs, build as an executable using the freestanding
2021-05-21 18:24:13 +00:00
OS target. Here's an example of running Zig code compiled to WebAssembly with nodejs.< / p >
2024-04-25 00:41:47 +00:00
{#code|math.zig#}
2023-01-19 11:33:00 +00:00
{#syntax_block|javascript|test.js#}
const fs = require('fs');
2019-05-20 02:26:10 +00:00
const source = fs.readFileSync("./math.wasm");
const typedArray = new Uint8Array(source);
WebAssembly.instantiate(typedArray, {
env: {
2021-09-27 02:34:59 +00:00
print: (result) => { console.log(`The result is ${result}`); }
}}).then(result => {
2019-05-20 02:26:10 +00:00
const add = result.instance.exports.add;
add(1, 2);
2023-01-19 11:33:00 +00:00
});
{#end_syntax_block#}
{#shell_samp#}$ node test.js
2021-08-11 09:01:03 +00:00
The result is 3{#end_shell_samp#}
{#header_close#}
2019-05-01 22:48:26 +00:00
{#header_open|WASI#}
2020-02-27 19:41:44 +00:00
< p > Zig's support for WebAssembly System Interface (WASI) is under active development.
Example of using the standard library and reading command line arguments:< / p >
2024-04-25 00:41:47 +00:00
{#code|wasi_args.zig#}
2023-05-15 14:37:56 +00:00
{#shell_samp#}$ wasmtime wasi_args.wasm 123 hello
0: wasi_args.wasm
2019-05-20 02:26:10 +00:00
1: 123
2021-08-11 09:01:03 +00:00
2: hello{#end_shell_samp#}
2020-06-11 20:06:57 +00:00
< p > A more interesting example would be extracting the list of preopens from the runtime.
2024-03-18 21:15:06 +00:00
This is now supported in the standard library via {#syntax#}std.fs.wasi.Preopens{#endsyntax#}:< / p >
2024-04-25 00:41:47 +00:00
{#code|wasi_preopens.zig#}
2020-06-11 20:06:57 +00:00
2023-05-15 14:37:56 +00:00
{#shell_samp#}$ wasmtime --dir=. wasi_preopens.wasm
2022-12-06 00:21:33 +00:00
0: stdin
1: stdout
2: stderr
3: .
2021-08-11 09:01:03 +00:00
{#end_shell_samp#}
2019-05-01 22:48:26 +00:00
{#header_close#}
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|Targets#}
2017-11-07 08:22:27 +00:00
< p >
2024-01-09 01:48:10 +00:00
< strong > Target< / strong > refers to the computer that will be used to run an executable.
It is composed of the CPU architecture, the set of enabled CPU features, operating system,
minimum and maximum operating system version, ABI, and ABI version.
< / p >
< p >
Zig is a general-purpose programming language which means that it is designed to
generate optimal code for a large set of targets. The command < code > zig targets< / code >
provides information about all of the targets the compiler is aware of.< / p >
< p > When no target option is provided to the compiler, the default choice
is to target the < strong > host computer< / strong > , meaning that the
resulting executable will be < em > unsuitable for copying to a different
computer< / em > . In order to copy an executable to another computer, the compiler
needs to know about the target requirements via the < code > -target< / code > option.
< / p >
< p >
The Zig Standard Library ({#syntax#}@import("std"){#endsyntax#}) has
cross-platform abstractions, making the same source code viable on many targets.
Some code is more portable than other code. In general, Zig code is extremely
portable compared to other programming languages.
< / p >
< p >
Each platform requires its own implementations to make Zig's
cross-platform abstractions work. These implementations are at various
degrees of completion. Each tagged release of the compiler comes with
release notes that provide the full support table for each target.
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Style Guide#}
2017-11-07 08:22:27 +00:00
< p >
These coding conventions are not enforced by the compiler, but they are shipped in
this documentation along with the compiler in order to provide a point of
reference, should anyone wish to point to an authority on agreed upon Zig
coding style.
< / p >
2024-03-19 06:36:10 +00:00
{#header_open|Avoid Redundancy in Names#}
< p > Avoid these words in type names:< / p >
< ul >
< li > Value< / li >
< li > Data< / li >
< li > Context< / li >
< li > Manager< / li >
< li > utils, misc, or somebody's initials< / li >
< / ul >
< p > Everything is a value, all types are data, everything is context, all logic manages state.
2024-07-09 21:25:42 +00:00
Nothing is communicated by using a word that applies to all types.< / p >
2024-03-19 06:36:10 +00:00
< p > Temptation to use "utilities", "miscellaneous", or somebody's initials
is a failure to categorize, or more commonly, overcategorization. Such
declarations can live at the root of a module that needs them with no
namespace needed.< / p >
{#header_close#}
{#header_open|Avoid Redundant Names in Fully-Qualified Namespaces#}
< p > Every declaration is assigned a < strong > fully qualified
namespace< / strong > by the compiler, creating a tree structure. Choose names based
on the fully-qualified namespace, and avoid redundant name segments.< / p >
2024-04-25 00:41:47 +00:00
{#code|redundant_fqn.zig#}
2024-03-19 06:36:10 +00:00
< p > In this example, "json" is repeated in the fully-qualified namespace. The solution
is to delete < code > Json< / code > from < code > JsonValue< / code > . In this example we have
an empty struct named < code > json< / code > but remember that files also act
as part of the fully-qualified namespace.< / p >
< p > This example is an exception to the rule specified in {#link|Avoid Redundancy in Names#}.
The meaning of the type has been reduced to its core: it is a json value. The name
cannot be any more specific without being incorrect.< / p >
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|Whitespace#}
2017-11-07 08:22:27 +00:00
< ul >
< li >
4 space indentation
< / li >
< li >
Open braces on same line, unless you need to wrap.
< / li >
< li > If a list of things is longer than 2, put each item on its own line and
2018-11-16 18:33:58 +00:00
exercise the ability to put an extra comma at the end.
2017-11-07 08:22:27 +00:00
< / li >
< li >
Line length: aim for 100; use common sense.
< / li >
< / ul >
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Names#}
2017-11-07 08:22:27 +00:00
< p >
2018-09-14 14:35:03 +00:00
Roughly speaking: {#syntax#}camelCaseFunctionName{#endsyntax#}, {#syntax#}TitleCaseTypeName{#endsyntax#},
{#syntax#}snake_case_variable_name{#endsyntax#}. More precisely:
2017-11-07 08:22:27 +00:00
< / p >
< ul >
< li >
2019-02-28 15:11:32 +00:00
If {#syntax#}x{#endsyntax#} is a {#syntax#}type{#endsyntax#}
then {#syntax#}x{#endsyntax#} should be {#syntax#}TitleCase{#endsyntax#}, unless it
is a {#syntax#}struct{#endsyntax#} with 0 fields and is never meant to be instantiated,
2019-03-01 22:23:27 +00:00
in which case it is considered to be a "namespace" and uses {#syntax#}snake_case{#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / li >
< li >
2019-02-28 15:11:32 +00:00
If {#syntax#}x{#endsyntax#} is callable, and {#syntax#}x{#endsyntax#}'s return type is
{#syntax#}type{#endsyntax#}, then {#syntax#}x{#endsyntax#} should be {#syntax#}TitleCase{#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / li >
< li >
2019-02-28 15:11:32 +00:00
If {#syntax#}x{#endsyntax#} is otherwise callable, then {#syntax#}x{#endsyntax#} should
be {#syntax#}camelCase{#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / li >
< li >
2018-09-14 14:35:03 +00:00
Otherwise, {#syntax#}x{#endsyntax#} should be {#syntax#}snake_case{#endsyntax#}.
2017-11-07 08:22:27 +00:00
< / li >
< / ul >
< p >
Acronyms, initialisms, proper nouns, or any other word that has capitalization
rules in written English are subject to naming conventions just like any other
word. Even acronyms that are only 2 letters long are subject to these
conventions.
< / p >
< p >
2020-05-02 21:28:44 +00:00
File names fall into two categories: types and namespaces. If the file
2021-06-03 22:04:58 +00:00
(implicitly a struct) has top level fields, it should be named like any
2021-08-11 09:01:03 +00:00
other struct with fields using < code class = "file" > TitleCase< / code > . Otherwise,
it should use < code class = "file" > snake_case< / code > . Directory names should be
< code class = "file" > snake_case< / code > .
2020-05-02 21:28:44 +00:00
< / p >
< p >
2017-11-07 08:22:27 +00:00
These are general rules of thumb; if it makes sense to do something different,
do what makes sense. For example, if there is an established convention such as
2018-09-14 14:35:03 +00:00
{#syntax#}ENOENT{#endsyntax#}, follow the established convention.
2017-11-07 08:22:27 +00:00
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Examples#}
2021-08-11 09:01:03 +00:00
{#syntax_block|zig|style_example.zig#}
2018-01-19 08:03:20 +00:00
const namespace_name = @import("dir_name/file_name.zig");
2020-05-02 21:28:44 +00:00
const TypeName = @import("dir_name/TypeName.zig");
2017-11-07 08:22:27 +00:00
var global_var: i32 = undefined;
const const_name = 42;
const primitive_type_alias = f32;
const string_alias = []u8;
2019-02-28 15:11:32 +00:00
const StructName = struct {
field: i32,
};
2017-11-07 08:22:27 +00:00
const StructAlias = StructName;
2018-01-25 09:10:11 +00:00
fn functionName(param_name: TypeName) void {
2017-11-07 08:22:27 +00:00
var functionPointer = functionName;
functionPointer();
functionPointer = otherFunction;
functionPointer();
}
const functionAlias = functionName;
2018-01-25 09:10:11 +00:00
fn ListTemplateFunction(comptime ChildType: type, comptime fixed_size: usize) type {
2017-11-07 08:22:27 +00:00
return List(ChildType, fixed_size);
}
2018-01-25 09:10:11 +00:00
fn ShortList(comptime T: type, comptime n: usize) type {
2018-11-13 13:08:37 +00:00
return struct {
2017-11-07 08:22:27 +00:00
field_name: [n]T,
2018-01-25 09:10:11 +00:00
fn methodName() void {}
2018-01-19 08:03:20 +00:00
};
2017-11-07 08:22:27 +00:00
}
// The word XML loses its casing when used in Zig identifiers.
const xml_document =
2018-01-19 08:03:20 +00:00
\\<?xml version="1.0" encoding="UTF-8"?>
\\< document >
\\< / document >
2017-11-07 08:22:27 +00:00
;
2019-02-28 15:11:32 +00:00
const XmlParser = struct {
field: i32,
};
2017-11-07 08:22:27 +00:00
// The initials BE (Big Endian) are just another word in Zig identifier names.
2018-01-25 09:10:11 +00:00
fn readU32Be() u32 {}
2021-08-11 09:01:03 +00:00
{#end_syntax_block#}
2017-11-07 08:22:27 +00:00
< p >
2021-08-11 09:01:03 +00:00
See the {#link|Zig Standard Library#} for more examples.
2017-11-07 08:22:27 +00:00
< / p >
2018-01-17 04:19:05 +00:00
{#header_close#}
2021-06-28 23:46:37 +00:00
{#header_open|Doc Comment Guidance#}
< ul >
< li > Omit any information that is redundant based on the name of the thing being documented.< / li >
< li > Duplicating information onto multiple similar functions is encouraged because it helps IDEs and other tools provide better help text.< / li >
< li > Use the word < strong > assume< / strong > to indicate invariants that cause {#link|Undefined Behavior#} when violated.< / li >
< li > Use the word < strong > assert< / strong > to indicate invariants that cause < em > safety-checked< / em > {#link|Undefined Behavior#} when violated.< / li >
< / ul >
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_close#}
2018-02-01 01:42:27 +00:00
{#header_open|Source Encoding#}
< p > Zig source code is encoded in UTF-8. An invalid UTF-8 byte sequence results in a compile error.< / p >
2021-03-03 19:30:46 +00:00
< p > Throughout all zig source code (including in comments), some code points are never allowed:< / p >
2018-02-01 01:42:27 +00:00
< ul >
2021-03-03 19:30:46 +00:00
< li > Ascii control characters, except for U+000a (LF), U+000d (CR), and U+0009 (HT): U+0000 - U+0008, U+000b - U+000c, U+000e - U+0001f, U+007f.< / li >
2018-02-01 01:42:27 +00:00
< li > Non-Ascii Unicode line endings: U+0085 (NEL), U+2028 (LS), U+2029 (PS).< / li >
< / ul >
2021-03-03 19:30:46 +00:00
< p >
LF (byte value 0x0a, code point U+000a, {#syntax#}'\n'{#endsyntax#}) is the line terminator in Zig source code.
This byte value terminates every line of zig source code except the last line of the file.
It is recommended that non-empty source files end with an empty line, which means the last byte would be 0x0a (LF).
< / p >
< p >
Each LF may be immediately preceded by a single CR (byte value 0x0d, code point U+000d, {#syntax#}'\r'{#endsyntax#})
2023-10-23 05:43:48 +00:00
to form a Windows style line ending, but this is discouraged. Note that in multiline strings, CRLF sequences will
2023-02-19 12:14:03 +00:00
be encoded as LF when compiled into a zig program.
2021-03-03 19:30:46 +00:00
A CR in any other context is not allowed.
< / p >
< p >
HT hard tabs (byte value 0x09, code point U+0009, {#syntax#}'\t'{#endsyntax#}) are interchangeable with
SP spaces (byte value 0x20, code point U+0020, {#syntax#}' '{#endsyntax#}) as a token separator,
but use of hard tabs is discouraged. See {#link|Grammar#}.
< / p >
< p >
2023-01-16 18:14:43 +00:00
For compatibility with other tools, the compiler ignores a UTF-8-encoded byte order mark (U+FEFF)
if it is the first Unicode code point in the source text. A byte order mark is not allowed anywhere else in the source.
< / p >
< p >
2021-08-11 09:01:03 +00:00
Note that running < kbd > zig fmt< / kbd > on a source file will implement all recommendations mentioned here.
2021-03-03 19:30:46 +00:00
< / p >
< p >
Note that a tool reading Zig source code can make assumptions if the source code is assumed to be correct Zig code.
For example, when identifying the ends of lines, a tool can use a naive search such as < code > /\n/< / code > ,
or an < a href = "https://msdn.microsoft.com/en-us/library/dd409797.aspx" > advanced< / a >
search such as < code > /\r\n?|[\n\u0085\u2028\u2029]/< / code > , and in either case line endings will be correctly identified.
For another example, when identifying the whitespace before the first token on a line,
a tool can either use a naive search such as < code > /[ \t]/< / code > ,
or an < a href = "https://tc39.es/ecma262/#sec-characterclassescape" > advanced< / a > search such as < code > /\s/< / code > ,
and in either case whitespace will be correctly identified.
< / p >
2018-02-01 01:42:27 +00:00
{#header_close#}
2019-03-13 17:04:55 +00:00
{#header_open|Keyword Reference#}
2020-08-24 22:29:53 +00:00
< div class = "table-wrapper" >
2020-08-24 15:59:55 +00:00
< table >
2021-08-11 09:01:03 +00:00
< thead >
2020-08-24 15:59:55 +00:00
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "col" > Keyword< / th >
< th scope = "col" > Description< / th >
2020-08-24 15:59:55 +00:00
< / tr >
2021-08-11 09:01:03 +00:00
< / thead >
< tbody >
2023-01-16 18:32:24 +00:00
< tr >
< th scope = "row" >
< pre > {#syntax#}addrspace{#endsyntax#}< / pre >
< / th >
< td >
The {#syntax#}addrspace{#endsyntax#} keyword.
< ul >
< li > TODO add documentation for addrspace< / li >
< / ul >
< / td >
< / tr >
2020-08-24 15:59:55 +00:00
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}align{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
2020-08-24 18:12:23 +00:00
{#syntax#}align{#endsyntax#} can be used to specify the alignment of a pointer.
It can also be used after a variable or function declaration to specify the alignment of pointers to that variable or function.
2020-08-24 15:59:55 +00:00
< ul >
< li > See also {#link|Alignment#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}allowzero{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
The pointer attribute {#syntax#}allowzero{#endsyntax#} allows a pointer to have address zero.
< ul >
< li > See also {#link|allowzero#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}and{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
The boolean operator {#syntax#}and{#endsyntax#}.
< ul >
< li > See also {#link|Operators#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}anyframe{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
{#syntax#}anyframe{#endsyntax#} can be used as a type for variables which hold pointers to function frames.
< ul >
< li > See also {#link|Async Functions#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}anytype{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
2022-08-27 14:44:05 +00:00
Function parameters can be declared with {#syntax#}anytype{#endsyntax#} in place of the type.
The type will be inferred where the function is called.
2020-08-24 15:59:55 +00:00
< ul >
< li > See also {#link|Function Parameter Type Inference#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}asm{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
{#syntax#}asm{#endsyntax#} begins an inline assembly expression. This allows for directly controlling the machine code generated on compilation.
< ul >
< li > See also {#link|Assembly#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}async{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
{#syntax#}async{#endsyntax#} can be used before a function call to get a pointer to the function's frame when it suspends.
< ul >
< li > See also {#link|Async Functions#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}await{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
2020-08-24 18:12:23 +00:00
{#syntax#}await{#endsyntax#} can be used to suspend the current function until the frame provided after the {#syntax#}await{#endsyntax#} completes.
{#syntax#}await{#endsyntax#} copies the value returned from the target function's frame to the caller.
2020-08-24 15:59:55 +00:00
< ul >
< li > See also {#link|Async Functions#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}break{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
2020-08-24 18:12:23 +00:00
{#syntax#}break{#endsyntax#} can be used with a block label to return a value from the block.
It can also be used to exit a loop before iteration completes naturally.
2020-08-24 15:59:55 +00:00
< ul >
2022-10-30 23:27:02 +00:00
< li > See also {#link|Blocks#}, {#link|while#}, {#link|for#}< / li >
2020-08-24 15:59:55 +00:00
< / ul >
< / td >
< / tr >
2023-09-06 16:10:24 +00:00
< tr >
< th scope = "row" >
< pre > {#syntax#}callconv{#endsyntax#}< / pre >
< / th >
< td >
{#syntax#}callconv{#endsyntax#} can be used to specify the calling convention in a function type.
< ul >
< li > See also {#link|Functions#}< / li >
< / ul >
< / td >
< / tr >
2020-08-24 15:59:55 +00:00
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}catch{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
2020-08-24 18:12:23 +00:00
{#syntax#}catch{#endsyntax#} can be used to evaluate an expression if the expression before it evaluates to an error.
The expression after the {#syntax#}catch{#endsyntax#} can optionally capture the error value.
2020-08-24 15:59:55 +00:00
< ul >
< li > See also {#link|catch#}, {#link|Operators#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}comptime{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
{#syntax#}comptime{#endsyntax#} before a declaration can be used to label variables or function parameters as known at compile time.
It can also be used to guarantee an expression is run at compile time.
< ul >
< li > See also {#link|comptime#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}const{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
{#syntax#}const{#endsyntax#} declares a variable that can not be modified.
2020-08-24 18:12:23 +00:00
Used as a pointer attribute, it denotes the value referenced by the pointer cannot be modified.
2020-08-24 15:59:55 +00:00
< ul >
< li > See also {#link|Variables#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}continue{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
{#syntax#}continue{#endsyntax#} can be used in a loop to jump back to the beginning of the loop.
< ul >
< li > See also {#link|while#}, {#link|for#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}defer{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
2020-08-24 18:12:23 +00:00
{#syntax#}defer{#endsyntax#} will execute an expression when control flow leaves the current block.
2020-08-24 15:59:55 +00:00
< ul >
< li > See also {#link|defer#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}else{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
2020-08-24 18:12:23 +00:00
{#syntax#}else{#endsyntax#} can be used to provide an alternate branch for {#syntax#}if{#endsyntax#}, {#syntax#}switch{#endsyntax#},
{#syntax#}while{#endsyntax#}, and {#syntax#}for{#endsyntax#} expressions.
2020-08-24 15:59:55 +00:00
< ul >
< li > If used after an if expression, the else branch will be executed if the test value returns false, null, or an error.< / li >
< li > If used within a switch expression, the else branch will be executed if the test value matches no other cases.< / li >
< li > If used after a loop expression, the else branch will be executed if the loop finishes without breaking.< / li >
< li > See also {#link|if#}, {#link|switch#}, {#link|while#}, {#link|for#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}enum{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
2020-08-24 18:12:23 +00:00
{#syntax#}enum{#endsyntax#} defines an enum type.
2020-08-24 15:59:55 +00:00
< ul >
< li > See also {#link|enum#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}errdefer{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
2022-05-17 19:03:42 +00:00
{#syntax#}errdefer{#endsyntax#} will execute an expression when control flow leaves the current block if the function returns an error, the errdefer expression can capture the unwrapped value.
2020-08-24 15:59:55 +00:00
< ul >
< li > See also {#link|errdefer#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}error{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
2020-08-24 18:12:23 +00:00
{#syntax#}error{#endsyntax#} defines an error type.
2020-08-24 15:59:55 +00:00
< ul >
< li > See also {#link|Errors#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}export{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
2020-08-24 18:12:23 +00:00
{#syntax#}export{#endsyntax#} makes a function or variable externally visible in the generated object file.
Exported functions default to the C calling convention.
2020-08-24 15:59:55 +00:00
< ul >
< li > See also {#link|Functions#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}extern{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
2020-08-24 18:12:23 +00:00
{#syntax#}extern{#endsyntax#} can be used to declare a function or variable that will be resolved at link time, when linking statically
or at runtime, when linking dynamically.
2020-08-24 15:59:55 +00:00
< ul >
< li > See also {#link|Functions#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}fn{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
{#syntax#}fn{#endsyntax#} declares a function.
< ul >
< li > See also {#link|Functions#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}for{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
2020-08-24 18:12:23 +00:00
A {#syntax#}for{#endsyntax#} expression can be used to iterate over the elements of a slice, array, or tuple.
2020-08-24 15:59:55 +00:00
< ul >
< li > See also {#link|for#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}if{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
An {#syntax#}if{#endsyntax#} expression can test boolean expressions, optional values, or error unions.
For optional values or error unions, the if expression can capture the unwrapped value.
< ul >
< li > See also {#link|if#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}inline{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
{#syntax#}inline{#endsyntax#} can be used to label a loop expression such that it will be unrolled at compile time.
It can also be used to force a function to be inlined at all call sites.
< ul >
< li > See also {#link|inline while#}, {#link|inline for#}, {#link|Functions#}< / li >
< / ul >
< / td >
< / tr >
2023-01-16 18:32:24 +00:00
< tr >
< th scope = "row" >
< pre > {#syntax#}linksection{#endsyntax#}< / pre >
< / th >
< td >
2023-09-11 07:54:59 +00:00
The {#syntax#}linksection{#endsyntax#} keyword can be used to specify what section the function or global variable will be put into (e.g. < code > .text< / code > ).
2023-01-16 18:32:24 +00:00
< / td >
< / tr >
2020-08-24 15:59:55 +00:00
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}noalias{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
The {#syntax#}noalias{#endsyntax#} keyword.
< ul >
< li > TODO add documentation for noalias< / li >
< / ul >
< / td >
< / tr >
2023-04-22 11:43:37 +00:00
< tr >
< th scope = "row" >
< pre > {#syntax#}noinline{#endsyntax#}< / pre >
< / th >
< td >
{#syntax#}noinline{#endsyntax#} disallows function to be inlined in all call sites.
< ul >
< li > See also {#link|Functions#}< / li >
< / ul >
< / td >
< / tr >
2020-08-24 15:59:55 +00:00
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}nosuspend{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
2021-04-02 18:57:42 +00:00
The {#syntax#}nosuspend{#endsyntax#} keyword can be used in front of a block, statement or expression, to mark a scope where no suspension points are reached.
In particular, inside a {#syntax#}nosuspend{#endsyntax#} scope:
< ul >
< li > Using the {#syntax#}suspend{#endsyntax#} keyword results in a compile error.< / li >
< li > Using {#syntax#}await{#endsyntax#} on a function frame which hasn't completed yet results in safety-checked {#link|Undefined Behavior#}.< / li >
< li > Calling an async function may result in safety-checked {#link|Undefined Behavior#}, because it's equivalent to < code > await async some_async_fn()< / code > , which contains an {#syntax#}await{#endsyntax#}.< / li >
< / ul >
Code inside a {#syntax#}nosuspend{#endsyntax#} scope does not cause the enclosing function to become an {#link|async function|Async Functions#}.
2020-08-24 15:59:55 +00:00
< ul >
2021-04-02 18:57:42 +00:00
< li > See also {#link|Async Functions#}< / li >
2020-08-24 15:59:55 +00:00
< / ul >
< / td >
< / tr >
2023-09-06 16:10:24 +00:00
< tr >
< th scope = "row" >
< pre > {#syntax#}opaque{#endsyntax#}< / pre >
< / th >
< td >
{#syntax#}opaque{#endsyntax#} defines an opaque type.
< ul >
< li > See also {#link|opaque#}< / li >
< / ul >
< / td >
< / tr >
2020-08-24 15:59:55 +00:00
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}or{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
The boolean operator {#syntax#}or{#endsyntax#}.
< ul >
< li > See also {#link|Operators#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}orelse{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
2020-08-24 18:12:23 +00:00
{#syntax#}orelse{#endsyntax#} can be used to evaluate an expression if the expression before it evaluates to null.
2020-08-24 15:59:55 +00:00
< ul >
< li > See also {#link|Optionals#}, {#link|Operators#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}packed{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
2020-08-24 18:12:23 +00:00
The {#syntax#}packed{#endsyntax#} keyword before a struct definition changes the struct's in-memory layout
to the guaranteed {#syntax#}packed{#endsyntax#} layout.
2020-08-24 15:59:55 +00:00
< ul >
< li > See also {#link|packed struct#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}pub{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
2020-08-24 18:12:23 +00:00
The {#syntax#}pub{#endsyntax#} in front of a top level declaration makes the declaration available
to reference from a different file than the one it is declared in.
2020-08-24 15:59:55 +00:00
< ul >
< li > See also {#link|import#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}resume{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
{#syntax#}resume{#endsyntax#} will continue execution of a function frame after the point the function was suspended.
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}return{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
{#syntax#}return{#endsyntax#} exits a function with a value.
< ul >
< li > See also {#link|Functions#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}struct{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
2020-08-24 18:12:23 +00:00
{#syntax#}struct{#endsyntax#} defines a struct.
2020-08-24 15:59:55 +00:00
< ul >
< li > See also {#link|struct#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}suspend{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
{#syntax#}suspend{#endsyntax#} will cause control flow to return to the call site or resumer of the function.
2020-08-24 18:12:23 +00:00
{#syntax#}suspend{#endsyntax#} can also be used before a block within a function,
to allow the function access to its frame before control flow returns to the call site.
2020-08-24 15:59:55 +00:00
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}switch{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
2020-08-24 18:12:23 +00:00
A {#syntax#}switch{#endsyntax#} expression can be used to test values of a common type.
{#syntax#}switch{#endsyntax#} cases can capture field values of a {#link|Tagged union#}.
2020-08-24 15:59:55 +00:00
< ul >
< li > See also {#link|switch#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}test{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
2020-08-24 18:12:23 +00:00
The {#syntax#}test{#endsyntax#} keyword can be used to denote a top-level block of code
used to make sure behavior meets expectations.
2020-08-24 15:59:55 +00:00
< ul >
< li > See also {#link|Zig Test#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}threadlocal{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
{#syntax#}threadlocal{#endsyntax#} can be used to specify a variable as thread-local.
< ul >
< li > See also {#link|Thread Local Variables#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}try{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
{#syntax#}try{#endsyntax#} evaluates an error union expression.
If it is an error, it returns from the current function with the same error.
Otherwise, the expression results in the unwrapped value.
< ul >
< li > See also {#link|try#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}union{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
2020-08-24 18:12:23 +00:00
{#syntax#}union{#endsyntax#} defines a union.
2020-08-24 15:59:55 +00:00
< ul >
< li > See also {#link|union#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}unreachable{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
2020-08-24 18:12:23 +00:00
{#syntax#}unreachable{#endsyntax#} can be used to assert that control flow will never happen upon a particular location.
2020-08-24 15:59:55 +00:00
Depending on the build mode, {#syntax#}unreachable{#endsyntax#} may emit a panic.
< ul >
2021-08-11 09:01:03 +00:00
< li > Emits a panic in {#syntax#}Debug{#endsyntax#} and {#syntax#}ReleaseSafe{#endsyntax#} mode, or when using < kbd > zig test< / kbd > .< / li >
2022-12-13 20:02:17 +00:00
< li > Does not emit a panic in {#syntax#}ReleaseFast{#endsyntax#} and {#syntax#}ReleaseSmall{#endsyntax#} mode.< / li >
2020-08-24 15:59:55 +00:00
< li > See also {#link|unreachable#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}usingnamespace{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
2020-08-24 18:12:23 +00:00
{#syntax#}usingnamespace{#endsyntax#} is a top-level declaration that imports all the public declarations of the operand,
which must be a struct, union, or enum, into the current scope.
2020-08-24 15:59:55 +00:00
< ul >
< li > See also {#link|usingnamespace#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}var{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
{#syntax#}var{#endsyntax#} declares a variable that may be modified.
< ul >
< li > See also {#link|Variables#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}volatile{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
{#syntax#}volatile{#endsyntax#} can be used to denote loads or stores of a pointer have side effects.
It can also modify an inline assembly expression to denote it has side effects.
< ul >
< li > See also {#link|volatile#}, {#link|Assembly#}< / li >
< / ul >
< / td >
< / tr >
< tr >
2021-08-11 09:01:03 +00:00
< th scope = "row" >
2020-08-24 15:59:55 +00:00
< pre > {#syntax#}while{#endsyntax#}< / pre >
2021-08-11 09:01:03 +00:00
< / th >
2020-08-24 15:59:55 +00:00
< td >
2020-08-24 18:12:23 +00:00
A {#syntax#}while{#endsyntax#} expression can be used to repeatedly test a boolean, optional, or error union expression,
and cease looping when that expression evaluates to false, null, or an error, respectively.
2020-08-24 15:59:55 +00:00
< ul >
< li > See also {#link|while#}< / li >
< / ul >
< / td >
< / tr >
2021-08-11 09:01:03 +00:00
< / tbody >
2020-08-24 15:59:55 +00:00
< / table >
2020-08-24 22:29:53 +00:00
< / div >
2019-03-13 17:04:55 +00:00
{#header_close#}
2022-11-09 08:29:50 +00:00
{#header_open|Appendix#}
{#header_open|Containers#}
< p >
A < em > container< / em > in Zig is any syntactical construct that acts as a namespace to hold {#link|variable|Container Level Variables#} and {#link|function|Functions#} declarations.
Containers are also type definitions which can be instantiated.
{#link|Structs|struct#}, {#link|enums|enum#}, {#link|unions|union#}, {#link|opaques|opaque#}, and even Zig source files themselves are containers.
< / p >
< p >
Although containers (except Zig source files) use curly braces to surround their definition, they should not be confused with {#link|blocks|Blocks#} or functions.
Containers do not contain statements.
< / p >
{#header_close#}
2018-01-17 04:19:05 +00:00
{#header_open|Grammar#}
2023-01-19 11:33:00 +00:00
{#syntax_block|peg|grammar.y#}
Root < - skip container_doc_comment ? ContainerMembers eof
2017-11-07 08:22:27 +00:00
2018-11-14 07:56:26 +00:00
# *** Top level ***
2023-10-09 14:26:53 +00:00
ContainerMembers < - ContainerDeclaration * ( ContainerField COMMA ) * ( ContainerField / ContainerDeclaration * )
2021-05-28 19:21:14 +00:00
2023-10-09 14:26:53 +00:00
ContainerDeclaration < - TestDecl / ComptimeDecl / doc_comment ? KEYWORD_pub ? Decl
2017-11-07 08:22:27 +00:00
2022-12-28 10:34:41 +00:00
TestDecl < - KEYWORD_test ( STRINGLITERALSINGLE / IDENTIFIER ) ? Block
2017-11-07 08:22:27 +00:00
2022-12-26 14:35:24 +00:00
ComptimeDecl < - KEYWORD_comptime Block
2017-11-07 08:22:27 +00:00
2022-12-26 14:35:24 +00:00
Decl
2023-09-28 06:10:41 +00:00
< - ( KEYWORD_export / KEYWORD_extern STRINGLITERALSINGLE ? / KEYWORD_inline / KEYWORD_noinline ) ? FnProto ( SEMICOLON / Block )
2023-09-14 23:49:15 +00:00
/ (KEYWORD_export / KEYWORD_extern STRINGLITERALSINGLE?)? KEYWORD_threadlocal? GlobalVarDecl
2019-10-06 20:39:27 +00:00
/ KEYWORD_usingnamespace Expr SEMICOLON
2017-11-07 08:22:27 +00:00
2022-12-26 14:35:24 +00:00
FnProto < - KEYWORD_fn IDENTIFIER ? LPAREN ParamDeclList RPAREN ByteAlign ? AddrSpace ? LinkSection ? CallConv ? EXCLAMATIONMARK ? TypeExpr
2017-12-19 06:19:49 +00:00
2023-09-14 23:49:15 +00:00
VarDeclProto < - ( KEYWORD_const / KEYWORD_var ) IDENTIFIER ( COLON TypeExpr ) ? ByteAlign ? AddrSpace ? LinkSection ?
GlobalVarDecl < - VarDeclProto ( EQUAL Expr ) ? SEMICOLON
2017-11-07 08:22:27 +00:00
2024-03-06 05:49:17 +00:00
ContainerField < - doc_comment ? KEYWORD_comptime ? ! KEYWORD_fn ( IDENTIFIER COLON ) ? TypeExpr ByteAlign ? ( EQUAL Expr ) ?
2017-11-07 08:22:27 +00:00
2018-11-14 07:56:26 +00:00
# *** Block Level ***
Statement
2023-09-14 23:49:15 +00:00
< - KEYWORD_comptime ComptimeStatement
2020-05-14 08:19:14 +00:00
/ KEYWORD_nosuspend BlockExprStatement
2021-05-28 19:21:14 +00:00
/ KEYWORD_suspend BlockExprStatement
2018-11-14 07:56:26 +00:00
/ KEYWORD_defer BlockExprStatement
2021-05-28 19:21:14 +00:00
/ KEYWORD_errdefer Payload? BlockExprStatement
2018-11-14 07:56:26 +00:00
/ IfStatement
/ LabeledStatement
/ SwitchExpr
2023-09-14 23:49:15 +00:00
/ VarDeclExprStatement
ComptimeStatement
< - BlockExpr
/ VarDeclExprStatement
2017-11-07 08:22:27 +00:00
2018-11-14 07:56:26 +00:00
IfStatement
2021-10-02 20:33:58 +00:00
< - IfPrefix BlockExpr ( KEYWORD_else Payload ? Statement ) ?
2018-11-14 07:56:26 +00:00
/ IfPrefix AssignExpr ( SEMICOLON / KEYWORD_else Payload? Statement )
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
LabeledStatement < - BlockLabel ? ( Block / LoopStatement )
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
LoopStatement < - KEYWORD_inline ? ( ForStatement / WhileStatement )
2017-11-07 08:22:27 +00:00
2018-11-14 07:56:26 +00:00
ForStatement
2021-10-02 20:33:58 +00:00
< - ForPrefix BlockExpr ( KEYWORD_else Statement ) ?
2018-11-14 07:56:26 +00:00
/ ForPrefix AssignExpr ( SEMICOLON / KEYWORD_else Statement )
2017-11-07 08:22:27 +00:00
2018-11-14 07:56:26 +00:00
WhileStatement
2021-10-02 20:33:58 +00:00
< - WhilePrefix BlockExpr ( KEYWORD_else Payload ? Statement ) ?
2018-11-14 07:56:26 +00:00
/ WhilePrefix AssignExpr ( SEMICOLON / KEYWORD_else Payload? Statement )
2017-11-07 08:22:27 +00:00
2018-11-14 07:56:26 +00:00
BlockExprStatement
2021-10-02 20:33:58 +00:00
< - BlockExpr
2018-11-14 07:56:26 +00:00
/ AssignExpr SEMICOLON
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
BlockExpr < - BlockLabel ? Block
2017-11-07 08:22:27 +00:00
2023-09-14 23:49:15 +00:00
# An expression, assignment, or any destructure, as a statement.
VarDeclExprStatement
< - VarDeclProto ( COMMA ( VarDeclProto / Expr ) ) * EQUAL Expr SEMICOLON
/ Expr (AssignOp Expr / (COMMA (VarDeclProto / Expr))+ EQUAL Expr)? SEMICOLON
2018-11-14 07:56:26 +00:00
# *** Expression Level ***
2023-09-14 23:49:15 +00:00
# An assignment or a destructure whose LHS are all lvalue expressions.
AssignExpr < - Expr ( AssignOp Expr / ( COMMA Expr ) + EQUAL Expr ) ?
SingleAssignExpr < - Expr ( AssignOp Expr ) ?
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
Expr < - BoolOrExpr
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
BoolOrExpr < - BoolAndExpr ( KEYWORD_or BoolAndExpr ) *
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
BoolAndExpr < - CompareExpr ( KEYWORD_and CompareExpr ) *
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
CompareExpr < - BitwiseExpr ( CompareOp BitwiseExpr ) ?
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
BitwiseExpr < - BitShiftExpr ( BitwiseOp BitShiftExpr ) *
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
BitShiftExpr < - AdditionExpr ( BitShiftOp AdditionExpr ) *
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
AdditionExpr < - MultiplyExpr ( AdditionOp MultiplyExpr ) *
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
MultiplyExpr < - PrefixExpr ( MultiplyOp PrefixExpr ) *
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
PrefixExpr < - PrefixOp * PrimaryExpr
2017-11-07 08:22:27 +00:00
2018-11-14 07:56:26 +00:00
PrimaryExpr
2021-10-02 20:33:58 +00:00
< - AsmExpr
2018-11-14 07:56:26 +00:00
/ IfExpr
/ KEYWORD_break BreakLabel? Expr?
/ KEYWORD_comptime Expr
2020-05-14 08:19:14 +00:00
/ KEYWORD_nosuspend Expr
2018-11-14 07:56:26 +00:00
/ KEYWORD_continue BreakLabel?
/ KEYWORD_resume Expr
/ KEYWORD_return Expr?
2019-03-22 08:01:30 +00:00
/ BlockLabel? LoopExpr
/ Block
2018-11-14 07:56:26 +00:00
/ CurlySuffixExpr
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
IfExpr < - IfPrefix Expr ( KEYWORD_else Payload ? Expr ) ?
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
Block < - LBRACE Statement * RBRACE
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
LoopExpr < - KEYWORD_inline ? ( ForExpr / WhileExpr )
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
ForExpr < - ForPrefix Expr ( KEYWORD_else Expr ) ?
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
WhileExpr < - WhilePrefix Expr ( KEYWORD_else Payload ? Expr ) ?
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
CurlySuffixExpr < - TypeExpr InitList ?
2019-04-24 04:02:10 +00:00
2018-11-14 07:56:26 +00:00
InitList
2021-10-02 20:33:58 +00:00
< - LBRACE FieldInit ( COMMA FieldInit ) * COMMA ? RBRACE
2018-11-14 07:56:26 +00:00
/ LBRACE Expr (COMMA Expr)* COMMA? RBRACE
/ LBRACE RBRACE
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
TypeExpr < - PrefixTypeOp * ErrorUnionExpr
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
ErrorUnionExpr < - SuffixExpr ( EXCLAMATIONMARK TypeExpr ) ?
2017-11-07 08:22:27 +00:00
2018-11-14 07:56:26 +00:00
SuffixExpr
2021-10-02 20:33:58 +00:00
< - KEYWORD_async PrimaryTypeExpr SuffixOp * FnCallArguments
2018-11-17 13:36:36 +00:00
/ PrimaryTypeExpr (SuffixOp / FnCallArguments)*
2017-11-07 08:22:27 +00:00
2018-11-14 07:56:26 +00:00
PrimaryTypeExpr
2021-10-02 20:33:58 +00:00
< - BUILTINIDENTIFIER FnCallArguments
2018-11-14 07:56:26 +00:00
/ CHAR_LITERAL
/ ContainerDecl
2019-05-11 18:26:41 +00:00
/ DOT IDENTIFIER
2020-01-12 08:35:30 +00:00
/ DOT InitList
2018-11-14 07:56:26 +00:00
/ ErrorSetDecl
/ FLOAT
/ FnProto
/ GroupedExpr
/ LabeledTypeExpr
/ IDENTIFIER
/ IfTypeExpr
/ INTEGER
/ KEYWORD_comptime TypeExpr
/ KEYWORD_error DOT IDENTIFIER
2020-05-15 20:38:40 +00:00
/ KEYWORD_anyframe
2018-11-14 07:56:26 +00:00
/ KEYWORD_unreachable
/ STRINGLITERAL
/ SwitchExpr
2018-01-07 21:51:46 +00:00
2021-10-02 20:33:58 +00:00
ContainerDecl < - ( KEYWORD_extern / KEYWORD_packed ) ? ContainerDeclAuto
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
ErrorSetDecl < - KEYWORD_error LBRACE IdentifierList RBRACE
2018-02-26 05:04:11 +00:00
2021-10-02 20:33:58 +00:00
GroupedExpr < - LPAREN Expr RPAREN
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
IfTypeExpr < - IfPrefix TypeExpr ( KEYWORD_else Payload ? TypeExpr ) ?
2018-02-20 05:05:38 +00:00
2018-11-14 07:56:26 +00:00
LabeledTypeExpr
2021-10-02 20:33:58 +00:00
< - BlockLabel Block
2018-11-14 07:56:26 +00:00
/ BlockLabel? LoopTypeExpr
2018-03-01 03:18:48 +00:00
2021-10-02 20:33:58 +00:00
LoopTypeExpr < - KEYWORD_inline ? ( ForTypeExpr / WhileTypeExpr )
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
ForTypeExpr < - ForPrefix TypeExpr ( KEYWORD_else TypeExpr ) ?
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
WhileTypeExpr < - WhilePrefix TypeExpr ( KEYWORD_else Payload ? TypeExpr ) ?
2018-02-26 05:04:11 +00:00
2021-10-02 20:33:58 +00:00
SwitchExpr < - KEYWORD_switch LPAREN Expr RPAREN LBRACE SwitchProngList RBRACE
2017-11-07 08:22:27 +00:00
2018-11-14 07:56:26 +00:00
# *** Assembly ***
2021-10-02 20:33:58 +00:00
AsmExpr < - KEYWORD_asm KEYWORD_volatile ? LPAREN Expr AsmOutput ? RPAREN
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
AsmOutput < - COLON AsmOutputList AsmInput ?
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
AsmOutputItem < - LBRACKET IDENTIFIER RBRACKET STRINGLITERAL LPAREN ( MINUSRARROW TypeExpr / IDENTIFIER ) RPAREN
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
AsmInput < - COLON AsmInputList AsmClobbers ?
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
AsmInputItem < - LBRACKET IDENTIFIER RBRACKET STRINGLITERAL LPAREN Expr RPAREN
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
AsmClobbers < - COLON StringList
2017-11-07 08:22:27 +00:00
2018-11-14 07:56:26 +00:00
# *** Helper grammar ***
2021-10-02 20:33:58 +00:00
BreakLabel < - COLON IDENTIFIER
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
BlockLabel < - IDENTIFIER COLON
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
FieldInit < - DOT IDENTIFIER EQUAL Expr
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
WhileContinueExpr < - COLON LPAREN AssignExpr RPAREN
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
LinkSection < - KEYWORD_linksection LPAREN Expr RPAREN
2017-11-07 08:22:27 +00:00
2022-12-26 14:35:24 +00:00
AddrSpace < - KEYWORD_addrspace LPAREN Expr RPAREN
2021-05-28 19:21:14 +00:00
# Fn specific
2021-10-02 20:33:58 +00:00
CallConv < - KEYWORD_callconv LPAREN Expr RPAREN
2020-11-22 19:15:12 +00:00
2021-05-28 19:21:14 +00:00
ParamDecl
2021-10-02 20:33:58 +00:00
< - doc_comment ? ( KEYWORD_noalias / KEYWORD_comptime ) ? ( IDENTIFIER COLON ) ? ParamType
2021-05-28 19:21:14 +00:00
/ DOT3
2017-11-07 08:22:27 +00:00
2018-11-14 07:56:26 +00:00
ParamType
2021-10-02 20:33:58 +00:00
< - KEYWORD_anytype
2018-11-14 07:56:26 +00:00
/ TypeExpr
2017-11-07 08:22:27 +00:00
2018-11-14 07:56:26 +00:00
# Control flow prefixes
2021-10-02 20:33:58 +00:00
IfPrefix < - KEYWORD_if LPAREN Expr RPAREN PtrPayload ?
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
WhilePrefix < - KEYWORD_while LPAREN Expr RPAREN PtrPayload ? WhileContinueExpr ?
2017-11-07 08:22:27 +00:00
2023-09-14 23:49:15 +00:00
ForPrefix < - KEYWORD_for LPAREN ForArgumentsList RPAREN PtrListPayload
2017-11-07 08:22:27 +00:00
2018-11-14 07:56:26 +00:00
# Payloads
2021-10-02 20:33:58 +00:00
Payload < - PIPE IDENTIFIER PIPE
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
PtrPayload < - PIPE ASTERISK ? IDENTIFIER PIPE
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
PtrIndexPayload < - PIPE ASTERISK ? IDENTIFIER ( COMMA IDENTIFIER ) ? PIPE
2017-11-07 08:22:27 +00:00
2023-09-14 23:49:15 +00:00
PtrListPayload < - PIPE ASTERISK ? IDENTIFIER ( COMMA ASTERISK ? IDENTIFIER ) * COMMA ? PIPE
2017-11-07 08:22:27 +00:00
2018-11-14 07:56:26 +00:00
# Switch specific
2023-09-14 23:49:15 +00:00
SwitchProng < - KEYWORD_inline ? SwitchCase EQUALRARROW PtrIndexPayload ? SingleAssignExpr
2017-11-07 08:22:27 +00:00
2018-11-14 07:56:26 +00:00
SwitchCase
2021-10-02 20:33:58 +00:00
< - SwitchItem ( COMMA SwitchItem ) * COMMA ?
2018-11-14 07:56:26 +00:00
/ KEYWORD_else
2017-11-07 08:22:27 +00:00
2021-10-02 20:33:58 +00:00
SwitchItem < - Expr ( DOT3 Expr ) ?
2017-11-07 08:22:27 +00:00
2023-09-14 23:49:15 +00:00
# For specific
ForArgumentsList < - ForItem ( COMMA ForItem ) * COMMA ?
ForItem < - Expr ( DOT2 Expr ? ) ?
2018-11-14 07:56:26 +00:00
# Operators
AssignOp
2021-10-02 20:33:58 +00:00
< - ASTERISKEQUAL
2022-12-26 14:35:24 +00:00
/ ASTERISKPIPEEQUAL
2018-11-14 07:56:26 +00:00
/ SLASHEQUAL
/ PERCENTEQUAL
/ PLUSEQUAL
2022-12-26 14:35:24 +00:00
/ PLUSPIPEEQUAL
2018-11-14 07:56:26 +00:00
/ MINUSEQUAL
2022-12-26 14:35:24 +00:00
/ MINUSPIPEEQUAL
2018-11-14 07:56:26 +00:00
/ LARROW2EQUAL
2022-12-26 14:35:24 +00:00
/ LARROW2PIPEEQUAL
2018-11-14 07:56:26 +00:00
/ RARROW2EQUAL
/ AMPERSANDEQUAL
/ CARETEQUAL
/ PIPEEQUAL
/ ASTERISKPERCENTEQUAL
/ PLUSPERCENTEQUAL
/ MINUSPERCENTEQUAL
/ EQUAL
2017-11-07 08:22:27 +00:00
2018-11-14 07:56:26 +00:00
CompareOp
2021-10-02 20:33:58 +00:00
< - EQUALEQUAL
2018-11-14 07:56:26 +00:00
/ EXCLAMATIONMARKEQUAL
/ LARROW
/ RARROW
/ LARROWEQUAL
/ RARROWEQUAL
2018-03-24 23:25:53 +00:00
2018-11-14 07:56:26 +00:00
BitwiseOp
2021-10-02 20:33:58 +00:00
< - AMPERSAND
2018-11-14 07:56:26 +00:00
/ CARET
/ PIPE
/ KEYWORD_orelse
/ KEYWORD_catch Payload?
2017-11-07 08:22:27 +00:00
2018-11-14 07:56:26 +00:00
BitShiftOp
2021-10-02 20:33:58 +00:00
< - LARROW2
2018-11-14 07:56:26 +00:00
/ RARROW2
2022-12-26 14:35:24 +00:00
/ LARROW2PIPE
2017-11-07 08:22:27 +00:00
2018-11-14 07:56:26 +00:00
AdditionOp
2021-10-02 20:33:58 +00:00
< - PLUS
2018-11-14 07:56:26 +00:00
/ MINUS
/ PLUS2
/ PLUSPERCENT
/ MINUSPERCENT
2022-12-26 14:35:24 +00:00
/ PLUSPIPE
/ MINUSPIPE
2018-11-14 07:56:26 +00:00
MultiplyOp
2021-10-02 20:33:58 +00:00
< - PIPE2
2018-11-14 07:56:26 +00:00
/ ASTERISK
/ SLASH
/ PERCENT
/ ASTERISK2
/ ASTERISKPERCENT
2022-12-26 14:35:24 +00:00
/ ASTERISKPIPE
2018-11-14 07:56:26 +00:00
PrefixOp
2021-10-02 20:33:58 +00:00
< - EXCLAMATIONMARK
2018-11-14 07:56:26 +00:00
/ MINUS
/ TILDE
/ MINUSPERCENT
/ AMPERSAND
/ KEYWORD_try
/ KEYWORD_await
PrefixTypeOp
2021-10-02 20:33:58 +00:00
< - QUESTIONMARK
2020-05-15 20:38:40 +00:00
/ KEYWORD_anyframe MINUSRARROW
2022-12-26 14:35:24 +00:00
/ SliceTypeStart (ByteAlign / AddrSpace / KEYWORD_const / KEYWORD_volatile / KEYWORD_allowzero)*
2022-12-28 10:34:41 +00:00
/ PtrTypeStart (AddrSpace / KEYWORD_align LPAREN Expr (COLON Expr COLON Expr)? RPAREN / KEYWORD_const / KEYWORD_volatile / KEYWORD_allowzero)*
2021-05-28 19:21:14 +00:00
/ ArrayTypeStart
2018-11-14 07:56:26 +00:00
SuffixOp
2021-10-02 20:33:58 +00:00
< - LBRACKET Expr ( DOT2 ( Expr ? ( COLON Expr ) ? ) ? ) ? RBRACKET
2018-11-14 07:56:26 +00:00
/ DOT IDENTIFIER
/ DOTASTERISK
/ DOTQUESTIONMARK
2021-10-02 20:33:58 +00:00
FnCallArguments < - LPAREN ExprList RPAREN
2018-11-14 07:56:26 +00:00
# Ptr specific
2021-10-02 20:33:58 +00:00
SliceTypeStart < - LBRACKET ( COLON Expr ) ? RBRACKET
2018-11-14 07:56:26 +00:00
PtrTypeStart
2021-10-02 20:33:58 +00:00
< - ASTERISK
2018-11-14 07:56:26 +00:00
/ ASTERISK2
2020-11-22 19:15:12 +00:00
/ LBRACKET ASTERISK (LETTERC / COLON Expr)? RBRACKET
2018-11-14 07:56:26 +00:00
2021-10-02 20:33:58 +00:00
ArrayTypeStart < - LBRACKET Expr ( COLON Expr ) ? RBRACKET
2021-05-28 19:21:14 +00:00
2018-11-14 07:56:26 +00:00
# ContainerDecl specific
2021-10-02 20:33:58 +00:00
ContainerDeclAuto < - ContainerDeclType LBRACE container_doc_comment ? ContainerMembers RBRACE
2018-11-14 07:56:26 +00:00
ContainerDeclType
2022-12-26 14:35:24 +00:00
< - KEYWORD_struct ( LPAREN Expr RPAREN ) ?
2020-11-22 19:15:12 +00:00
/ KEYWORD_opaque
/ KEYWORD_enum (LPAREN Expr RPAREN)?
2018-11-14 07:56:26 +00:00
/ KEYWORD_union (LPAREN (KEYWORD_enum (LPAREN Expr RPAREN)? / Expr) RPAREN)?
# Alignment
2021-10-02 20:33:58 +00:00
ByteAlign < - KEYWORD_align LPAREN Expr RPAREN
2018-11-14 07:56:26 +00:00
# Lists
2021-10-02 20:33:58 +00:00
IdentifierList < - ( doc_comment ? IDENTIFIER COMMA ) * ( doc_comment ? IDENTIFIER ) ?
2018-11-14 07:56:26 +00:00
2021-10-02 20:33:58 +00:00
SwitchProngList < - ( SwitchProng COMMA ) * SwitchProng ?
2018-11-14 07:56:26 +00:00
2021-10-02 20:33:58 +00:00
AsmOutputList < - ( AsmOutputItem COMMA ) * AsmOutputItem ?
2018-11-14 07:56:26 +00:00
2021-10-02 20:33:58 +00:00
AsmInputList < - ( AsmInputItem COMMA ) * AsmInputItem ?
2018-11-14 07:56:26 +00:00
2021-10-02 20:33:58 +00:00
StringList < - ( STRINGLITERAL COMMA ) * STRINGLITERAL ?
2018-11-14 07:56:26 +00:00
2021-10-02 20:33:58 +00:00
ParamDeclList < - ( ParamDecl COMMA ) * ParamDecl ?
2018-11-14 07:56:26 +00:00
2021-10-02 20:33:58 +00:00
ExprList < - ( Expr COMMA ) * Expr ?
2018-11-14 07:56:26 +00:00
# *** Tokens ***
2021-10-02 20:33:58 +00:00
eof < - ! .
bin < - [ 01 ]
bin_ < - ' _ ' ? bin
oct < - [ 0-7 ]
oct_ < - ' _ ' ? oct
hex < - [ 0-9a-fA-F ]
hex_ < - ' _ ' ? hex
dec < - [ 0-9 ]
dec_ < - ' _ ' ? dec
bin_int < - bin bin_ *
oct_int < - oct oct_ *
dec_int < - dec dec_ *
hex_int < - hex hex_ *
ox80_oxBF < - [ \ 200- \ 277 ]
oxF4 < - ' \ 364 '
ox80_ox8F < - [ \ 200- \ 217 ]
oxF1_oxF3 < - [ \ 361- \ 363 ]
oxF0 < - ' \ 360 '
ox90_0xBF < - [ \ 220- \ 277 ]
oxEE_oxEF < - [ \ 356- \ 357 ]
oxED < - ' \ 355 '
ox80_ox9F < - [ \ 200- \ 237 ]
oxE1_oxEC < - [ \ 341- \ 354 ]
oxE0 < - ' \ 340 '
oxA0_oxBF < - [ \ 240- \ 277 ]
oxC2_oxDF < - [ \ 302- \ 337 ]
2021-05-28 19:21:14 +00:00
# From https://lemire.me/blog/2018/05/09/how-quickly-can-you-check-that-a-string-is-valid-unicode-utf-8/
# First Byte Second Byte Third Byte Fourth Byte
# [0x00,0x7F]
# [0xC2,0xDF] [0x80,0xBF]
# 0xE0 [0xA0,0xBF] [0x80,0xBF]
# [0xE1,0xEC] [0x80,0xBF] [0x80,0xBF]
# 0xED [0x80,0x9F] [0x80,0xBF]
# [0xEE,0xEF] [0x80,0xBF] [0x80,0xBF]
# 0xF0 [0x90,0xBF] [0x80,0xBF] [0x80,0xBF]
# [0xF1,0xF3] [0x80,0xBF] [0x80,0xBF] [0x80,0xBF]
# 0xF4 [0x80,0x8F] [0x80,0xBF] [0x80,0xBF]
2021-10-02 20:33:58 +00:00
mb_utf8_literal < -
2021-05-28 19:21:14 +00:00
oxF4 ox80_ox8F ox80_oxBF ox80_oxBF
/ oxF1_oxF3 ox80_oxBF ox80_oxBF ox80_oxBF
/ oxF0 ox90_0xBF ox80_oxBF ox80_oxBF
/ oxEE_oxEF ox80_oxBF ox80_oxBF
/ oxED ox80_ox9F ox80_oxBF
/ oxE1_oxEC ox80_oxBF ox80_oxBF
/ oxE0 oxA0_oxBF ox80_oxBF
/ oxC2_oxDF ox80_oxBF
2024-04-02 09:22:19 +00:00
ascii_char_not_nl_slash_squote < - [ \ 000- \ 011 \ 013- \ 046 \ 050- \ 133 \ 135- \ 177 ]
2020-11-22 19:15:12 +00:00
2018-11-14 07:56:26 +00:00
char_escape
2021-10-02 20:33:58 +00:00
< - " \ \ x " hex hex
/ "\\u{" hex+ "}"
/ "\\" [nr\\t'"]
2018-11-14 07:56:26 +00:00
char_char
2021-10-02 20:33:58 +00:00
< - mb_utf8_literal
2021-05-28 19:21:14 +00:00
/ char_escape
/ ascii_char_not_nl_slash_squote
2018-11-14 07:56:26 +00:00
string_char
2021-10-02 20:33:58 +00:00
< - char_escape
/ [^\\"\n]
2018-11-14 07:56:26 +00:00
2022-12-27 11:52:37 +00:00
container_doc_comment < - ( ' / / ! ' [ ^ \ n ] * [ \ n ] * skip ) +
doc_comment < - ( ' / / / ' [ ^ \ n ] * [ \ n ] * skip ) +
2021-10-02 20:33:58 +00:00
line_comment < - ' / / ' ! [ ! / ] [ ^ \ n ] * / ' / / / / ' [ ^ \ n ] *
line_string < - ( " \ \ \ \ " [ ^ \ n ] * [ \ n ] * ) +
skip < - ( [ \ n ] / line_comment ) *
2018-11-14 07:56:26 +00:00
2021-10-02 20:33:58 +00:00
CHAR_LITERAL < - " ' " char_char " ' " skip
2018-11-14 07:56:26 +00:00
FLOAT
2021-10-02 20:33:58 +00:00
< - " 0x " hex_int " . " hex_int ( [ pP ] [ - + ] ? dec_int ) ? skip
/ dec_int "." dec_int ([eE] [-+]? dec_int)? skip
/ "0x" hex_int [pP] [-+]? dec_int skip
/ dec_int [eE] [-+]? dec_int skip
2018-11-14 07:56:26 +00:00
INTEGER
2021-10-02 20:33:58 +00:00
< - " 0b " bin_int skip
/ "0o" oct_int skip
/ "0x" hex_int skip
2020-11-22 19:15:12 +00:00
/ dec_int skip
2021-10-02 20:33:58 +00:00
STRINGLITERALSINGLE < - " \ " " string_char * " \ " " skip
2018-11-14 07:56:26 +00:00
STRINGLITERAL
2021-10-02 20:33:58 +00:00
< - STRINGLITERALSINGLE
2021-05-28 19:21:14 +00:00
/ (line_string skip)+
2018-11-14 07:56:26 +00:00
IDENTIFIER
2021-10-02 20:33:58 +00:00
< - ! keyword [ A-Za-z_ ] [ A-Za-z0-9_ ] * skip
2023-09-14 23:49:15 +00:00
/ "@" STRINGLITERALSINGLE
2021-10-02 20:33:58 +00:00
BUILTINIDENTIFIER < - " @ " [ A-Za-z_ ] [ A-Za-z0-9_ ] * skip
AMPERSAND < - ' & ' ! [ = ] skip
AMPERSANDEQUAL < - ' & = ' skip
2022-12-26 14:35:24 +00:00
ASTERISK < - ' * ' ! [ * % = | ] skip
2021-10-02 20:33:58 +00:00
ASTERISK2 < - ' * * ' skip
ASTERISKEQUAL < - ' * = ' skip
ASTERISKPERCENT < - ' * % ' ! [ = ] skip
ASTERISKPERCENTEQUAL < - ' * % = ' skip
2022-12-26 14:35:24 +00:00
ASTERISKPIPE < - ' * | ' ! [ = ] skip
ASTERISKPIPEEQUAL < - ' * | = ' skip
2021-10-02 20:33:58 +00:00
CARET < - ' ^ ' ! [ = ] skip
CARETEQUAL < - ' ^ = ' skip
COLON < - ' : ' skip
COMMA < - ' , ' skip
DOT < - ' . ' ! [ * . ? ] skip
DOT2 < - ' . . ' ! [ . ] skip
DOT3 < - ' . . . ' skip
DOTASTERISK < - ' . * ' skip
DOTQUESTIONMARK < - ' . ? ' skip
EQUAL < - ' = ' ! [ > =] skip
EQUALEQUAL < - ' = = ' skip
EQUALRARROW < - ' = > ' skip
EXCLAMATIONMARK < - ' ! ' ! [ = ] skip
EXCLAMATIONMARKEQUAL < - ' ! = ' skip
LARROW < - ' < ' ! [ < = ] skip
2022-12-26 14:35:24 +00:00
LARROW2 < - ' < < ' ! [ = | ] skip
2021-10-02 20:33:58 +00:00
LARROW2EQUAL < - ' < < = ' skip
2022-12-26 14:35:24 +00:00
LARROW2PIPE < - ' < < | ' ! [ = ] skip
LARROW2PIPEEQUAL < - ' < < | = ' skip
2021-10-02 20:33:58 +00:00
LARROWEQUAL < - ' < = ' skip
LBRACE < - ' { ' skip
LBRACKET < - ' [ ' skip
LPAREN < - ' ( ' skip
2022-12-26 14:35:24 +00:00
MINUS < - ' - ' ! [ % = > |] skip
2021-10-02 20:33:58 +00:00
MINUSEQUAL < - ' - = ' skip
MINUSPERCENT < - ' - % ' ! [ = ] skip
MINUSPERCENTEQUAL < - ' - % = ' skip
2022-12-26 14:35:24 +00:00
MINUSPIPE < - ' - | ' ! [ = ] skip
MINUSPIPEEQUAL < - ' - | = ' skip
2021-10-02 20:33:58 +00:00
MINUSRARROW < - ' - > ' skip
PERCENT < - ' % ' ! [ = ] skip
PERCENTEQUAL < - ' % = ' skip
PIPE < - ' | ' ! [ | = ] skip
PIPE2 < - ' | | ' skip
PIPEEQUAL < - ' | = ' skip
2022-12-26 14:35:24 +00:00
PLUS < - ' + ' ! [ % + = | ] skip
2021-10-02 20:33:58 +00:00
PLUS2 < - ' + + ' skip
PLUSEQUAL < - ' + = ' skip
PLUSPERCENT < - ' + % ' ! [ = ] skip
PLUSPERCENTEQUAL < - ' + % = ' skip
2022-12-26 14:35:24 +00:00
PLUSPIPE < - ' + | ' ! [ = ] skip
PLUSPIPEEQUAL < - ' + | = ' skip
2021-10-02 20:33:58 +00:00
LETTERC < - ' c ' skip
QUESTIONMARK < - ' ? ' skip
RARROW < - ' > ' ![>=] skip
RARROW2 < - ' > >' ![=] skip
RARROW2EQUAL < - ' > >=' skip
RARROWEQUAL < - ' > =' skip
RBRACE < - ' } ' skip
RBRACKET < - ' ] ' skip
RPAREN < - ' ) ' skip
SEMICOLON < - ' ; ' skip
SLASH < - ' / ' ! [ = ] skip
SLASHEQUAL < - ' / = ' skip
TILDE < - ' ~ ' skip
end_of_word < - ! [ a-zA-Z0-9_ ] skip
2022-12-26 14:35:24 +00:00
KEYWORD_addrspace < - ' addrspace ' end_of_word
2021-10-02 20:33:58 +00:00
KEYWORD_align < - ' align ' end_of_word
KEYWORD_allowzero < - ' allowzero ' end_of_word
KEYWORD_and < - ' and ' end_of_word
KEYWORD_anyframe < - ' anyframe ' end_of_word
KEYWORD_anytype < - ' anytype ' end_of_word
KEYWORD_asm < - ' asm ' end_of_word
KEYWORD_async < - ' async ' end_of_word
KEYWORD_await < - ' await ' end_of_word
KEYWORD_break < - ' break ' end_of_word
KEYWORD_callconv < - ' callconv ' end_of_word
KEYWORD_catch < - ' catch ' end_of_word
KEYWORD_comptime < - ' comptime ' end_of_word
KEYWORD_const < - ' const ' end_of_word
KEYWORD_continue < - ' continue ' end_of_word
KEYWORD_defer < - ' defer ' end_of_word
KEYWORD_else < - ' else ' end_of_word
KEYWORD_enum < - ' enum ' end_of_word
KEYWORD_errdefer < - ' errdefer ' end_of_word
KEYWORD_error < - ' error ' end_of_word
KEYWORD_export < - ' export ' end_of_word
KEYWORD_extern < - ' extern ' end_of_word
KEYWORD_fn < - ' fn ' end_of_word
KEYWORD_for < - ' for ' end_of_word
KEYWORD_if < - ' if ' end_of_word
KEYWORD_inline < - ' inline ' end_of_word
KEYWORD_noalias < - ' noalias ' end_of_word
KEYWORD_nosuspend < - ' nosuspend ' end_of_word
KEYWORD_noinline < - ' noinline ' end_of_word
KEYWORD_opaque < - ' opaque ' end_of_word
KEYWORD_or < - ' or ' end_of_word
KEYWORD_orelse < - ' orelse ' end_of_word
KEYWORD_packed < - ' packed ' end_of_word
KEYWORD_pub < - ' pub ' end_of_word
KEYWORD_resume < - ' resume ' end_of_word
KEYWORD_return < - ' return ' end_of_word
2023-09-14 23:49:15 +00:00
KEYWORD_linksection < - ' linksection ' end_of_word
2021-10-02 20:33:58 +00:00
KEYWORD_struct < - ' struct ' end_of_word
KEYWORD_suspend < - ' suspend ' end_of_word
KEYWORD_switch < - ' switch ' end_of_word
KEYWORD_test < - ' test ' end_of_word
KEYWORD_threadlocal < - ' threadlocal ' end_of_word
KEYWORD_try < - ' try ' end_of_word
KEYWORD_union < - ' union ' end_of_word
KEYWORD_unreachable < - ' unreachable ' end_of_word
KEYWORD_usingnamespace < - ' usingnamespace ' end_of_word
KEYWORD_var < - ' var ' end_of_word
KEYWORD_volatile < - ' volatile ' end_of_word
KEYWORD_while < - ' while ' end_of_word
2022-12-26 14:35:24 +00:00
keyword < - KEYWORD_addrspace / KEYWORD_align / KEYWORD_allowzero / KEYWORD_and
/ KEYWORD_anyframe / KEYWORD_anytype / KEYWORD_asm / KEYWORD_async
/ KEYWORD_await / KEYWORD_break / KEYWORD_callconv / KEYWORD_catch
/ KEYWORD_comptime / KEYWORD_const / KEYWORD_continue / KEYWORD_defer
/ KEYWORD_else / KEYWORD_enum / KEYWORD_errdefer / KEYWORD_error / KEYWORD_export
2021-08-28 17:54:14 +00:00
/ KEYWORD_extern / KEYWORD_fn / KEYWORD_for / KEYWORD_if
2023-09-14 23:49:15 +00:00
/ KEYWORD_inline / KEYWORD_noalias / KEYWORD_nosuspend / KEYWORD_noinline
/ KEYWORD_opaque / KEYWORD_or / KEYWORD_orelse / KEYWORD_packed
/ KEYWORD_pub / KEYWORD_resume / KEYWORD_return / KEYWORD_linksection
2021-10-02 20:33:58 +00:00
/ KEYWORD_struct / KEYWORD_suspend / KEYWORD_switch / KEYWORD_test
/ KEYWORD_threadlocal / KEYWORD_try / KEYWORD_union / KEYWORD_unreachable
2020-11-22 19:15:12 +00:00
/ KEYWORD_usingnamespace / KEYWORD_var / KEYWORD_volatile / KEYWORD_while
2023-01-19 11:33:00 +00:00
{#end_syntax_block#}
2018-01-17 04:19:05 +00:00
{#header_close#}
{#header_open|Zen#}
2017-11-07 08:22:27 +00:00
< ul >
< li > Communicate intent precisely.< / li >
< li > Edge cases matter.< / li >
< li > Favor reading code over writing code.< / li >
< li > Only one obvious way to do things.< / li >
< li > Runtime crashes are better than bugs.< / li >
< li > Compile errors are better than runtime crashes.< / li >
< li > Incremental improvements.< / li >
< li > Avoid local maximums.< / li >
< li > Reduce the amount one must remember.< / li >
2020-09-15 20:40:01 +00:00
< li > Focus on code rather than style.< / li >
< li > Resource allocation may fail; resource deallocation must succeed.< / li >
< li > Memory is a resource.< / li >
2020-08-31 21:40:08 +00:00
< li > Together we serve the users.< / li >
2017-11-07 08:22:27 +00:00
< / ul >
2018-01-17 04:19:05 +00:00
{#header_close#}
2022-11-09 08:29:50 +00:00
{#header_close#}
2021-08-11 09:01:03 +00:00
< / main > < / div >
2017-11-07 08:22:27 +00:00
< / div >
< / body >
< / html >