|
781 | 781 | implementation feature, not a language semantic, so it is not guaranteed to be observable to code. |
782 | 782 | </p> |
783 | 783 | {#header_close#} |
| 784 | + |
| 785 | + {#header_open|Destructuring#} |
| 786 | + <p> |
| 787 | + A destructuring assignment can separate elements of indexable aggregate types |
| 788 | + ({#link|Tuples#}, {#link|Arrays#}, {#link|Vectors#}): |
| 789 | + </p> |
| 790 | + {#code|destructuring_to_existing.zig#} |
| 791 | + |
| 792 | + <p> |
| 793 | + A destructuring expression may only appear within a block (i.e. not at container scope). |
| 794 | + The left hand side of the assignment must consist of a comma separated list, |
| 795 | + each element of which may be either an lvalue (for instance, an existing `var`) or a variable declaration: |
| 796 | + </p> |
| 797 | + {#code|destructuring_mixed.zig#} |
| 798 | + |
| 799 | + <p> |
| 800 | + A destructure may be prefixed with the {#syntax#}comptime{#endsyntax#} keyword, in which case the entire |
| 801 | + destructure expression is evaluated at {#link|comptime#}. All {#syntax#}var{#endsyntax#}s declared would |
| 802 | + be {#syntax#}comptime var{#endsyntax#}s and all expressions (both result locations and the assignee |
| 803 | + expression) are evaluated at {#link|comptime#}. |
| 804 | + </p> |
| 805 | + |
| 806 | + {#see_also|Destructuring Tuples|Destructuring Arrays|Destructuring Vectors#} |
| 807 | + {#header_close#} |
784 | 808 | {#header_close#} |
785 | 809 | {#header_close#} |
786 | 810 | {#header_open|Zig Test#} |
@@ -1882,6 +1906,15 @@ or |
1882 | 1906 |
|
1883 | 1907 | {#see_also|Sentinel-Terminated Pointers|Sentinel-Terminated Slices#} |
1884 | 1908 | {#header_close#} |
| 1909 | + |
| 1910 | + {#header_open|Destructuring Arrays#} |
| 1911 | + <p> |
| 1912 | + Arrays can be destructured: |
| 1913 | + </p> |
| 1914 | + {#code|destructuring_arrays.zig#} |
| 1915 | + |
| 1916 | + {#see_also|Destructuring|Destructuring Tuples|Destructuring Vectors#} |
| 1917 | + {#header_close#} |
1885 | 1918 | {#header_close#} |
1886 | 1919 |
|
1887 | 1920 | {#header_open|Vectors#} |
@@ -1929,6 +1962,14 @@ or |
1929 | 1962 | </p> |
1930 | 1963 | {#see_also|@splat|@shuffle|@select|@reduce#} |
1931 | 1964 |
|
| 1965 | + {#header_open|Destructuring Vectors#} |
| 1966 | + <p> |
| 1967 | + Vectors can be destructured: |
| 1968 | + </p> |
| 1969 | + {#code|destructuring_vectors.zig#} |
| 1970 | + {#see_also|Destructuring|Destructuring Tuples|Destructuring Arrays#} |
| 1971 | + {#header_close#} |
| 1972 | + |
1932 | 1973 | {#header_close#} |
1933 | 1974 |
|
1934 | 1975 | {#header_open|Pointers#} |
@@ -2309,6 +2350,23 @@ or |
2309 | 2350 | </p> |
2310 | 2351 | {#code|test_tuples.zig#} |
2311 | 2352 |
|
| 2353 | + {#header_open|Destructuring Tuples#} |
| 2354 | + <p> |
| 2355 | + Tuples can be {#link|destructured|Destructuring#}. |
| 2356 | + </p> |
| 2357 | + <p> |
| 2358 | + Tuple destructuring is helpful for returning multiple values from a block: |
| 2359 | + </p> |
| 2360 | + {#code|destructuring_block.zig#} |
| 2361 | + |
| 2362 | + <p> |
| 2363 | + Tuple destructuring is helpful for dealing with functions and built-ins that return multiple values |
| 2364 | + as a tuple: |
| 2365 | + </p> |
| 2366 | + {#code|destructuring_return_value.zig#} |
| 2367 | + |
| 2368 | + {#see_also|Destructuring|Destructuring Arrays|Destructuring Vectors#} |
| 2369 | + {#header_close#} |
2312 | 2370 | {#header_close#} |
2313 | 2371 | {#see_also|comptime|@fieldParentPtr#} |
2314 | 2372 | {#header_close#} |
|
0 commit comments