|
777 | 777 | implementation feature, not a language semantic, so it is not guaranteed to be observable to code. |
778 | 778 | </p> |
779 | 779 | {#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 assignment 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#}. All {#syntax#}var{#endsyntax#}s declared would |
| 798 | + be {#syntax#}comptime var{#endsyntax#}s and all expressions (both result locations and the assignee |
| 799 | + expression) are evaluated at {#link|comptime#}. |
| 800 | + </p> |
| 801 | + |
| 802 | + {#see_also|Destructuring Tuples|Destructuring Arrays|Destructuring Vectors#} |
| 803 | + {#header_close#} |
780 | 804 | {#header_close#} |
781 | 805 | {#header_close#} |
782 | 806 | {#header_open|Zig Test#} |
@@ -1878,6 +1902,15 @@ or |
1878 | 1902 |
|
1879 | 1903 | {#see_also|Sentinel-Terminated Pointers|Sentinel-Terminated Slices#} |
1880 | 1904 | {#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#} |
1881 | 1914 | {#header_close#} |
1882 | 1915 |
|
1883 | 1916 | {#header_open|Vectors#} |
@@ -1925,6 +1958,14 @@ or |
1925 | 1958 | </p> |
1926 | 1959 | {#see_also|@splat|@shuffle|@select|@reduce#} |
1927 | 1960 |
|
| 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 | + |
1928 | 1969 | {#header_close#} |
1929 | 1970 |
|
1930 | 1971 | {#header_open|Pointers#} |
@@ -2297,6 +2338,23 @@ or |
2297 | 2338 | </p> |
2298 | 2339 | {#code|test_tuples.zig#} |
2299 | 2340 |
|
| 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#} |
2300 | 2358 | {#header_close#} |
2301 | 2359 | {#see_also|comptime|@fieldParentPtr#} |
2302 | 2360 | {#header_close#} |
|
0 commit comments