-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathindex.html
13617 lines (11825 loc) · 750 KB
/
index.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>
body{
font-family: system-ui, -apple-system, Roboto, "Segoe UI", sans-serif;
margin: 0;
}
a:not(:hover) {
text-decoration: none;
}
table, th, td {
border-collapse: collapse;
border: 1px solid grey;
}
th, td {
padding: 0.1em;
}
.t0_1, .t37, .t37_1 {
font-weight: bold;
}
.t2_0 {
color: grey;
}
.t31_1 {
color: red;
}
.t32_1 {
color: green;
}
.t36_1 {
color: #0086b3;
}
.file {
text-decoration: underline;
}
pre,code {
font-size: 12pt;
}
pre > code {
display: block;
overflow: auto;
padding: 0.5em;
color: #333;
background: #f8f8f8;
}
.table-wrapper {
width: 100%;
overflow-y: auto;
}
.tok-kw {
color: #333;
font-weight: bold;
}
.tok-str {
color: #d14;
}
.tok-builtin {
color: #0086b3;
}
.tok-comment {
color: #777;
font-style: italic;
}
.tok-fn {
color: #900;
font-weight: bold;
}
.tok-null {
color: #008080;
}
.tok-number {
color: #008080;
}
.tok-type {
color: #458;
font-weight: bold;
}
#main-wrapper {
display: flex;
flex-direction: column;
height: 100vh;
}
#contents-wrapper {
flex-grow: 1;
padding: 0 2em;
}
#contents {
max-width: 60em;
margin: auto;
}
#toc {
padding: 0 1em;
}
@media screen and (min-width: 1025px) {
#main-wrapper {
flex-direction: row;
}
#contents-wrapper, #toc {
overflow: auto;
}
}
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;
}
@media (prefers-color-scheme: dark) {
body{
background-color:#111;
color: #bbb;
}
a {
color: #88f;
}
table, th, td {
border-color: grey;
}
.t2_0 {
color: grey;
}
.t31_1 {
color: red;
}
.t32_1 {
color: green;
}
.t36_1 {
color: #0086b3;
}
pre > code {
color: #ccc;
background: #222;
}
.tok-kw {
color: #eee;
}
.tok-str {
color: #2e5;
}
.tok-builtin {
color: #ff894c;
}
.tok-comment {
color: #aa7;
}
.tok-fn {
color: #e33;
}
.tok-null {
color: #ff8080;
}
.tok-number {
color: #ff8080;
}
.tok-type {
color: #68f;
}
h1 a, h2 a, h3 a, h4 a, h5 a {
color: #aaa;
}
}
</style>
</head>
<body>
<div id="main-wrapper">
<div id="toc">
<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.0/">0.7.0</a> |
master
<h1>Contents</h1>
<ul>
<li><a id="toc-Introduction" href="#Introduction">Introduction</a></li>
<li><a id="toc-Hello-World" href="#Hello-World">Hello World</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-Global-Variables" href="#Global-Variables">Global 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-packed-enum" href="#packed-enum">packed 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-Arrays-and-Pointers" href="#Type-Coercion-Arrays-and-Pointers">Type Coercion: 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-byteOffsetOf" href="#byteOffsetOf">@byteOffsetOf</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-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-memcpy" href="#memcpy">@memcpy</a></li>
<li><a id="toc-memset" href="#memset">@memset</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-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-Test" href="#Zig-Test">Zig Test</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-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>
</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>
</div>
<div id="contents-wrapper"><div id="contents">
<h1 id="Introduction"><a href="#toc-Introduction">Introduction</a> <a class="hdr" href="#Introduction">§</a></h1>
<p>
Zig is a general-purpose programming language and toolchain for maintaining
<strong>robust</strong>, <strong>optimal</strong>, and <strong>reusable</strong> software.
</p>
<ul>
<li><strong>Robust</strong> - behavior is correct even for edge cases such as out of memory.</li>
<li><strong>Optimal</strong> - write programs the best way they can behave and perform.</li>
<li><strong>Reusable</strong> - the same code works in many environments which have different
constraints.</li>
<li><strong>Maintainable</strong> - 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.</li>
</ul>
<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>
If you search for something specific in this documentation and do not find it,
please <a href="https://github.com/ziglang/www.ziglang.org/issues/new?title=I%20searched%20for%20___%20in%20the%20docs%20and%20didn%27t%20find%20it">file an issue</a> or <a href="https://webchat.freenode.net/?channels=%23zig">say something on IRC</a>.
</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>
<p>
<a href="https://github.com/ziglang/zig/wiki/FAQ#where-is-the-documentation-for-the-zig-standard-library">Where is the documentation for the Zig standard library?</a>
</p>
<h1 id="Hello-World"><a href="#toc-Hello-World">Hello World</a> <a class="hdr" href="#Hello-World">§</a></h1>
<p class="file">hello.zig</p><pre><code class="zig"><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><pre><code class="shell">$ zig build-exe hello.zig
$ ./hello
Hello, world!
</code></pre>
<p>
The Zig code sample above demonstrates one way to create a program that will output <code>Hello, world!</code>.
</p>
<p>
The code sample shows the contents of a file named <code>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>.zig</code> extension.
</p>
<p>
Following the <code>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>hello.zig</code> source code. Then, the
<code>hello</code> program is executed showing its output <code>Hello, world!</code>. The
lines beginning with <code>$</code> represent command line prompts and a command.
Everything else is program output.
</p>
<p>
The code sample begins by adding Zig's Standard Library to the build using the <a href="#import">@import</a> builtin function.
The <code class="zig"><span class="tok-builtin">@import</span>(<span class="tok-str">"std"</span>)</code> function call creates a structure to represent the Standard Library.
The code then makes a <a href="#Global-Variables">top-level declaration</a> of a
<a href="#Assignment">constant identifier</a>, named <code>std</code>, for easy access to
<a href="https://github.com/ziglang/zig/wiki/FAQ#where-is-the-documentation-for-the-zig-standard-library">Zig's standard library</a>.
</p>
<p>
Next, a <a href="#Functions">public function</a>, <code class="zig"><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 class="zig"><span class="tok-kw">pub</span> <span class="tok-kw">fn</span> <span class="tok-fn">main</span></code> function.
For more advanced use cases, Zig offers other features to inform the compiler where the start of
the program exists. Libraries, on the other hand, do not need a <code>main</code> function because
library code is usually called by other programs.
</p>
<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>hello.zig</code> code sample, the <code>main</code> function is declared
with the <code class="zig">!<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 a <a href="#Primitive-Types">Primitive Type</a>.
The full form of an error union type is
<code><error set type></code><code class="zig">!</code><code><primitive type></code>. In the code
sample, the error set type is not explicitly written on the left side of the <code class="zig">!</code> operator.
When written this way, the error set type is a special kind of error union type that has an
<a href="#Inferred-Error-Sets">inferred error set type</a>. The <code class="zig"><span class="tok-type">void</span></code> after the <code class="zig">!</code> operator
tells the compiler that the function will not return a value under normal circumstances (i.e. no errors occur).
</p>
<p>
Note to experienced programmers: Zig also has the boolean <a href="#Operators">operator</a> <code class="zig">!a</code>
where <code class="zig">a</code> is a value of type <code class="zig"><span class="tok-type">bool</span></code>. Error union types contain the
name of the type in the syntax: <code class="zig">!</code><code><primitive type></code>.
</p>
<p>
In Zig, a function's block of statements and expressions are surrounded by <code>{</code> and
<code>}</code> curly-braces. Inside of the <code>main</code> function are expressions that perform
the task of outputting <code>Hello, world!</code> 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 <code>Hello, world!</code>
message to standard output.
</p>
<p>
Functions sometimes need information to perform their task. In Zig, information is passed
to functions between open <code>(</code> and close <code>)</code> parenthesis 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>"Hello, {s}!\n"</code>
and <code>.{"world"}</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 class="zig"><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>hello_again.zig</code> demonstrates the use of
<code>std.debug.print</code>.
</p>
<p class="file">hello_again.zig</p><pre><code class="zig"><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><pre><code class="shell">$ zig build-exe hello_again.zig
$ ./hello_again
Hello, world!
</code></pre>
<p>
Note that you can leave off the <code class="zig">!</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>
<h1 id="Comments"><a href="#toc-Comments">Comments</a> <a class="hdr" href="#Comments">§</a></h1>
<p class="file">comments.zig</p><pre><code class="zig"><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>
expect(x);
}</code></pre><pre><code class="shell">$ zig test comments.zig
Test [1/1] test "comments"...
All 1 tests passed.
</code></pre>
<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>
<h2 id="Doc-comments"><a href="#toc-Doc-comments">Doc comments</a> <a class="hdr" href="#Doc-comments">§</a></h2>
<p>
A doc comment is one that begins with exactly three slashes (i.e.
<code class="zig"><span class="tok-comment">///</span></code> but not <code class="zig"><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>
<pre><code class="zig"><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>
<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>
<h2 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></h2>
<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 class="zig"><span class="tok-comment">//!</span></code>.</p>
<pre><code class="zig"><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>
<h1 id="Values"><a href="#toc-Values">Values</a> <a class="hdr" href="#Values">§</a></h1>
<p class="file">values.zig</p><pre><code class="zig"><span class="tok-comment">// Top-level declarations are order-independent:</span>
<span class="tok-kw">const</span> print = std.debug.print;
<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> os = std.os;
<span class="tok-kw">const</span> assert = std.debug.assert;
<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-comment">// integers</span>
<span class="tok-kw">const</span> one_plus_one: <span class="tok-type">i32</span> = <span class="tok-number">1</span> + <span class="tok-number">1</span>;
print(<span class="tok-str">"1 + 1 = {}\n"</span>, .{one_plus_one});
<span class="tok-comment">// floats</span>
<span class="tok-kw">const</span> seven_div_three: <span class="tok-type">f32</span> = <span class="tok-number">7.0</span> / <span class="tok-number">3.0</span>;
print(<span class="tok-str">"7.0 / 3.0 = {}\n"</span>, .{seven_div_three});
<span class="tok-comment">// boolean</span>
print(<span class="tok-str">"{}\n{}\n{}\n"</span>, .{
<span class="tok-null">true</span> <span class="tok-kw">and</span> <span class="tok-null">false</span>,
<span class="tok-null">true</span> <span class="tok-kw">or</span> <span class="tok-null">false</span>,
!<span class="tok-null">true</span>,
});
<span class="tok-comment">// optional</span>
<span class="tok-kw">var</span> optional_value: ?[]<span class="tok-kw">const</span> <span class="tok-type">u8</span> = <span class="tok-null">null</span>;
assert(optional_value == <span class="tok-null">null</span>);
print(<span class="tok-str">"\noptional 1\ntype: {s}\nvalue: {s}\n"</span>, .{
<span class="tok-builtin">@typeName</span>(<span class="tok-builtin">@TypeOf</span>(optional_value)),
optional_value,
});
optional_value = <span class="tok-str">"hi"</span>;
assert(optional_value != <span class="tok-null">null</span>);
print(<span class="tok-str">"\noptional 2\ntype: {s}\nvalue: {s}\n"</span>, .{
<span class="tok-builtin">@typeName</span>(<span class="tok-builtin">@TypeOf</span>(optional_value)),
optional_value,
});
<span class="tok-comment">// error union</span>
<span class="tok-kw">var</span> number_or_error: <span class="tok-type">anyerror</span>!<span class="tok-type">i32</span> = <span class="tok-kw">error</span>.ArgNotFound;
print(<span class="tok-str">"\nerror union 1\ntype: {s}\nvalue: {}\n"</span>, .{
<span class="tok-builtin">@typeName</span>(<span class="tok-builtin">@TypeOf</span>(number_or_error)),
number_or_error,
});
number_or_error = <span class="tok-number">1234</span>;
print(<span class="tok-str">"\nerror union 2\ntype: {s}\nvalue: {}\n"</span>, .{
<span class="tok-builtin">@typeName</span>(<span class="tok-builtin">@TypeOf</span>(number_or_error)),
number_or_error,
});
}</code></pre><pre><code class="shell">$ zig build-exe values.zig
$ ./values
1 + 1 = 2
7.0 / 3.0 = 2.33333325e+00
false
true
false
optional 1
type: ?[]const u8
value: null
optional 2
type: ?[]const u8
value: hi
error union 1
type: anyerror!i32
value: error.ArgNotFound
error union 2
type: anyerror!i32
value: 1234
</code></pre>
<h2 id="Primitive-Types"><a href="#toc-Primitive-Types">Primitive Types</a> <a class="hdr" href="#Primitive-Types">§</a></h2>
<div class="table-wrapper">
<table>
<tr>
<th>
Name
</th>
<th>
C Equivalent
</th>
<th>
Description
</th>
</tr>
<tr>
<td><code class="zig"><span class="tok-type">i8</span></code></td>
<td><code class="c">int8_t</code></td>
<td>signed 8-bit integer</td>
</tr>
<tr>
<td><code class="zig"><span class="tok-type">u8</span></code></td>
<td><code class="c">uint8_t</code></td>
<td>unsigned 8-bit integer</td>
</tr>
<tr>
<td><code class="zig"><span class="tok-type">i16</span></code></td>
<td><code class="c">int16_t</code></td>
<td>signed 16-bit integer</td>
</tr>
<tr>
<td><code class="zig"><span class="tok-type">u16</span></code></td>
<td><code class="c">uint16_t</code></td>
<td>unsigned 16-bit integer</td>
</tr>
<tr>
<td><code class="zig"><span class="tok-type">i32</span></code></td>
<td><code class="c">int32_t</code></td>
<td>signed 32-bit integer</td>
</tr>
<tr>
<td><code class="zig"><span class="tok-type">u32</span></code></td>
<td><code class="c">uint32_t</code></td>
<td>unsigned 32-bit integer</td>
</tr>
<tr>
<td><code class="zig"><span class="tok-type">i64</span></code></td>
<td><code class="c">int64_t</code></td>
<td>signed 64-bit integer</td>
</tr>
<tr>
<td><code class="zig"><span class="tok-type">u64</span></code></td>
<td><code class="c">uint64_t</code></td>
<td>unsigned 64-bit integer</td>
</tr>
<tr>
<td><code class="zig"><span class="tok-type">i128</span></code></td>
<td><code class="c">__int128</code></td>
<td>signed 128-bit integer</td>
</tr>
<tr>
<td><code class="zig"><span class="tok-type">u128</span></code></td>
<td><code class="c">unsigned __int128</code></td>
<td>unsigned 128-bit integer</td>
</tr>
<tr>
<td><code class="zig"><span class="tok-type">isize</span></code></td>
<td><code class="c">intptr_t</code></td>
<td>signed pointer sized integer</td>
</tr>
<tr>
<td><code class="zig"><span class="tok-type">usize</span></code></td>
<td><code class="c">uintptr_t</code></td>
<td>unsigned pointer sized integer</td>
</tr>
<tr>
<td><code class="zig"><span class="tok-type">c_short</span></code></td>
<td><code class="c">short</code></td>
<td>for ABI compatibility with C</td>
</tr>
<tr>
<td><code class="zig"><span class="tok-type">c_ushort</span></code></td>
<td><code class="c">unsigned short</code></td>
<td>for ABI compatibility with C</td>
</tr>
<tr>
<td><code class="zig"><span class="tok-type">c_int</span></code></td>
<td><code class="c">int</code></td>
<td>for ABI compatibility with C</td>
</tr>
<tr>
<td><code class="zig"><span class="tok-type">c_uint</span></code></td>
<td><code class="c">unsigned int</code></td>
<td>for ABI compatibility with C</td>
</tr>
<tr>
<td><code class="zig"><span class="tok-type">c_long</span></code></td>
<td><code class="c">long</code></td>
<td>for ABI compatibility with C</td>
</tr>
<tr>
<td><code class="zig"><span class="tok-type">c_ulong</span></code></td>
<td><code class="c">unsigned long</code></td>
<td>for ABI compatibility with C</td>
</tr>
<tr>
<td><code class="zig"><span class="tok-type">c_longlong</span></code></td>
<td><code class="c">long long</code></td>
<td>for ABI compatibility with C</td>
</tr>