Skip to content

Commit ee572b5

Browse files
committed
langref: Document destructuring
1 parent ea527f7 commit ee572b5

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed

doc/langref.html.in

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,30 @@
777777
implementation feature, not a language semantic, so it is not guaranteed to be observable to code.
778778
</p>
779779
{#header_close#}
780+
781+
{#header_open|Destructuring#}
782+
<p>
783+
Some aggregate values (e.g. {#link|Tuples#}, {#link|Arrays#}, {#link|Vectors#}) can be destructed using
784+
destructuring assignment:
785+
</p>
786+
{#code|destructuring_to_existing.zig#}
787+
788+
<p>
789+
A destructuring expression may only appear within a block (i.e. not at container scope).
790+
The left hand side of the assignemnt must consist of a comma separated list,
791+
each element of which may be either an {#link|result location|Result Location Semantics#} or a variable declaration:
792+
</p>
793+
{#code|destructuring_mixed.zig#}
794+
795+
<p>
796+
A destructure may be prefixed with the {#syntax#}comptime{#endsyntax#} keyword, in which case the entire
797+
destructure expression is evaluated at {#link|comptime#}.
798+
This means that all {#syntax#}var{#endsyntax#}s declared would be {#syntax#}comptime var{#endsyntax#}s
799+
and all expressions (both result locations and the assignee expression) are evaluated at {#link|comptime#}.
800+
</p>
801+
802+
{#see_also|Destructuring Tuples|Destructuring Arrays|Destructuring Vectors#}
803+
{#header_close#}
780804
{#header_close#}
781805
{#header_close#}
782806
{#header_open|Zig Test#}
@@ -1878,6 +1902,15 @@ or
18781902

18791903
{#see_also|Sentinel-Terminated Pointers|Sentinel-Terminated Slices#}
18801904
{#header_close#}
1905+
1906+
{#header_open|Destructuring Arrays#}
1907+
<p>
1908+
Arrays can be destructured:
1909+
</p>
1910+
{#code|destructuring_arrays.zig#}
1911+
1912+
{#see_also|Destructuring|Destructuring Tuples|Destructuring Vectors#}
1913+
{#header_close#}
18811914
{#header_close#}
18821915

18831916
{#header_open|Vectors#}
@@ -1925,6 +1958,14 @@ or
19251958
</p>
19261959
{#see_also|@splat|@shuffle|@select|@reduce#}
19271960

1961+
{#header_open|Destructuring Vectors#}
1962+
<p>
1963+
Vectors can be destructured:
1964+
</p>
1965+
{#code|destructuring_vectors.zig#}
1966+
{#see_also|Destructuring|Destructuring Tuples|Destructuring Arrays#}
1967+
{#header_close#}
1968+
19281969
{#header_close#}
19291970

19301971
{#header_open|Pointers#}
@@ -2297,6 +2338,23 @@ or
22972338
</p>
22982339
{#code|test_tuples.zig#}
22992340

2341+
{#header_open|Destructuring Tuples#}
2342+
<p>
2343+
Tuples can be {#link|destructured|Destructuring#}.
2344+
</p>
2345+
<p>
2346+
Destructuring is helpful for returning multiple values from a block:
2347+
</p>
2348+
{#code|destructuring_block.zig#}
2349+
2350+
<p>
2351+
Destructuring is helpful for dealing with functions and built-ins that return multiple values
2352+
as a tuple:
2353+
</p>
2354+
{#code|destructuring_return_value.zig#}
2355+
2356+
{#see_also|Destructuring|Destructuring Arrays|Destructuring Vectors#}
2357+
{#header_close#}
23002358
{#header_close#}
23012359
{#see_also|comptime|@fieldParentPtr#}
23022360
{#header_close#}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const print = @import("std").debug.print;
2+
3+
pub fn main() void {
4+
const digits = [_]i8 { 3, 8, 9, 0, 7, 4, 1 };
5+
6+
const min, const max = blk: {
7+
var min: i8 = 127;
8+
var max: i8 = -128;
9+
10+
for (digits) |digit| {
11+
if (digit < min) min = digit;
12+
if (digit > max) max = digit;
13+
}
14+
15+
break :blk .{ min, max };
16+
};
17+
18+
print("min = {}", .{ min });
19+
print("max = {}", .{ max });
20+
}
21+
22+
// exe=succeed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const print = @import("std").debug.print;
2+
3+
pub fn main() void {
4+
var x: u32 = undefined;
5+
6+
const tuple = .{ 1, 2, 3 };
7+
8+
x, var y : u32, const z = tuple;
9+
10+
print("x = {}\n", .{x});
11+
print("y = {}\n", .{y});
12+
print("z = {}\n", .{z});
13+
14+
// y are mutable
15+
y = 100;
16+
}
17+
18+
// exe=succeed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const print = @import("std").debug.print;
2+
3+
fn divmod(numerator: u32, denominator: u32) struct { u32, u32 } {
4+
return .{ numerator / denominator, numerator % denominator };
5+
}
6+
7+
pub fn main() void {
8+
const div, const mod = divmod(10, 3);
9+
10+
print("10 / 3 = {}\n", .{div});
11+
print("10 % 3 = {}\n", .{mod});
12+
}
13+
14+
// exe=succeed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const print = @import("std").debug.print;
2+
3+
pub fn main() void {
4+
var x: u32 = undefined;
5+
var y: u32 = undefined;
6+
var z: u32 = undefined;
7+
8+
const tuple = .{ 1, 2, 3 };
9+
10+
x, y, z = tuple;
11+
12+
print("x = {}\n", .{x});
13+
print("y = {}\n", .{y});
14+
print("z = {}\n", .{z});
15+
}
16+
17+
// exe=succeed

0 commit comments

Comments
 (0)