authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-02-18 14:55:02-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-02-18 19:20:19-07:00
log7abeb52abc9e9e38d8af4e17e25e89083cdec397
treeeab2d90b45b7c29d33e80b4cdda86cbc3ca8c865
parent4dd958d585256df3119d5617d22492f41ed02884

langref: update to new for loop syntax


1 files changed, 17 insertions(+), 17 deletions(-)

doc/langref.html.in+17-17
......@@ -2367,7 +2367,7 @@ test "iterate over an array" {
23672367var some_integers: [100]i32 = undefined;
23682368
23692369test "modify an array" {
2370 for (some_integers) |*item, i| {
2370 for (&some_integers, 0..) |*item, i| {
23712371 item.* = @intCast(i32, i);
23722372 }
23732373 try expect(some_integers[10] == 10);
......@@ -2408,7 +2408,7 @@ comptime {
24082408// use compile-time code to initialize an array
24092409var fancy_array = init: {
24102410 var initial_value: [10]Point = undefined;
2411 for (initial_value) |*pt, i| {
2411 for (&initial_value, 0..) |*pt, i| {
24122412 pt.* = Point{
24132413 .x = @intCast(i32, i),
24142414 .y = @intCast(i32, i) * 2,
......@@ -2461,8 +2461,8 @@ test "multidimensional arrays" {
24612461 try expect(mat4x4[1][1] == 1.0);
24622462
24632463 // Here we iterate with for loops.
2464 for (mat4x4) |row, row_index| {
2465 for (row) |cell, column_index| {
2464 for (mat4x4, 0..) |row, row_index| {
2465 for (row, 0..) |cell, column_index| {
24662466 if (row_index == column_index) {
24672467 try expect(cell == 1.0);
24682468 }
......@@ -3579,7 +3579,7 @@ test "tuple" {
35793579 } ++ .{false} ** 2;
35803580 try expect(values[0] == 1234);
35813581 try expect(values[4] == false);
3582 inline for (values) |v, i| {
3582 inline for (values, 0..) |v, i| {
35833583 if (i != 2) continue;
35843584 try expect(v);
35853585 }
......@@ -4659,10 +4659,10 @@ test "for basics" {
46594659 }
46604660 try expect(sum == 20);
46614661
4662 // To access the index of iteration, specify a second capture value.
4663 // This is zero-indexed.
4662 // To access the index of iteration, specify a second condition as well
4663 // as a second capture value.
46644664 var sum2: i32 = 0;
4665 for (items) |_, i| {
4665 for (items, 0..) |_, i| {
46664666 try expect(@TypeOf(i) == usize);
46674667 sum2 += @intCast(i32, i);
46684668 }
......@@ -4674,7 +4674,7 @@ test "for reference" {
46744674
46754675 // Iterate over the slice by reference by
46764676 // specifying that the capture value is a pointer.
4677 for (items) |*value| {
4677 for (&items) |*value| {
46784678 value.* += 1;
46794679 }
46804680
......@@ -5659,7 +5659,7 @@ fn genFoos(allocator: Allocator, num: usize) ![]Foo {
56595659 var foos = try allocator.alloc(Foo, num);
56605660 errdefer allocator.free(foos);
56615661
5662 for(foos) |*foo, i| {
5662 for (foos, 0..) |*foo, i| {
56635663 foo.data = try allocator.create(u32);
56645664 // This errdefer does not last between iterations
56655665 errdefer allocator.destroy(foo.data);
......@@ -5700,14 +5700,14 @@ fn genFoos(allocator: Allocator, num: usize) ![]Foo {
57005700 // Used to track how many foos have been initialized
57015701 // (including their data being allocated)
57025702 var num_allocated: usize = 0;
5703 errdefer for(foos[0..num_allocated]) |foo| {
5703 errdefer for (foos[0..num_allocated]) |foo| {
57045704 allocator.destroy(foo.data);
57055705 };
5706 for(foos) |*foo, i| {
5706 for (foos, 0..) |*foo, i| {
57075707 foo.data = try allocator.create(u32);
57085708 num_allocated += 1;
57095709
5710 if(i >= 3) return error.TooManyFoos;
5710 if (i >= 3) return error.TooManyFoos;
57115711
57125712 foo.data.* = try getData();
57135713 }
......@@ -7265,7 +7265,7 @@ const Writer = struct {
72657265 comptime var state = State.start;
72667266 comptime var next_arg: usize = 0;
72677267
7268 inline for (format) |c, i| {
7268 inline for (format, 0..) |c, i| {
72697269 switch (state) {
72707270 State.start => switch (c) {
72717271 '{' => {
......@@ -8629,7 +8629,7 @@ test "integer cast panic" {
86298629 This function is a low level intrinsic with no safety mechanisms. Most code
86308630 should not use this function, instead using something like this:
86318631 </p>
8632 <pre>{#syntax#}for (source[0..byte_count]) |b, i| dest[i] = b;{#endsyntax#}</pre>
8632 <pre>{#syntax#}for (dest, source[0..byte_count]) |*d, s| d.* = s;{#endsyntax#}</pre>
86338633 <p>
86348634 The optimizer is intelligent enough to turn the above snippet into a memcpy.
86358635 </p>
......@@ -11116,7 +11116,7 @@ pub fn main() !void {
1111611116 const args = try std.process.argsAlloc(gpa);
1111711117 defer std.process.argsFree(gpa, args);
1111811118
11119 for (args) |arg, i| {
11119 for (args, 0..) |arg, i| {
1112011120 std.debug.print("{}: {s}\n", .{ i, arg });
1112111121 }
1112211122}
......@@ -11142,7 +11142,7 @@ pub fn main() !void {
1114211142
1114311143 const preopens = try fs.wasi.preopensAlloc(arena);
1114411144
11145 for (preopens.names) |preopen, i| {
11145 for (preopens.names, 0..) |preopen, i| {
1114611146 std.debug.print("{}: {s}\n", .{ i, preopen });
1114711147 }
1114811148}