@@ -1422,7 +1422,8 @@ fn foo() i32 {
1422
1422
1423
1423
{#header_open|Thread Local Variables#}
1424
1424
<p>A variable may be specified to be a thread-local variable using the
1425
- {#syntax#}threadlocal{#endsyntax#} keyword:</p>
1425
+ {#syntax#}threadlocal{#endsyntax#} keyword,
1426
+ which makes each thread work with a separate instance of the variable:</p>
1426
1427
{#code_begin|test|test_thread_local_variables#}
1427
1428
const std = @import("std");
1428
1429
const assert = std.debug.assert;
@@ -4278,7 +4279,7 @@ const expectError = std.testing.expectError;
4278
4279
fn isFieldOptional(comptime T: type, field_index: usize) !bool {
4279
4280
const fields = @typeInfo(T).Struct.fields;
4280
4281
return switch (field_index) {
4281
- // This prong is analyzed `fields.len - 1` times with `idx` being an
4282
+ // This prong is analyzed `fields.len - 1` times with `idx` being a
4282
4283
// unique comptime-known value each time.
4283
4284
inline 0...fields.len - 1 => |idx| @typeInfo(fields[idx].type) == .Optional,
4284
4285
else => return error.IndexOutOfBounds,
@@ -4667,6 +4668,29 @@ test "for basics" {
4667
4668
sum2 += @intCast(i32, i);
4668
4669
}
4669
4670
try expect(sum2 == 10);
4671
+
4672
+ // To iterate over consecutive integers, use the range syntax.
4673
+ // Unbounded range is always a compile error.
4674
+ var sum3 : usize = 0;
4675
+ for (0..5) |i| {
4676
+ sum3 += i;
4677
+ }
4678
+ try expect(sum3 == 10);
4679
+ }
4680
+
4681
+ test "multi object for" {
4682
+ const items = [_]usize{ 1, 2, 3 };
4683
+ const items2 = [_]usize{ 4, 5, 6 };
4684
+ var count: usize = 0;
4685
+
4686
+ // Iterate over multiple objects.
4687
+ // All lengths must be equal at the start of the loop, otherwise detectable
4688
+ // illegal behavior occurs.
4689
+ for (items, items2) |i, j| {
4690
+ count += i + j;
4691
+ }
4692
+
4693
+ try expect(count == 21);
4670
4694
}
4671
4695
4672
4696
test "for reference" {
@@ -4710,8 +4734,8 @@ const expect = std.testing.expect;
4710
4734
4711
4735
test "nested break" {
4712
4736
var count: usize = 0;
4713
- outer: for ([_]i32{ 1, 2, 3, 4, 5 } ) |_| {
4714
- for ([_]i32{ 1, 2, 3, 4, 5 } ) |_| {
4737
+ outer: for (1..6 ) |_| {
4738
+ for (1..6 ) |_| {
4715
4739
count += 1;
4716
4740
break :outer;
4717
4741
}
@@ -4721,8 +4745,8 @@ test "nested break" {
4721
4745
4722
4746
test "nested continue" {
4723
4747
var count: usize = 0;
4724
- outer: for ([_]i32{ 1, 2, 3, 4, 5, 6, 7, 8 } ) |_| {
4725
- for ([_]i32{ 1, 2, 3, 4, 5 } ) |_| {
4748
+ outer: for (1..9 ) |_| {
4749
+ for (1..6 ) |_| {
4726
4750
count += 1;
4727
4751
continue :outer;
4728
4752
}
@@ -5160,7 +5184,8 @@ export fn sub(a: i8, b: i8) i8 { return a - b; }
5160
5184
5161
5185
// The extern specifier is used to declare a function that will be resolved
5162
5186
// at link time, when linking statically, or at runtime, when linking
5163
- // dynamically.
5187
+ // dynamically. The quoted identifier after the extern keyword specifies
5188
+ // the library that has the function. (e.g. "c" -> libc.so)
5164
5189
// The callconv specifier changes the calling convention of the function.
5165
5190
const WINAPI: std.builtin.CallingConvention = if (native_arch == .x86) .Stdcall else .C;
5166
5191
extern "kernel32" fn ExitProcess(exit_code: u32) callconv(WINAPI) noreturn;
@@ -8017,7 +8042,7 @@ pub const CallModifier = enum {
8017
8042
<p>{#syntax#}@TypeOf(operand){#endsyntax#} must be an integer type or an integer vector type.</p>
8018
8043
<p>{#syntax#}operand{#endsyntax#} may be an {#link|integer|Integers#} or {#link|vector|Vectors#}.</p>
8019
8044
<p>
8020
- This function counts the number of most-significant (leading in a big-Endian sense) zeroes in an integer.
8045
+ Counts the number of most-significant (leading in a big-endian sense) zeroes in an integer - "count leading zeroes" .
8021
8046
</p>
8022
8047
<p>
8023
8048
If {#syntax#}operand{#endsyntax#} is a {#link|comptime#}-known integer,
@@ -8167,7 +8192,7 @@ test "main" {
8167
8192
<p>{#syntax#}@TypeOf(operand){#endsyntax#} must be an integer type or an integer vector type.</p>
8168
8193
<p>{#syntax#}operand{#endsyntax#} may be an {#link|integer|Integers#} or {#link|vector|Vectors#}.</p>
8169
8194
<p>
8170
- This function counts the number of least-significant (trailing in a big-Endian sense) zeroes in an integer.
8195
+ Counts the number of least-significant (trailing in a big-endian sense) zeroes in an integer - "count trailing zeroes" .
8171
8196
</p>
8172
8197
<p>
8173
8198
If {#syntax#}operand{#endsyntax#} is a {#link|comptime#}-known integer,
@@ -8553,16 +8578,27 @@ test "@hasDecl" {
8553
8578
</p>
8554
8579
<ul>
8555
8580
<li>{#syntax#}@import("std"){#endsyntax#} - Zig Standard Library</li>
8556
- <li>{#syntax#}@import("builtin"){#endsyntax#} - Target-specific information.
8581
+ <li>{#syntax#}@import("builtin"){#endsyntax#} - Target-specific information
8557
8582
The command <code>zig build-exe --show-builtin</code> outputs the source to stdout for reference.
8558
8583
</li>
8559
- <li>{#syntax#}@import("root"){#endsyntax#} - Points to the root source file.
8560
- This is usually <code>src/main.zig</code> but it depends on what file is chosen to be built.
8584
+ <li>{#syntax#}@import("root"){#endsyntax#} - Root source file
8585
+ This is usually <code>src/main.zig</code> but depends on what file is built.
8561
8586
</li>
8562
8587
</ul>
8563
8588
{#see_also|Compile Variables|@embedFile#}
8564
8589
{#header_close#}
8565
8590
8591
+ {#header_open|@inComptime#}
8592
+ <pre>{#syntax#}@inComptime() bool{#endsyntax#}</pre>
8593
+ <p>
8594
+ Returns whether the builtin was run in a {#syntax#}comptime{#endsyntax#} context. The result is a compile-time constant.
8595
+ </p>
8596
+ <p>
8597
+ This can be used to provide alternative, comptime-friendly implementations of functions. It should not be used, for instance, to exclude certain functions from being evaluated at comptime.
8598
+ </p>
8599
+ {#see_also|comptime#}
8600
+ {#header_close#}
8601
+
8566
8602
{#header_open|@intCast#}
8567
8603
<pre>{#syntax#}@intCast(comptime DestType: type, int: anytype) DestType{#endsyntax#}</pre>
8568
8604
<p>
@@ -8646,40 +8682,30 @@ test "integer cast panic" {
8646
8682
{#header_close#}
8647
8683
8648
8684
{#header_open|@memcpy#}
8649
- <pre>{#syntax#}@memcpy(noalias dest: [*]u8 , noalias source: [*]const u8, byte_count: usize ) void{#endsyntax#}</pre>
8650
- <p>
8651
- This function copies bytes from one region of memory to another. {#syntax#}dest{#endsyntax#} and
8652
- {#syntax#}source{#endsyntax#} are both pointers and must not overlap.
8653
- </p>
8654
- <p>
8655
- This function is a low level intrinsic with no safety mechanisms. Most code
8656
- should not use this function, instead using something like this:
8657
- </p>
8658
- <pre> {#syntax#}for (dest, source[0..byte_count]) |*d, s| d.* = s;{#endsyntax#}</pre>
8659
- <p>
8660
- The optimizer is intelligent enough to turn the above snippet into a memcpy.
8661
- </p>
8662
- <p>There is also a standard library function for this:</p>
8663
- <pre>{#syntax#}const mem = @import("std").mem;
8664
- mem.copy(u8, dest[0..byte_count], source[0..byte_count]);{#endsyntax#}</pre >
8685
+ <pre>{#syntax#}@memcpy(noalias dest, noalias source) void{#endsyntax#}</pre>
8686
+ <p>This function copies bytes from one region of memory to another.</p>
8687
+ <p>{#syntax#}dest{#endsyntax#} must be a mutable slice, a mutable pointer to an array, or
8688
+ a mutable many-item {#link|pointer|Pointers#}. It may have any
8689
+ alignment, and it may have any element type. </p>
8690
+ <p>Likewise, {#syntax#}source{#endsyntax#} must be a mutable slice, a
8691
+ mutable pointer to an array, or a mutable many-item
8692
+ {#link|pointer|Pointers#}. It may have any alignment, and it may have any
8693
+ element type. </p>
8694
+ <p>The {#syntax#}source{#endsyntax#} element type must support {#link|Type Coercion#}
8695
+ into the {#syntax#}dest{#endsyntax#} element type. The element types may have
8696
+ different ABI size, however, that may incur a performance penalty.</p>
8697
+ <p>Similar to {#link|for#} loops, at least one of {#syntax#}source{#endsyntax#} and
8698
+ {#syntax#}dest{#endsyntax#} must provide a length, and if two lengths are provided,
8699
+ they must be equal.</p>
8700
+ <p>Finally, the two memory regions must not overlap.</p >
8665
8701
{#header_close#}
8666
8702
8667
8703
{#header_open|@memset#}
8668
- <pre>{#syntax#}@memset(dest: [*]u8, c: u8, byte_count: usize) void{#endsyntax#}</pre>
8669
- <p>
8670
- This function sets a region of memory to {#syntax#}c{#endsyntax#}. {#syntax#}dest{#endsyntax#} is a pointer.
8671
- </p>
8672
- <p>
8673
- This function is a low level intrinsic with no safety mechanisms. Most
8674
- code should not use this function, instead using something like this:
8675
- </p>
8676
- <pre>{#syntax#}for (dest[0..byte_count]) |*b| b.* = c;{#endsyntax#}</pre>
8677
- <p>
8678
- The optimizer is intelligent enough to turn the above snippet into a memset.
8679
- </p>
8680
- <p>There is also a standard library function for this:</p>
8681
- <pre>{#syntax#}const mem = @import("std").mem;
8682
- mem.set(u8, dest, c);{#endsyntax#}</pre>
8704
+ <pre>{#syntax#}@memset(dest, elem) void{#endsyntax#}</pre>
8705
+ <p>This function sets all the elements of a memory region to {#syntax#}elem{#endsyntax#}.</p>
8706
+ <p>{#syntax#}dest{#endsyntax#} must be a mutable slice or a mutable pointer to an array.
8707
+ It may have any alignment, and it may have any element type.</p>
8708
+ <p>{#syntax#}elem{#endsyntax#} is coerced to the element type of {#syntax#}dest{#endsyntax#}.</p>
8683
8709
{#header_close#}
8684
8710
8685
8711
{#header_open|@min#}
@@ -8780,7 +8806,9 @@ test "@wasmMemoryGrow" {
8780
8806
<pre>{#syntax#}@popCount(operand: anytype) anytype{#endsyntax#}</pre>
8781
8807
<p>{#syntax#}@TypeOf(operand){#endsyntax#} must be an integer type.</p>
8782
8808
<p>{#syntax#}operand{#endsyntax#} may be an {#link|integer|Integers#} or {#link|vector|Vectors#}.</p>
8783
- <p>Counts the number of bits set in an integer.</p>
8809
+ <p>
8810
+ Counts the number of bits set in an integer - "population count".
8811
+ </p>
8784
8812
<p>
8785
8813
If {#syntax#}operand{#endsyntax#} is a {#link|comptime#}-known integer,
8786
8814
the return type is {#syntax#}comptime_int{#endsyntax#}.
@@ -8812,6 +8840,8 @@ test "@wasmMemoryGrow" {
8812
8840
pub const PrefetchOptions = struct {
8813
8841
/// Whether the prefetch should prepare for a read or a write.
8814
8842
rw: Rw = .read,
8843
+ /// The data's locality in an inclusive range from 0 to 3.
8844
+ ///
8815
8845
/// 0 means no temporal locality. That is, the data can be immediately
8816
8846
/// dropped from the cache after it is accessed.
8817
8847
///
@@ -8821,12 +8851,12 @@ pub const PrefetchOptions = struct {
8821
8851
/// The cache that the prefetch should be preformed on.
8822
8852
cache: Cache = .data,
8823
8853
8824
- pub const Rw = enum {
8854
+ pub const Rw = enum(u1) {
8825
8855
read,
8826
8856
write,
8827
8857
};
8828
8858
8829
- pub const Cache = enum {
8859
+ pub const Cache = enum(u1) {
8830
8860
instruction,
8831
8861
data,
8832
8862
};
@@ -10948,7 +10978,7 @@ pub const MAKELOCAL = @compileError("unable to translate C expr: unexpected toke
10948
10978
</p>
10949
10979
<p>{#syntax#}[*c]T{#endsyntax#} - C pointer.</p>
10950
10980
<ul>
10951
- <li>Supports all the syntax of the other two pointer types.</li>
10981
+ <li>Supports all the syntax of the other two pointer types ({#syntax#}*T{#endsyntax#}) and ({#syntax#}[*]T{#endsyntax#}) .</li>
10952
10982
<li>Coerces to other pointer types, as well as {#link|Optional Pointers#}.
10953
10983
When a C pointer is coerced to a non-optional pointer, safety-checked
10954
10984
{#link|Undefined Behavior#} occurs if the address is 0.
@@ -11966,6 +11996,17 @@ fn readU32Be() u32 {}
11966
11996
</ul>
11967
11997
</td>
11968
11998
</tr>
11999
+ <tr>
12000
+ <th scope="row">
12001
+ <pre>{#syntax#}noinline{#endsyntax#}</pre>
12002
+ </th>
12003
+ <td>
12004
+ {#syntax#}noinline{#endsyntax#} disallows function to be inlined in all call sites.
12005
+ <ul>
12006
+ <li>See also {#link|Functions#}</li>
12007
+ </ul>
12008
+ </td>
12009
+ </tr>
11969
12010
<tr>
11970
12011
<th scope="row">
11971
12012
<pre>{#syntax#}nosuspend{#endsyntax#}</pre>
0 commit comments