Skip to content

Commit 2e574b2

Browse files
committed
update
1 parent 0516f48 commit 2e574b2

File tree

6 files changed

+105
-5
lines changed

6 files changed

+105
-5
lines changed

course/basic/advanced_type/array.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ outline: deep
2424

2525
关于[越界问题](https://ziglang.org/documentation/master/#Index-out-of-Bounds),zig 在编译期和运行时均有完整的越界保护和完善的堆栈错误跟踪。
2626

27+
### 解构数组
28+
29+
我们在变量声明的章节提到了,数组可以结构,再来回忆一下:
30+
31+
<<<@/code/release/array.zig#deconstruct
32+
2733
### 多维数组
2834

2935
多维数组(矩阵)实际上就是嵌套数组,我们很容易就可以创建一个多维数组出来:

course/basic/advanced_type/vector.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ outline: deep
66

77
> 向量(Vector)为我们提供了并行操纵一组同类型(布尔、整型、浮点、指针)的值的方法,它尽可能使用 `SIMD` 指令。
88
9+
向量类型使用内置函数 [@Vector](https://ziglang.org/documentation/master/#Vector) 创建
10+
911
## 基本使用
1012

1113
向量支持与底层基本类型相同的内置运算符。这些操作是按元素执行,并返回与输入向量长度相同的向量,包括:
@@ -30,6 +32,12 @@ Zig 支持任何已知的最大 2^32-1 向量长度。请注意,过长的向
3032

3133
:::
3234

35+
## 解构向量
36+
37+
和数组一样,向量也可以被解构:
38+
39+
<<<@/code/release/vector.zig#deconstruct
40+
3341
## `@splat`
3442

3543
`@splat(scalar: anytype) anytype`

course/basic/define-variable.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ zig 使用 `const` 作为关键字来声明常量,它无法再被更改,只
6969

7070
<<<@/code/release/define_variable.zig#deconstruct
7171

72+
解构表达式只能出现在块内(不在容器范围内),赋值的左侧必须由逗号分隔的列表组成,其中每个元素可以是左值(例如`var`)或变量声明:
73+
74+
<<<@/code/release/define_variable.zig#deconstruct_2
75+
76+
解构可以以 `comptime` 关键字作为前缀,在这种情况下,整个解构表达式在 `comptime` 处求值。所有声明的 `var` 都将是 `comptime var`,并且所有表达式(左值和右值)都在 `comptime` 处求值。
77+
7278
##
7379

7480
块(block)用于限制变量声明的范围,例如以下代码是非法的:

course/code/14/array.zig

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,28 @@ const CreateArray = struct {
2121
// #endregion create_array
2222
};
2323

24+
const Deconstruct = struct {
25+
// #region deconstruct
26+
const print = @import("std").debug.print;
27+
28+
fn swizzleRgbaToBgra(rgba: [4]u8) [4]u8 {
29+
// 解构
30+
const r, const g, const b, const a = rgba;
31+
return .{ b, g, r, a };
32+
}
33+
34+
pub fn main() void {
35+
const pos = [_]i32{ 1, 2 };
36+
// 解构
37+
const x, const y = pos;
38+
print("x = {}, y = {}\n", .{ x, y });
39+
40+
const orange: [4]u8 = .{ 255, 165, 0, 255 };
41+
print("{any}\n", .{swizzleRgbaToBgra(orange)});
42+
}
43+
// #endregion deconstruct
44+
};
45+
2446
const Matrix = struct {
2547
// #region matrix
2648
const print = @import("std").debug.print;

course/code/14/define_variable.zig

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,53 @@ const Block = struct {
137137
const 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

course/code/14/vector.zig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ const Splat = struct {
4949
}
5050
};
5151

52+
const Deconstruct = struct {
53+
// #region deconstruct
54+
const print = @import("std").debug.print;
55+
56+
pub fn unpack(x: @Vector(4, f32), y: @Vector(4, f32)) @Vector(4, f32) {
57+
const a, const c, _, _ = x;
58+
const b, const d, _, _ = y;
59+
return .{ a, b, c, d };
60+
}
61+
62+
pub fn main() void {
63+
const x: @Vector(4, f32) = .{ 1.0, 2.0, 3.0, 4.0 };
64+
const y: @Vector(4, f32) = .{ 5.0, 6.0, 7.0, 8.0 };
65+
print("{}", .{unpack(x, y)});
66+
}
67+
// #endregion deconstruct
68+
};
69+
5270
const Reduce = struct {
5371
const std = @import("std");
5472
const print = std.debug.print;

0 commit comments

Comments
 (0)