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" {...@@ -2367,7 +2367,7 @@ test "iterate over an array" {
2367var some_integers: [100]i32 = undefined;2367var some_integers: [100]i32 = undefined;
23682368
2369test "modify an array" {2369test "modify an array" {
2370 for (some_integers) |*item, i| {2370 for (&some_integers, 0..) |*item, i| {
2371 item.* = @intCast(i32, i);2371 item.* = @intCast(i32, i);
2372 }2372 }
2373 try expect(some_integers[10] == 10);2373 try expect(some_integers[10] == 10);
...@@ -2408,7 +2408,7 @@ comptime {...@@ -2408,7 +2408,7 @@ comptime {
2408// use compile-time code to initialize an array2408// use compile-time code to initialize an array
2409var fancy_array = init: {2409var fancy_array = init: {
2410 var initial_value: [10]Point = undefined;2410 var initial_value: [10]Point = undefined;
2411 for (initial_value) |*pt, i| {2411 for (&initial_value, 0..) |*pt, i| {
2412 pt.* = Point{2412 pt.* = Point{
2413 .x = @intCast(i32, i),2413 .x = @intCast(i32, i),
2414 .y = @intCast(i32, i) * 2,2414 .y = @intCast(i32, i) * 2,
...@@ -2461,8 +2461,8 @@ test "multidimensional arrays" {...@@ -2461,8 +2461,8 @@ test "multidimensional arrays" {
2461 try expect(mat4x4[1][1] == 1.0);2461 try expect(mat4x4[1][1] == 1.0);
24622462
2463 // Here we iterate with for loops.2463 // Here we iterate with for loops.
2464 for (mat4x4) |row, row_index| {2464 for (mat4x4, 0..) |row, row_index| {
2465 for (row) |cell, column_index| {2465 for (row, 0..) |cell, column_index| {
2466 if (row_index == column_index) {2466 if (row_index == column_index) {
2467 try expect(cell == 1.0);2467 try expect(cell == 1.0);
2468 }2468 }
...@@ -3579,7 +3579,7 @@ test "tuple" {...@@ -3579,7 +3579,7 @@ test "tuple" {
3579 } ++ .{false} ** 2;3579 } ++ .{false} ** 2;
3580 try expect(values[0] == 1234);3580 try expect(values[0] == 1234);
3581 try expect(values[4] == false);3581 try expect(values[4] == false);
3582 inline for (values) |v, i| {3582 inline for (values, 0..) |v, i| {
3583 if (i != 2) continue;3583 if (i != 2) continue;
3584 try expect(v);3584 try expect(v);
3585 }3585 }
...@@ -4659,10 +4659,10 @@ test "for basics" {...@@ -4659,10 +4659,10 @@ test "for basics" {
4659 }4659 }
4660 try expect(sum == 20);4660 try expect(sum == 20);
46614661
4662 // To access the index of iteration, specify a second capture value.4662 // To access the index of iteration, specify a second condition as well
4663 // This is zero-indexed.4663 // as a second capture value.
4664 var sum2: i32 = 0;4664 var sum2: i32 = 0;
4665 for (items) |_, i| {4665 for (items, 0..) |_, i| {
4666 try expect(@TypeOf(i) == usize);4666 try expect(@TypeOf(i) == usize);
4667 sum2 += @intCast(i32, i);4667 sum2 += @intCast(i32, i);
4668 }4668 }
...@@ -4674,7 +4674,7 @@ test "for reference" {...@@ -4674,7 +4674,7 @@ test "for reference" {
46744674
4675 // Iterate over the slice by reference by4675 // Iterate over the slice by reference by
4676 // specifying that the capture value is a pointer.4676 // specifying that the capture value is a pointer.
4677 for (items) |*value| {4677 for (&items) |*value| {
4678 value.* += 1;4678 value.* += 1;
4679 }4679 }
46804680
...@@ -5659,7 +5659,7 @@ fn genFoos(allocator: Allocator, num: usize) ![]Foo {...@@ -5659,7 +5659,7 @@ fn genFoos(allocator: Allocator, num: usize) ![]Foo {
5659 var foos = try allocator.alloc(Foo, num);5659 var foos = try allocator.alloc(Foo, num);
5660 errdefer allocator.free(foos);5660 errdefer allocator.free(foos);
56615661
5662 for(foos) |*foo, i| {5662 for (foos, 0..) |*foo, i| {
5663 foo.data = try allocator.create(u32);5663 foo.data = try allocator.create(u32);
5664 // This errdefer does not last between iterations5664 // This errdefer does not last between iterations
5665 errdefer allocator.destroy(foo.data);5665 errdefer allocator.destroy(foo.data);
...@@ -5700,14 +5700,14 @@ fn genFoos(allocator: Allocator, num: usize) ![]Foo {...@@ -5700,14 +5700,14 @@ fn genFoos(allocator: Allocator, num: usize) ![]Foo {
5700 // Used to track how many foos have been initialized5700 // Used to track how many foos have been initialized
5701 // (including their data being allocated)5701 // (including their data being allocated)
5702 var num_allocated: usize = 0;5702 var num_allocated: usize = 0;
5703 errdefer for(foos[0..num_allocated]) |foo| {5703 errdefer for (foos[0..num_allocated]) |foo| {
5704 allocator.destroy(foo.data);5704 allocator.destroy(foo.data);
5705 };5705 };
5706 for(foos) |*foo, i| {5706 for (foos, 0..) |*foo, i| {
5707 foo.data = try allocator.create(u32);5707 foo.data = try allocator.create(u32);
5708 num_allocated += 1;5708 num_allocated += 1;
57095709
5710 if(i >= 3) return error.TooManyFoos;5710 if (i >= 3) return error.TooManyFoos;
57115711
5712 foo.data.* = try getData();5712 foo.data.* = try getData();
5713 }5713 }
...@@ -7265,7 +7265,7 @@ const Writer = struct {...@@ -7265,7 +7265,7 @@ const Writer = struct {
7265 comptime var state = State.start;7265 comptime var state = State.start;
7266 comptime var next_arg: usize = 0;7266 comptime var next_arg: usize = 0;
72677267
7268 inline for (format) |c, i| {7268 inline for (format, 0..) |c, i| {
7269 switch (state) {7269 switch (state) {
7270 State.start => switch (c) {7270 State.start => switch (c) {
7271 '{' => {7271 '{' => {
...@@ -8629,7 +8629,7 @@ test "integer cast panic" {...@@ -8629,7 +8629,7 @@ test "integer cast panic" {
8629 This function is a low level intrinsic with no safety mechanisms. Most code8629 This function is a low level intrinsic with no safety mechanisms. Most code
8630 should not use this function, instead using something like this:8630 should not use this function, instead using something like this:
8631 </p>8631 </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>
8633 <p>8633 <p>
8634 The optimizer is intelligent enough to turn the above snippet into a memcpy.8634 The optimizer is intelligent enough to turn the above snippet into a memcpy.
8635 </p>8635 </p>
...@@ -11116,7 +11116,7 @@ pub fn main() !void {...@@ -11116,7 +11116,7 @@ pub fn main() !void {
11116 const args = try std.process.argsAlloc(gpa);11116 const args = try std.process.argsAlloc(gpa);
11117 defer std.process.argsFree(gpa, args);11117 defer std.process.argsFree(gpa, args);
1111811118
11119 for (args) |arg, i| {11119 for (args, 0..) |arg, i| {
11120 std.debug.print("{}: {s}\n", .{ i, arg });11120 std.debug.print("{}: {s}\n", .{ i, arg });
11121 }11121 }
11122}11122}
...@@ -11142,7 +11142,7 @@ pub fn main() !void {...@@ -11142,7 +11142,7 @@ pub fn main() !void {
1114211142
11143 const preopens = try fs.wasi.preopensAlloc(arena);11143 const preopens = try fs.wasi.preopensAlloc(arena);
1114411144
11145 for (preopens.names) |preopen, i| {11145 for (preopens.names, 0..) |preopen, i| {
11146 std.debug.print("{}: {s}\n", .{ i, preopen });11146 std.debug.print("{}: {s}\n", .{ i, preopen });
11147 }11147 }
11148}11148}