-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-dark-mode-01.html
14197 lines (12531 loc) · 823 KB
/
test-dark-mode-01.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documentation - The Zig Programming Language</title>
<link rel="icon" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAgklEQVR4AWMYWuD7EllJIM4G4g4g5oIJ/odhOJ8wToOxSTXgNxDHoeiBMfA4+wGShjyYOCkG/IGqWQziEzYAoUAeiF9D5U+DxEg14DRU7jWIT5IBIOdCxf+A+CQZAAoopEB7QJwBCBwHiip8UYmRdrAlDpIMgApwQZNnNii5Dq0MBgCxxycBnwEd+wAAAABJRU5ErkJggg=="/>
<style>
:root{
--nav-width: 24em;
--nav-margin-l: 1em;
}
body{
font-family: system-ui, -apple-system, Roboto, "Segoe UI", sans-serif;
margin: 0;
line-height: 1.5;
}
#contents {
max-width: 60em;
margin: auto;
padding: 0 1em;
}
#navigation {
padding: 0 1em;
}
@media screen and (min-width: 1025px) {
#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));
}
}
a:hover,a:focus {
background: #fff2a8;
}
dt {
font-weight: bold;
}
table, th, td {
border-collapse: collapse;
border: 1px solid grey;
}
th, td {
padding: 0.1em;
}
th[scope=row] {
text-align: left;
font-weight: normal;
}
.t0_1, .t37, .t37_1 {
font-weight: bold;
}
.t2_0 {
color: #575757;
}
.t31_1 {
color: #b40000;
}
.t32_1 {
color: green;
}
.t36_1 {
color: #005C7A;
}
.file {
font-weight: bold;
border: unset;
}
code {
background: #f8f8f8;
border: 1px dotted silver;
padding-left: 0.3em;
padding-right: 0.3em;
}
pre > code {
display: block;
overflow: auto;
padding: 0.5em;
border: 1px solid #eee;
line-height: normal;
}
samp {
background: #fafafa;
}
pre > samp {
display: block;
overflow: auto;
padding: 0.5em;
border: 1px solid #eee;
line-height: normal;
}
kbd {
font-weight: bold;
}
.table-wrapper {
width: 100%;
overflow-x: auto;
}
.tok-kw {
color: #333;
font-weight: bold;
}
.tok-str {
color: #d14;
}
.tok-builtin {
color: #005C7A;
}
.tok-comment {
color: #545454;
font-style: italic;
}
.tok-fn {
color: #900;
font-weight: bold;
}
.tok-null {
color: #005C5C;
}
.tok-number {
color: #005C5C;
}
.tok-type {
color: #458;
font-weight: bold;
}
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;
}
figcaption.zig-cap {
background: #fcdba5;
}
figcaption.c-cap {
background: #a8b9cc;
color: #000;
}
figcaption.javascript-cap {
background: #365d95;
color: #fff;
}
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;
}
body{
background:#121212;
color: #ccc;
}
a {
color: #88f;
}
a:hover,a:focus {
color: #000;
}
table, th, td {
border-color: grey;
}
.t2_0 {
color: grey;
}
.t31_1 {
color: red;
}
.t32_1 {
color: #00B800;
}
.t36_1 {
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.shell-cap {
background: #2a2a2a;
color: #fff;
}
</style>
</head>
<body>
<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.0/">0.8.0</a> |
master
</nav>
<nav aria-labelledby="table-of-contents">
<h2 id="table-of-contents">Table of Contents</h2>
<ul>
<li><a id="toc-Introduction" href="#Introduction">Introduction</a></li>
<li><a id="toc-Zig-Standard-Library" href="#Zig-Standard-Library">Zig Standard Library</a></li>
<li><a id="toc-Hello-World" href="#Hello-World">Hello World</a></li>
<li><a id="toc-Zig-Test" href="#Zig-Test">Zig Test</a></li>
<li><a id="toc-Comments" href="#Comments">Comments</a>
<ul>
<li><a id="toc-Doc-comments" href="#Doc-comments">Doc comments</a></li>
<li><a id="toc-Top-Level-Doc-Comments" href="#Top-Level-Doc-Comments">Top-Level Doc Comments</a></li>
</ul></li>
<li><a id="toc-Values" href="#Values">Values</a>
<ul>
<li><a id="toc-Primitive-Types" href="#Primitive-Types">Primitive Types</a></li>
<li><a id="toc-Primitive-Values" href="#Primitive-Values">Primitive Values</a></li>
<li><a id="toc-String-Literals-and-Unicode-Code-Point-Literals" href="#String-Literals-and-Unicode-Code-Point-Literals">String Literals and Unicode Code Point Literals</a>
<ul>
<li><a id="toc-Escape-Sequences" href="#Escape-Sequences">Escape Sequences</a></li>
<li><a id="toc-Multiline-String-Literals" href="#Multiline-String-Literals">Multiline String Literals</a></li>
</ul></li>
<li><a id="toc-Assignment" href="#Assignment">Assignment</a>
<ul>
<li><a id="toc-undefined" href="#undefined">undefined</a></li>
</ul></li>
</ul></li>
<li><a id="toc-Variables" href="#Variables">Variables</a>
<ul>
<li><a id="toc-Container-Level-Variables" href="#Container-Level-Variables">Container Level Variables</a></li>
<li><a id="toc-Static-Local-Variables" href="#Static-Local-Variables">Static Local Variables</a></li>
<li><a id="toc-Thread-Local-Variables" href="#Thread-Local-Variables">Thread Local Variables</a></li>
<li><a id="toc-Local-Variables" href="#Local-Variables">Local Variables</a></li>
</ul></li>
<li><a id="toc-Integers" href="#Integers">Integers</a>
<ul>
<li><a id="toc-Integer-Literals" href="#Integer-Literals">Integer Literals</a></li>
<li><a id="toc-Runtime-Integer-Values" href="#Runtime-Integer-Values">Runtime Integer Values</a></li>
</ul></li>
<li><a id="toc-Floats" href="#Floats">Floats</a>
<ul>
<li><a id="toc-Float-Literals" href="#Float-Literals">Float Literals</a></li>
<li><a id="toc-Floating-Point-Operations" href="#Floating-Point-Operations">Floating Point Operations</a></li>
</ul></li>
<li><a id="toc-Operators" href="#Operators">Operators</a>
<ul>
<li><a id="toc-Table-of-Operators" href="#Table-of-Operators">Table of Operators</a></li>
<li><a id="toc-Precedence" href="#Precedence">Precedence</a></li>
</ul></li>
<li><a id="toc-Arrays" href="#Arrays">Arrays</a>
<ul>
<li><a id="toc-Anonymous-List-Literals" href="#Anonymous-List-Literals">Anonymous List Literals</a></li>
<li><a id="toc-Multidimensional-Arrays" href="#Multidimensional-Arrays">Multidimensional Arrays</a></li>
<li><a id="toc-Sentinel-Terminated-Arrays" href="#Sentinel-Terminated-Arrays">Sentinel-Terminated Arrays</a></li>
</ul></li>
<li><a id="toc-Vectors" href="#Vectors">Vectors</a>
<ul>
<li><a id="toc-SIMD" href="#SIMD">SIMD</a></li>
</ul></li>
<li><a id="toc-Pointers" href="#Pointers">Pointers</a>
<ul>
<li><a id="toc-volatile" href="#volatile">volatile</a></li>
<li><a id="toc-Alignment" href="#Alignment">Alignment</a></li>
<li><a id="toc-allowzero" href="#allowzero">allowzero</a></li>
<li><a id="toc-Sentinel-Terminated-Pointers" href="#Sentinel-Terminated-Pointers">Sentinel-Terminated Pointers</a></li>
</ul></li>
<li><a id="toc-Slices" href="#Slices">Slices</a>
<ul>
<li><a id="toc-Sentinel-Terminated-Slices" href="#Sentinel-Terminated-Slices">Sentinel-Terminated Slices</a></li>
</ul></li>
<li><a id="toc-struct" href="#struct">struct</a>
<ul>
<li><a id="toc-Default-Field-Values" href="#Default-Field-Values">Default Field Values</a></li>
<li><a id="toc-extern-struct" href="#extern-struct">extern struct</a></li>
<li><a id="toc-packed-struct" href="#packed-struct">packed struct</a></li>
<li><a id="toc-Struct-Naming" href="#Struct-Naming">Struct Naming</a></li>
<li><a id="toc-Anonymous-Struct-Literals" href="#Anonymous-Struct-Literals">Anonymous Struct Literals</a></li>
</ul></li>
<li><a id="toc-enum" href="#enum">enum</a>
<ul>
<li><a id="toc-extern-enum" href="#extern-enum">extern enum</a></li>
<li><a id="toc-Enum-Literals" href="#Enum-Literals">Enum Literals</a></li>
<li><a id="toc-Non-exhaustive-enum" href="#Non-exhaustive-enum">Non-exhaustive enum</a></li>
</ul></li>
<li><a id="toc-union" href="#union">union</a>
<ul>
<li><a id="toc-Tagged-union" href="#Tagged-union">Tagged union</a></li>
<li><a id="toc-extern-union" href="#extern-union">extern union</a></li>
<li><a id="toc-packed-union" href="#packed-union">packed union</a></li>
<li><a id="toc-Anonymous-Union-Literals" href="#Anonymous-Union-Literals">Anonymous Union Literals</a></li>
</ul></li>
<li><a id="toc-opaque" href="#opaque">opaque</a></li>
<li><a id="toc-blocks" href="#blocks">blocks</a>
<ul>
<li><a id="toc-Shadowing" href="#Shadowing">Shadowing</a></li>
</ul></li>
<li><a id="toc-switch" href="#switch">switch</a>
<ul>
<li><a id="toc-Exhaustive-Switching" href="#Exhaustive-Switching">Exhaustive Switching</a></li>
<li><a id="toc-Switching-with-Enum-Literals" href="#Switching-with-Enum-Literals">Switching with Enum Literals</a></li>
</ul></li>
<li><a id="toc-while" href="#while">while</a>
<ul>
<li><a id="toc-Labeled-while" href="#Labeled-while">Labeled while</a></li>
<li><a id="toc-while-with-Optionals" href="#while-with-Optionals">while with Optionals</a></li>
<li><a id="toc-while-with-Error-Unions" href="#while-with-Error-Unions">while with Error Unions</a></li>
<li><a id="toc-inline-while" href="#inline-while">inline while</a></li>
</ul></li>
<li><a id="toc-for" href="#for">for</a>
<ul>
<li><a id="toc-Labeled-for" href="#Labeled-for">Labeled for</a></li>
<li><a id="toc-inline-for" href="#inline-for">inline for</a></li>
</ul></li>
<li><a id="toc-if" href="#if">if</a></li>
<li><a id="toc-defer" href="#defer">defer</a></li>
<li><a id="toc-unreachable" href="#unreachable">unreachable</a>
<ul>
<li><a id="toc-Basics" href="#Basics">Basics</a></li>
<li><a id="toc-At-Compile-Time" href="#At-Compile-Time">At Compile-Time</a></li>
</ul></li>
<li><a id="toc-noreturn" href="#noreturn">noreturn</a></li>
<li><a id="toc-Functions" href="#Functions">Functions</a>
<ul>
<li><a id="toc-Pass-by-value-Parameters" href="#Pass-by-value-Parameters">Pass-by-value Parameters</a></li>
<li><a id="toc-Function-Parameter-Type-Inference" href="#Function-Parameter-Type-Inference">Function Parameter Type Inference</a></li>
<li><a id="toc-Function-Reflection" href="#Function-Reflection">Function Reflection</a></li>
</ul></li>
<li><a id="toc-Errors" href="#Errors">Errors</a>
<ul>
<li><a id="toc-Error-Set-Type" href="#Error-Set-Type">Error Set Type</a>
<ul>
<li><a id="toc-The-Global-Error-Set" href="#The-Global-Error-Set">The Global Error Set</a></li>
</ul></li>
<li><a id="toc-Error-Union-Type" href="#Error-Union-Type">Error Union Type</a>
<ul>
<li><a id="toc-catch" href="#catch">catch</a></li>
<li><a id="toc-try" href="#try">try</a></li>
<li><a id="toc-errdefer" href="#errdefer">errdefer</a></li>
<li><a id="toc-Merging-Error-Sets" href="#Merging-Error-Sets">Merging Error Sets</a></li>
<li><a id="toc-Inferred-Error-Sets" href="#Inferred-Error-Sets">Inferred Error Sets</a></li>
</ul></li>
<li><a id="toc-Error-Return-Traces" href="#Error-Return-Traces">Error Return Traces</a>
<ul>
<li><a id="toc-Implementation-Details" href="#Implementation-Details">Implementation Details</a></li>
</ul></li>
</ul></li>
<li><a id="toc-Optionals" href="#Optionals">Optionals</a>
<ul>
<li><a id="toc-Optional-Type" href="#Optional-Type">Optional Type</a></li>
<li><a id="toc-null" href="#null">null</a></li>
<li><a id="toc-Optional-Pointers" href="#Optional-Pointers">Optional Pointers</a></li>
</ul></li>
<li><a id="toc-Casting" href="#Casting">Casting</a>
<ul>
<li><a id="toc-Type-Coercion" href="#Type-Coercion">Type Coercion</a>
<ul>
<li><a id="toc-Type-Coercion-Stricter-Qualification" href="#Type-Coercion-Stricter-Qualification">Type Coercion: Stricter Qualification</a></li>
<li><a id="toc-Type-Coercion-Integer-and-Float-Widening" href="#Type-Coercion-Integer-and-Float-Widening">Type Coercion: Integer and Float Widening</a></li>
<li><a id="toc-Type-Coercion-Coercion-Float-to-Int" href="#Type-Coercion-Coercion-Float-to-Int">Type Coercion: Coercion Float to Int</a></li>
<li><a id="toc-Type-Coercion-Slices-Arrays-and-Pointers" href="#Type-Coercion-Slices-Arrays-and-Pointers">Type Coercion: Slices, Arrays and Pointers</a></li>
<li><a id="toc-Type-Coercion-Optionals" href="#Type-Coercion-Optionals">Type Coercion: Optionals</a></li>
<li><a id="toc-Type-Coercion-Error-Unions" href="#Type-Coercion-Error-Unions">Type Coercion: Error Unions</a></li>
<li><a id="toc-Type-Coercion-Compile-Time-Known-Numbers" href="#Type-Coercion-Compile-Time-Known-Numbers">Type Coercion: Compile-Time Known Numbers</a></li>
<li><a id="toc-Type-Coercion-unions-and-enums" href="#Type-Coercion-unions-and-enums">Type Coercion: unions and enums</a></li>
<li><a id="toc-Type-Coercion-Zero-Bit-Types" href="#Type-Coercion-Zero-Bit-Types">Type Coercion: Zero Bit Types</a></li>
<li><a id="toc-Type-Coercion-undefined" href="#Type-Coercion-undefined">Type Coercion: undefined</a></li>
</ul></li>
<li><a id="toc-Explicit-Casts" href="#Explicit-Casts">Explicit Casts</a></li>
<li><a id="toc-Peer-Type-Resolution" href="#Peer-Type-Resolution">Peer Type Resolution</a></li>
</ul></li>
<li><a id="toc-Zero-Bit-Types" href="#Zero-Bit-Types">Zero Bit Types</a>
<ul>
<li><a id="toc-void" href="#void">void</a></li>
<li><a id="toc-Pointers-to-Zero-Bit-Types" href="#Pointers-to-Zero-Bit-Types">Pointers to Zero Bit Types</a></li>
</ul></li>
<li><a id="toc-Result-Location-Semantics" href="#Result-Location-Semantics">Result Location Semantics</a></li>
<li><a id="toc-usingnamespace" href="#usingnamespace">usingnamespace</a></li>
<li><a id="toc-comptime" href="#comptime">comptime</a>
<ul>
<li><a id="toc-Introducing-the-Compile-Time-Concept" href="#Introducing-the-Compile-Time-Concept">Introducing the Compile-Time Concept</a>
<ul>
<li><a id="toc-Compile-Time-Parameters" href="#Compile-Time-Parameters">Compile-Time Parameters</a></li>
<li><a id="toc-Compile-Time-Variables" href="#Compile-Time-Variables">Compile-Time Variables</a></li>
<li><a id="toc-Compile-Time-Expressions" href="#Compile-Time-Expressions">Compile-Time Expressions</a></li>
</ul></li>
<li><a id="toc-Generic-Data-Structures" href="#Generic-Data-Structures">Generic Data Structures</a></li>
<li><a id="toc-Case-Study-printf-in-Zig" href="#Case-Study-printf-in-Zig">Case Study: printf in Zig</a></li>
</ul></li>
<li><a id="toc-Assembly" href="#Assembly">Assembly</a>
<ul>
<li><a id="toc-Output-Constraints" href="#Output-Constraints">Output Constraints</a></li>
<li><a id="toc-Input-Constraints" href="#Input-Constraints">Input Constraints</a></li>
<li><a id="toc-Clobbers" href="#Clobbers">Clobbers</a></li>
<li><a id="toc-Global-Assembly" href="#Global-Assembly">Global Assembly</a></li>
</ul></li>
<li><a id="toc-Atomics" href="#Atomics">Atomics</a></li>
<li><a id="toc-Async-Functions" href="#Async-Functions">Async Functions</a>
<ul>
<li><a id="toc-Suspend-and-Resume" href="#Suspend-and-Resume">Suspend and Resume</a>
<ul>
<li><a id="toc-Resuming-from-Suspend-Blocks" href="#Resuming-from-Suspend-Blocks">Resuming from Suspend Blocks</a></li>
</ul></li>
<li><a id="toc-Async-and-Await" href="#Async-and-Await">Async and Await</a></li>
<li><a id="toc-Async-Function-Example" href="#Async-Function-Example">Async Function Example</a></li>
</ul></li>
<li><a id="toc-Builtin-Functions" href="#Builtin-Functions">Builtin Functions</a>
<ul style="columns: 2">
<li><a id="toc-addWithOverflow" href="#addWithOverflow">@addWithOverflow</a></li>
<li><a id="toc-alignCast" href="#alignCast">@alignCast</a></li>
<li><a id="toc-alignOf" href="#alignOf">@alignOf</a></li>
<li><a id="toc-as" href="#as">@as</a></li>
<li><a id="toc-asyncCall" href="#asyncCall">@asyncCall</a></li>
<li><a id="toc-atomicLoad" href="#atomicLoad">@atomicLoad</a></li>
<li><a id="toc-atomicRmw" href="#atomicRmw">@atomicRmw</a></li>
<li><a id="toc-atomicStore" href="#atomicStore">@atomicStore</a></li>
<li><a id="toc-bitCast" href="#bitCast">@bitCast</a></li>
<li><a id="toc-bitOffsetOf" href="#bitOffsetOf">@bitOffsetOf</a></li>
<li><a id="toc-boolToInt" href="#boolToInt">@boolToInt</a></li>
<li><a id="toc-bitSizeOf" href="#bitSizeOf">@bitSizeOf</a></li>
<li><a id="toc-breakpoint" href="#breakpoint">@breakpoint</a></li>
<li><a id="toc-mulAdd" href="#mulAdd">@mulAdd</a></li>
<li><a id="toc-byteSwap" href="#byteSwap">@byteSwap</a></li>
<li><a id="toc-bitReverse" href="#bitReverse">@bitReverse</a></li>
<li><a id="toc-offsetOf" href="#offsetOf">@offsetOf</a></li>
<li><a id="toc-call" href="#call">@call</a></li>
<li><a id="toc-cDefine" href="#cDefine">@cDefine</a></li>
<li><a id="toc-cImport" href="#cImport">@cImport</a></li>
<li><a id="toc-cInclude" href="#cInclude">@cInclude</a></li>
<li><a id="toc-clz" href="#clz">@clz</a></li>
<li><a id="toc-cmpxchgStrong" href="#cmpxchgStrong">@cmpxchgStrong</a></li>
<li><a id="toc-cmpxchgWeak" href="#cmpxchgWeak">@cmpxchgWeak</a></li>
<li><a id="toc-compileError" href="#compileError">@compileError</a></li>
<li><a id="toc-compileLog" href="#compileLog">@compileLog</a></li>
<li><a id="toc-ctz" href="#ctz">@ctz</a></li>
<li><a id="toc-cUndef" href="#cUndef">@cUndef</a></li>
<li><a id="toc-divExact" href="#divExact">@divExact</a></li>
<li><a id="toc-divFloor" href="#divFloor">@divFloor</a></li>
<li><a id="toc-divTrunc" href="#divTrunc">@divTrunc</a></li>
<li><a id="toc-embedFile" href="#embedFile">@embedFile</a></li>
<li><a id="toc-enumToInt" href="#enumToInt">@enumToInt</a></li>
<li><a id="toc-errorName" href="#errorName">@errorName</a></li>
<li><a id="toc-errorReturnTrace" href="#errorReturnTrace">@errorReturnTrace</a></li>
<li><a id="toc-errorToInt" href="#errorToInt">@errorToInt</a></li>
<li><a id="toc-errSetCast" href="#errSetCast">@errSetCast</a></li>
<li><a id="toc-export" href="#export">@export</a></li>
<li><a id="toc-extern" href="#extern">@extern</a></li>
<li><a id="toc-fence" href="#fence">@fence</a></li>
<li><a id="toc-field" href="#field">@field</a></li>
<li><a id="toc-fieldParentPtr" href="#fieldParentPtr">@fieldParentPtr</a></li>
<li><a id="toc-floatCast" href="#floatCast">@floatCast</a></li>
<li><a id="toc-floatToInt" href="#floatToInt">@floatToInt</a></li>
<li><a id="toc-frame" href="#frame">@frame</a></li>
<li><a id="toc-Frame" href="#Frame">@Frame</a></li>
<li><a id="toc-frameAddress" href="#frameAddress">@frameAddress</a></li>
<li><a id="toc-frameSize" href="#frameSize">@frameSize</a></li>
<li><a id="toc-hasDecl" href="#hasDecl">@hasDecl</a></li>
<li><a id="toc-hasField" href="#hasField">@hasField</a></li>
<li><a id="toc-import" href="#import">@import</a></li>
<li><a id="toc-intCast" href="#intCast">@intCast</a></li>
<li><a id="toc-intToEnum" href="#intToEnum">@intToEnum</a></li>
<li><a id="toc-intToError" href="#intToError">@intToError</a></li>
<li><a id="toc-intToFloat" href="#intToFloat">@intToFloat</a></li>
<li><a id="toc-intToPtr" href="#intToPtr">@intToPtr</a></li>
<li><a id="toc-maximum" href="#maximum">@maximum</a></li>
<li><a id="toc-memcpy" href="#memcpy">@memcpy</a></li>
<li><a id="toc-memset" href="#memset">@memset</a></li>
<li><a id="toc-minimum" href="#minimum">@minimum</a></li>
<li><a id="toc-wasmMemorySize" href="#wasmMemorySize">@wasmMemorySize</a></li>
<li><a id="toc-wasmMemoryGrow" href="#wasmMemoryGrow">@wasmMemoryGrow</a></li>
<li><a id="toc-mod" href="#mod">@mod</a></li>
<li><a id="toc-mulWithOverflow" href="#mulWithOverflow">@mulWithOverflow</a></li>
<li><a id="toc-panic" href="#panic">@panic</a></li>
<li><a id="toc-popCount" href="#popCount">@popCount</a></li>
<li><a id="toc-ptrCast" href="#ptrCast">@ptrCast</a></li>
<li><a id="toc-ptrToInt" href="#ptrToInt">@ptrToInt</a></li>
<li><a id="toc-rem" href="#rem">@rem</a></li>
<li><a id="toc-returnAddress" href="#returnAddress">@returnAddress</a></li>
<li><a id="toc-select" href="#select">@select</a></li>
<li><a id="toc-setAlignStack" href="#setAlignStack">@setAlignStack</a></li>
<li><a id="toc-setCold" href="#setCold">@setCold</a></li>
<li><a id="toc-setEvalBranchQuota" href="#setEvalBranchQuota">@setEvalBranchQuota</a></li>
<li><a id="toc-setFloatMode" href="#setFloatMode">@setFloatMode</a></li>
<li><a id="toc-setRuntimeSafety" href="#setRuntimeSafety">@setRuntimeSafety</a></li>
<li><a id="toc-shlExact" href="#shlExact">@shlExact</a></li>
<li><a id="toc-shlWithOverflow" href="#shlWithOverflow">@shlWithOverflow</a></li>
<li><a id="toc-shrExact" href="#shrExact">@shrExact</a></li>
<li><a id="toc-shuffle" href="#shuffle">@shuffle</a></li>
<li><a id="toc-sizeOf" href="#sizeOf">@sizeOf</a></li>
<li><a id="toc-splat" href="#splat">@splat</a></li>
<li><a id="toc-reduce" href="#reduce">@reduce</a></li>
<li><a id="toc-src" href="#src">@src</a></li>
<li><a id="toc-sqrt" href="#sqrt">@sqrt</a></li>
<li><a id="toc-sin" href="#sin">@sin</a></li>
<li><a id="toc-cos" href="#cos">@cos</a></li>
<li><a id="toc-exp" href="#exp">@exp</a></li>
<li><a id="toc-exp2" href="#exp2">@exp2</a></li>
<li><a id="toc-log" href="#log">@log</a></li>
<li><a id="toc-log2" href="#log2">@log2</a></li>
<li><a id="toc-log10" href="#log10">@log10</a></li>
<li><a id="toc-fabs" href="#fabs">@fabs</a></li>
<li><a id="toc-floor" href="#floor">@floor</a></li>
<li><a id="toc-ceil" href="#ceil">@ceil</a></li>
<li><a id="toc-trunc" href="#trunc">@trunc</a></li>
<li><a id="toc-round" href="#round">@round</a></li>
<li><a id="toc-subWithOverflow" href="#subWithOverflow">@subWithOverflow</a></li>
<li><a id="toc-tagName" href="#tagName">@tagName</a></li>
<li><a id="toc-This" href="#This">@This</a></li>
<li><a id="toc-truncate" href="#truncate">@truncate</a></li>
<li><a id="toc-Type" href="#Type">@Type</a></li>
<li><a id="toc-typeInfo" href="#typeInfo">@typeInfo</a></li>
<li><a id="toc-typeName" href="#typeName">@typeName</a></li>
<li><a id="toc-TypeOf" href="#TypeOf">@TypeOf</a></li>
<li><a id="toc-unionInit" href="#unionInit">@unionInit</a></li>
</ul></li>
<li><a id="toc-Build-Mode" href="#Build-Mode">Build Mode</a>
<ul>
<li><a id="toc-Debug" href="#Debug">Debug</a></li>
<li><a id="toc-ReleaseFast" href="#ReleaseFast">ReleaseFast</a></li>
<li><a id="toc-ReleaseSafe" href="#ReleaseSafe">ReleaseSafe</a></li>
<li><a id="toc-ReleaseSmall" href="#ReleaseSmall">ReleaseSmall</a></li>
</ul></li>
<li><a id="toc-Single-Threaded-Builds" href="#Single-Threaded-Builds">Single Threaded Builds</a></li>
<li><a id="toc-Undefined-Behavior" href="#Undefined-Behavior">Undefined Behavior</a>
<ul>
<li><a id="toc-Reaching-Unreachable-Code" href="#Reaching-Unreachable-Code">Reaching Unreachable Code</a></li>
<li><a id="toc-Index-out-of-Bounds" href="#Index-out-of-Bounds">Index out of Bounds</a></li>
<li><a id="toc-Cast-Negative-Number-to-Unsigned-Integer" href="#Cast-Negative-Number-to-Unsigned-Integer">Cast Negative Number to Unsigned Integer</a></li>
<li><a id="toc-Cast-Truncates-Data" href="#Cast-Truncates-Data">Cast Truncates Data</a></li>
<li><a id="toc-Integer-Overflow" href="#Integer-Overflow">Integer Overflow</a>
<ul>
<li><a id="toc-Default-Operations" href="#Default-Operations">Default Operations</a></li>
<li><a id="toc-Standard-Library-Math-Functions" href="#Standard-Library-Math-Functions">Standard Library Math Functions</a></li>
<li><a id="toc-Builtin-Overflow-Functions" href="#Builtin-Overflow-Functions">Builtin Overflow Functions</a></li>
<li><a id="toc-Wrapping-Operations" href="#Wrapping-Operations">Wrapping Operations</a></li>
</ul></li>
<li><a id="toc-Exact-Left-Shift-Overflow" href="#Exact-Left-Shift-Overflow">Exact Left Shift Overflow</a></li>
<li><a id="toc-Exact-Right-Shift-Overflow" href="#Exact-Right-Shift-Overflow">Exact Right Shift Overflow</a></li>
<li><a id="toc-Division-by-Zero" href="#Division-by-Zero">Division by Zero</a></li>
<li><a id="toc-Remainder-Division-by-Zero" href="#Remainder-Division-by-Zero">Remainder Division by Zero</a></li>
<li><a id="toc-Exact-Division-Remainder" href="#Exact-Division-Remainder">Exact Division Remainder</a></li>
<li><a id="toc-Attempt-to-Unwrap-Null" href="#Attempt-to-Unwrap-Null">Attempt to Unwrap Null</a></li>
<li><a id="toc-Attempt-to-Unwrap-Error" href="#Attempt-to-Unwrap-Error">Attempt to Unwrap Error</a></li>
<li><a id="toc-Invalid-Error-Code" href="#Invalid-Error-Code">Invalid Error Code</a></li>
<li><a id="toc-Invalid-Enum-Cast" href="#Invalid-Enum-Cast">Invalid Enum Cast</a></li>
<li><a id="toc-Invalid-Error-Set-Cast" href="#Invalid-Error-Set-Cast">Invalid Error Set Cast</a></li>
<li><a id="toc-Incorrect-Pointer-Alignment" href="#Incorrect-Pointer-Alignment">Incorrect Pointer Alignment</a></li>
<li><a id="toc-Wrong-Union-Field-Access" href="#Wrong-Union-Field-Access">Wrong Union Field Access</a></li>
<li><a id="toc-Out-of-Bounds-Float-to-Integer-Cast" href="#Out-of-Bounds-Float-to-Integer-Cast">Out of Bounds Float to Integer Cast</a></li>
<li><a id="toc-Pointer-Cast-Invalid-Null" href="#Pointer-Cast-Invalid-Null">Pointer Cast Invalid Null</a></li>
</ul></li>
<li><a id="toc-Memory" href="#Memory">Memory</a>
<ul>
<li><a id="toc-Choosing-an-Allocator" href="#Choosing-an-Allocator">Choosing an Allocator</a></li>
<li><a id="toc-Where-are-the-bytes" href="#Where-are-the-bytes">Where are the bytes?</a></li>
<li><a id="toc-Implementing-an-Allocator" href="#Implementing-an-Allocator">Implementing an Allocator</a></li>
<li><a id="toc-Heap-Allocation-Failure" href="#Heap-Allocation-Failure">Heap Allocation Failure</a></li>
<li><a id="toc-Recursion" href="#Recursion">Recursion</a></li>
<li><a id="toc-Lifetime-and-Ownership" href="#Lifetime-and-Ownership">Lifetime and Ownership</a></li>
</ul></li>
<li><a id="toc-Compile-Variables" href="#Compile-Variables">Compile Variables</a></li>
<li><a id="toc-Root-Source-File" href="#Root-Source-File">Root Source File</a></li>
<li><a id="toc-Zig-Build-System" href="#Zig-Build-System">Zig Build System</a>
<ul>
<li><a id="toc-Building-an-Executable" href="#Building-an-Executable">Building an Executable</a></li>
<li><a id="toc-Building-a-Library" href="#Building-a-Library">Building a Library</a></li>
<li><a id="toc-Compiling-C-Source-Code" href="#Compiling-C-Source-Code">Compiling C Source Code</a></li>
</ul></li>
<li><a id="toc-C" href="#C">C</a>
<ul>
<li><a id="toc-C-Type-Primitives" href="#C-Type-Primitives">C Type Primitives</a></li>
<li><a id="toc-Import-from-C-Header-File" href="#Import-from-C-Header-File">Import from C Header File</a></li>
<li><a id="toc-C-Translation-CLI" href="#C-Translation-CLI">C Translation CLI</a>
<ul>
<li><a id="toc-Command-line-flags" href="#Command-line-flags">Command line flags</a></li>
<li><a id="toc-Using--target-and--cflags" href="#Using--target-and--cflags">Using -target and -cflags</a></li>
<li><a id="toc-cImport-vs-translate-c" href="#cImport-vs-translate-c">@cImport vs translate-c</a></li>
</ul></li>
<li><a id="toc-C-Translation-Caching" href="#C-Translation-Caching">C Translation Caching</a></li>
<li><a id="toc-Translation-failures" href="#Translation-failures">Translation failures</a></li>
<li><a id="toc-C-Macros" href="#C-Macros">C Macros</a></li>
<li><a id="toc-C-Pointers" href="#C-Pointers">C Pointers</a></li>
<li><a id="toc-Exporting-a-C-Library" href="#Exporting-a-C-Library">Exporting a C Library</a></li>
<li><a id="toc-Mixing-Object-Files" href="#Mixing-Object-Files">Mixing Object Files</a></li>
</ul></li>
<li><a id="toc-WebAssembly" href="#WebAssembly">WebAssembly</a>
<ul>
<li><a id="toc-Freestanding" href="#Freestanding">Freestanding</a></li>
<li><a id="toc-WASI" href="#WASI">WASI</a></li>
</ul></li>
<li><a id="toc-Targets" href="#Targets">Targets</a></li>
<li><a id="toc-Style-Guide" href="#Style-Guide">Style Guide</a>
<ul>
<li><a id="toc-Whitespace" href="#Whitespace">Whitespace</a></li>
<li><a id="toc-Names" href="#Names">Names</a></li>
<li><a id="toc-Examples" href="#Examples">Examples</a></li>
<li><a id="toc-Doc-Comment-Guidance" href="#Doc-Comment-Guidance">Doc Comment Guidance</a></li>
</ul></li>
<li><a id="toc-Source-Encoding" href="#Source-Encoding">Source Encoding</a></li>
<li><a id="toc-Keyword-Reference" href="#Keyword-Reference">Keyword Reference</a></li>
<li><a id="toc-Grammar" href="#Grammar">Grammar</a></li>
<li><a id="toc-Zen" href="#Zen">Zen</a></li>
</ul>
</nav>
</div>
<div id="contents-wrapper"><main id="contents">
<h1>Zig Language Reference</h1>
<h2 id="Introduction"><a href="#toc-Introduction">Introduction</a> <a class="hdr" href="#Introduction">§</a></h2>
<p>
<a href="https://ziglang.org">Zig</a> is a general-purpose programming language and toolchain for maintaining
<strong>robust</strong>, <strong>optimal</strong>, and <strong>reusable</strong> software.
</p>
<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
other programmers. The language imposes a low overhead to reading code and is
resilient to changing requirements and environments.</dd>
</dl>
<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>
The code samples in this document are compiled and tested as part of the main test suite of Zig.
</p>
<p>
This HTML document depends on no external files, so you can use it offline.
</p>
<h2 id="Zig-Standard-Library"><a href="#toc-Zig-Standard-Library">Zig Standard Library</a> <a class="hdr" href="#Zig-Standard-Library">§</a></h2>
<p>
The <a href="https://ziglang.org/documentation/master/std/">Zig Standard Library</a> has its own documentation.
</p>
<p>
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.
</p>
<h2 id="Hello-World"><a href="#toc-Hello-World">Hello World</a> <a class="hdr" href="#Hello-World">§</a></h2>
<figure><figcaption class="zig-cap"><cite class="file">hello.zig</cite></figcaption><pre><code><span class="tok-kw">const</span> std = <span class="tok-builtin">@import</span>(<span class="tok-str">"std"</span>);
<span class="tok-kw">pub</span> <span class="tok-kw">fn</span> <span class="tok-fn">main</span>() !<span class="tok-type">void</span> {
<span class="tok-kw">const</span> stdout = std.io.getStdOut().writer();
<span class="tok-kw">try</span> stdout.print(<span class="tok-str">"Hello, {s}!\n"</span>, .{<span class="tok-str">"world"</span>});
}</code></pre></figure><figure><figcaption class="shell-cap">Shell</figcaption><pre><samp>$ <kbd>zig build-exe hello.zig</kbd>
$ <kbd>./hello</kbd>
Hello, world!
</samp></pre></figure>
<p>
The Zig code sample above demonstrates one way to create a program that will output: <samp>Hello, world!</samp>.
</p>
<p>
The code sample shows the contents of a file named <code class="file">hello.zig</code>. Files storing Zig
source code are <a href="#Source-Encoding">UTF-8 encoded</a> text files. The files storing
Zig source code are usually named with the <code class="file"><em>.zig</em></code> extension.
</p>
<p>
Following the <code class="file">hello.zig</code> Zig code sample, the <a href="#Zig-Build-System">Zig Build System</a> is used
to build an executable program from the <code class="file">hello.zig</code> source code. Then, the
<code class="file">hello</code> program is executed showing its output <samp>Hello, world!</samp>. The
lines beginning with <samp>$</samp> represent command line prompts and a command.
Everything else is program output.
</p>
<p>
The code sample begins by adding the <a href="#Zig-Standard-Library">Zig Standard Library</a> to the build using the <a href="#import">@import</a> builtin function.
The <code><span class="tok-builtin">@import</span>(<span class="tok-str">"std"</span>)</code> function call creates a structure that represents the Zig Standard Library.
The code then <a href="#Container-Level-Variables">declares</a> a
<a href="#Assignment">constant identifier</a>, named <code>std</code>, that gives access the features of the Zig Standard Library.
</p>
<p>
Next, a <a href="#Functions">public function</a>, <code><span class="tok-kw">pub</span> <span class="tok-kw">fn</span></code>, named <code>main</code>
is declared. The <code>main</code> function is necessary because it tells the Zig compiler where the start of
the program exists. Programs designed to be executed will need a <code><span class="tok-kw">pub</span> <span class="tok-kw">fn</span> <span class="tok-fn">main</span></code> function.
</p>
<aside>
<p>
For more advanced use cases, Zig offers other features to inform the compiler where the start of
the program exists. Also, libraries do not need a <code><span class="tok-kw">pub</span> <span class="tok-kw">fn</span> <span class="tok-fn">main</span></code> function because
library code is called by other programs or libraries.
</p>
</aside>
<p>
A function is a block of any number of statements and expressions that, as a whole, perform a task.
Functions may or may not return data after they are done performing their task. If a function
cannot perform its task, it might return an error. Zig makes all of this explicit.
</p>
<p>
In the <code class="file">hello.zig</code> code sample, the <code>main</code> function is declared
with the <code>!<span class="tok-type">void</span></code> return type. This return type is known as an <a href="#Error-Union-Type">Error Union Type</a>.
This syntax tells the Zig compiler that the function will either return an
error or a value. An error union type combines an <a href="#Error-Set-Type">Error Set Type</a> and any other data type
(e.g. a <a href="#Primitive-Types">Primitive Type</a> or a user-defined type such as a <a href="#struct">struct</a>, <a href="#enum">enum</a>, or <a href="#union">union</a>).
The full form of an error union type is
<code><error set type></code><code>!</code><code><any data type></code>. In the code
sample, the error set type is not explicitly written on the left side of the <code>!</code> operator.
When written this way, the error set type is an <a href="#Inferred-Error-Sets">inferred error set type</a>. The
<code><span class="tok-type">void</span></code> after the <code>!</code> operator
tells the compiler that the function will not return a value under normal circumstances (i.e. when no errors occur).
</p>
<aside>
<p>
Note to experienced programmers: Zig also has the boolean <a href="#Operators">operator</a> <code>!a</code>
where <code>a</code> is a value of type <code><span class="tok-type">bool</span></code>. Error union types contain the
name of the type in the syntax: <code>!</code><code><any data type></code>.
</p>
</aside>
<p>
In Zig, a function's block of statements and expressions are surrounded by an open curly-brace <code>{</code> and
close curly-brace <code>}</code>. Inside of the <code>main</code> function are expressions that perform
the task of outputting <samp>Hello, world!</samp> to standard output.
</p>
<p>
First, a constant identifier, <code>stdout</code>, is initialized to represent standard output's
writer. Then, the program tries to print the <samp>Hello, world!</samp>
message to standard output.
</p>
<p>
Functions sometimes need information to perform their task. In Zig, information is passed
to functions between an open parenthesis <code>(</code> and a close parenthesis <code>)</code> placed after
the function's name. This information is also known as arguments. When there are
multiple arguments passed to a function, they are separated by commas <code>,</code>.
</p>
<p>
The two arguments passed to the <code>stdout.print()</code> function, <code><span class="tok-str">"Hello, {s}!\n"</span></code>
and <code>.{<span class="tok-str">"world"</span>}</code>, are evaluated at <a href="#comptime">compile-time</a>. The code sample is
purposely written to show how to perform <a href="#String-Literals-and-Unicode-Code-Point-Literals">string</a>
substitution in the <code>print</code> function. The curly-braces inside of the first argument
are substituted with the compile-time known value inside of the second argument
(known as an <a href="#Anonymous-Struct-Literals">anonymous struct literal</a>). The <code>\n</code>
inside of the double-quotes of the first argument is the <a href="#Escape-Sequences">escape sequence</a> for the
newline character. The <a href="#try">try</a> expression evaluates the result of <code>stdout.print</code>.
If the result is an error, then the <code><span class="tok-kw">try</span></code> expression will return from
<code>main</code> with the error. Otherwise, the program will continue. In this case, there are no
more statements or expressions left to execute in the <code>main</code> function, so the program exits.
</p>
<p>
In Zig, the standard output writer's <code>print</code> function is allowed to fail because
it is actually a function defined as part of a generic Writer. Consider a generic Writer that
represents writing data to a file. When the disk is full, a write to the file will fail.
However, we typically do not expect writing text to the standard output to fail. To avoid having
to handle the failure case of printing to standard output, you can use alternate functions: the
functions in <code>std.log</code> for proper logging or the <code>std.debug.print</code> function.
This documentation will use the latter option to print to standard error (stderr) and silently return
on failure. The next code sample, <code class="file">hello_again.zig</code> demonstrates the use of
<code>std.debug.print</code>.
</p>
<figure><figcaption class="zig-cap"><cite class="file">hello_again.zig</cite></figcaption><pre><code><span class="tok-kw">const</span> print = <span class="tok-builtin">@import</span>(<span class="tok-str">"std"</span>).debug.print;
<span class="tok-kw">pub</span> <span class="tok-kw">fn</span> <span class="tok-fn">main</span>() <span class="tok-type">void</span> {
print(<span class="tok-str">"Hello, world!\n"</span>, .{});
}</code></pre></figure><figure><figcaption class="shell-cap">Shell</figcaption><pre><samp>$ <kbd>zig build-exe hello_again.zig</kbd>
$ <kbd>./hello_again</kbd>
Hello, world!
</samp></pre></figure>
<p>
Note that you can leave off the <code>!</code> from the return type because <code>std.debug.print</code> cannot fail.
</p>
<p>See also:</p><ul>
<li><a href="#Values">Values</a></li>
<li><a href="#import">@import</a></li>
<li><a href="#Errors">Errors</a></li>
<li><a href="#Root-Source-File">Root Source File</a></li>
<li><a href="#Source-Encoding">Source Encoding</a></li>
</ul>
<h2 id="Zig-Test"><a href="#toc-Zig-Test">Zig Test</a> <a class="hdr" href="#Zig-Test">§</a></h2>
<p>
<kbd>zig test</kbd> is a tool that can be used to quickly build and run Zig code
to make sure behavior meets expectations. <code><span class="tok-builtin">@import</span>(<span class="tok-str">"builtin"</span>).is_test</code>
is available for code to detect whether the current build is a test build.
</p>
<figure><figcaption class="zig-cap"><cite class="file">detect_test.zig</cite></figcaption><pre><code><span class="tok-kw">const</span> std = <span class="tok-builtin">@import</span>(<span class="tok-str">"std"</span>);
<span class="tok-kw">const</span> builtin = <span class="tok-builtin">@import</span>(<span class="tok-str">"builtin"</span>);
<span class="tok-kw">const</span> expect = std.testing.expect;
<span class="tok-kw">test</span> <span class="tok-str">"builtin.is_test"</span> {
<span class="tok-kw">try</span> expect(builtin.is_test);
}</code></pre></figure><figure><figcaption class="shell-cap">Shell</figcaption><pre><samp>$ <kbd>zig test detect_test.zig</kbd>
Test [1/1] test "builtin.is_test"...
All 1 tests passed.
</samp></pre></figure>
<p>
Zig has lazy top level declaration analysis, which means that if a function is not called,
or otherwise used, it is not analyzed. This means that there may be an undiscovered
compile error in a function because it is never called.
</p>
<figure><figcaption class="zig-cap"><cite class="file">unused_fn.zig</cite></figcaption><pre><code><span class="tok-kw">fn</span> <span class="tok-fn">unused</span>() <span class="tok-type">i32</span> {
<span class="tok-kw">return</span> <span class="tok-str">"wrong return type"</span>;
}
<span class="tok-kw">test</span> <span class="tok-str">"unused function"</span> { }</code></pre></figure><figure><figcaption class="shell-cap">Shell</figcaption><pre><samp>$ <kbd>zig test unused_fn.zig</kbd>
Test [1/1] test "unused function"...
All 1 tests passed.
</samp></pre></figure>
<p>
Note that, while in <a href="#Debug">Debug</a> and <a href="#ReleaseSafe">ReleaseSafe</a> modes, <a href="#unreachable">unreachable</a> emits a
call to <a href="#panic">@panic</a>, in <a href="#ReleaseFast">ReleaseFast</a> and <a href="#ReleaseSmall">ReleaseSmall</a> modes, it is really
undefined behavior. The implementation of <code>std.debug.assert</code> is as
simple as:
</p>
<figure><figcaption class="zig-cap"><cite class="file">assert.zig</cite></figcaption><pre><code><span class="tok-kw">pub</span> <span class="tok-kw">fn</span> <span class="tok-fn">assert</span>(ok: <span class="tok-type">bool</span>) <span class="tok-type">void</span> {
<span class="tok-kw">if</span> (!ok) <span class="tok-kw">unreachable</span>;
}</code></pre></figure>
<p>
This means that when testing in ReleaseFast or ReleaseSmall mode, <code>assert</code>
is not sufficient to check the result of a computation:
</p>
<figure><figcaption class="zig-cap"><cite class="file">assert_release_fast_mode.zig</cite></figcaption><pre><code><span class="tok-kw">const</span> std = <span class="tok-builtin">@import</span>(<span class="tok-str">"std"</span>);
<span class="tok-kw">const</span> assert = std.debug.assert;
<span class="tok-kw">test</span> <span class="tok-str">"assert in release fast mode"</span> {
assert(<span class="tok-null">false</span>);
}</code></pre></figure>
<p>
When compiling this test in <a href="#ReleaseFast">ReleaseFast</a> mode, it invokes unchecked
<a href="#Undefined-Behavior">Undefined Behavior</a>. Since that could do anything, this documentation
cannot show you the output.
</p>
<p>
Better practice for checking the output when testing is to use <code>std.testing.expect</code>:
</p>
<figure><figcaption class="zig-cap"><cite class="file">test.zig</cite></figcaption><pre><code><span class="tok-kw">const</span> std = <span class="tok-builtin">@import</span>(<span class="tok-str">"std"</span>);
<span class="tok-kw">const</span> expect = std.testing.expect;
<span class="tok-kw">test</span> <span class="tok-str">"expect in release fast mode"</span> {
<span class="tok-kw">try</span> expect(<span class="tok-null">false</span>);
}</code></pre></figure><figure><figcaption class="shell-cap">Shell</figcaption><pre><samp>$ <kbd>zig test test.zig -O ReleaseFast</kbd>
Test [1/1] test "expect in release fast mode"...
Test [2/1] test "expect in release fast mode"... FAIL (TestUnexpectedResult)
0 passed; 0 skipped; 1 failed.
error: the following test command failed with exit code 1:
docgen_tmp/zig-cache/o/f55fc4426cb7633d83f37d04785be411/test /home/paul/Development/zig/build/zig
</samp></pre></figure>
<p>See the rest of the <code>std.testing</code> namespace for more available functions.</p>
<p>
<kbd>zig test</kbd> has a few command line parameters which affect the compilation. See
<kbd>zig --help</kbd> for a full list. The most interesting one is <kbd>--test-filter [text]</kbd>.
This makes the test build only include tests whose name contains the supplied filter text.
Again, thanks to lazy analysis, this can allow you to narrow a build to only a few functions in
isolation.
</p>
<h2 id="Comments"><a href="#toc-Comments">Comments</a> <a class="hdr" href="#Comments">§</a></h2>
<figure><figcaption class="zig-cap"><cite class="file">comments.zig</cite></figcaption><pre><code><span class="tok-kw">const</span> expect = <span class="tok-builtin">@import</span>(<span class="tok-str">"std"</span>).testing.expect;
<span class="tok-kw">test</span> <span class="tok-str">"comments"</span> {
<span class="tok-comment">// Comments in Zig start with "//" and end at the next LF byte (end of line).</span>
<span class="tok-comment">// The below line is a comment, and won't be executed.</span>
<span class="tok-comment">//expect(false);</span>
<span class="tok-kw">const</span> x = <span class="tok-null">true</span>; <span class="tok-comment">// another comment</span>
<span class="tok-kw">try</span> expect(x);
}</code></pre></figure><figure><figcaption class="shell-cap">Shell</figcaption><pre><samp>$ <kbd>zig test comments.zig</kbd>
Test [1/1] test "comments"...
All 1 tests passed.
</samp></pre></figure>
<p>
There are no multiline comments in Zig (e.g. like <code class="c">/* */</code>
comments in C). This helps allow Zig to have the property that each line
of code can be tokenized out of context.
</p>
<h3 id="Doc-comments"><a href="#toc-Doc-comments">Doc comments</a> <a class="hdr" href="#Doc-comments">§</a></h3>
<p>
A doc comment is one that begins with exactly three slashes (i.e.
<code><span class="tok-comment">///</span></code> but not <code><span class="tok-comment">////</span></code>);
multiple doc comments in a row are merged together to form a multiline
doc comment. The doc comment documents whatever immediately follows it.
</p>
<figure><figcaption class="zig-cap"><cite class="file">doc_comments.zig</cite></figcaption><pre><code><span class="tok-comment">/// A structure for storing a timestamp, with nanosecond precision (this is a</span>
<span class="tok-comment">/// multiline doc comment).</span>
<span class="tok-kw">const</span> Timestamp = <span class="tok-kw">struct</span> {
<span class="tok-comment">/// The number of seconds since the epoch (this is also a doc comment).</span>
seconds: <span class="tok-type">i64</span>, <span class="tok-comment">// signed so we can represent pre-1970 (not a doc comment)</span>
<span class="tok-comment">/// The number of nanoseconds past the second (doc comment again).</span>
nanos: <span class="tok-type">u32</span>,
<span class="tok-comment">/// Returns a `Timestamp` struct representing the Unix epoch; that is, the</span>
<span class="tok-comment">/// moment of 1970 Jan 1 00:00:00 UTC (this is a doc comment too).</span>
<span class="tok-kw">pub</span> <span class="tok-kw">fn</span> <span class="tok-fn">unixEpoch</span>() Timestamp {
<span class="tok-kw">return</span> Timestamp{
.seconds = <span class="tok-number">0</span>,
.nanos = <span class="tok-number">0</span>,
};
}
};</code></pre></figure>
<p>
Doc comments are only allowed in certain places; eventually, it will
become 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>
<h3 id="Top-Level-Doc-Comments"><a href="#toc-Top-Level-Doc-Comments">Top-Level Doc Comments</a> <a class="hdr" href="#Top-Level-Doc-Comments">§</a></h3>
<p>User documentation that doesn't belong to whatever
immediately follows it, like package-level documentation, goes
in top-level doc comments. A top-level doc comment is one that
begins with two slashes and an exclamation point:
<code><span class="tok-comment">//!</span></code>.</p>
<figure><figcaption class="zig-cap"><cite class="file">tldoc_comments.zig</cite></figcaption><pre><code><span class="tok-comment">//! This module provides functions for retrieving the current date and</span>
<span class="tok-comment">//! time with varying degrees of precision and accuracy. It does not</span>
<span class="tok-comment">//! depend on libc, but will use functions from it if available.</span></code></pre></figure>