@@ -137,13 +137,53 @@ const Block = struct {
137137const Deconstruct = struct {
138138 fn main () void {
139139 // #region deconstruct
140+ const print = @import ("std" ).debug .print ;
141+ var x : u32 = undefined ;
142+ var y : u32 = undefined ;
140143 var z : u32 = undefined ;
141- // var z: u32 = undefined;
142- const x , var y , z = [3 ]u32 { 1 , 2 , 3 };
143- y += 10 ;
144- // x 是 1,y 是 2,z 是 3
144+ // 元组
145+ const tuple = .{ 1 , 2 , 3 };
146+ // 解构元组
147+ x , y , z = tuple ;
148+
149+ print ("tuple: x = {}, y = {}, z = {}\n " , .{ x , y , z });
150+ // 数组
151+ const array = [_ ]u32 { 4 , 5 , 6 };
152+ // 解构数组
153+ x , y , z = array ;
154+
155+ print ("array: x = {}, y = {}, z = {}\n " , .{ x , y , z });
156+ // 向量定义
157+ const vector : @Vector (3 , u32 ) = .{ 7 , 8 , 9 };
158+ // 解构向量
159+ x , y , z = vector ;
160+
161+ print ("vector: x = {}, y = {}, z = {}\n " , .{ x , y , z });
145162 // #endregion deconstruct
146- _ = x ;
163+
164+ }
165+ };
166+
167+ const Deconstruct_2 = struct {
168+ pub fn main () ! void {
169+ // #region deconstruct_2
170+ const print = @import ("std" ).debug .print ;
171+ var x : u32 = undefined ;
172+
173+ const tuple = .{ 1 , 2 , 3 };
174+
175+ x , var y : u32 , const z = tuple ;
176+
177+ print ("x = {}, y = {}, z = {}\n " , .{ x , y , z });
178+
179+ // y 可变
180+ y = 100 ;
181+
182+ // 可以用 _ 丢弃不想要的值
183+ _ , x , _ = tuple ;
184+
185+ print ("x = {}" , .{x });
186+ // #endregion deconstruct_2
147187 }
148188};
149189
0 commit comments