|
| 1 | +pub fn main() !void { |
| 2 | + CreateArray.main(); |
| 3 | + Deconstruct.main(); |
| 4 | + Matrix.main(); |
| 5 | + TerminatedArray.main(); |
| 6 | + Multiply.main(); |
| 7 | + Connect.main(); |
| 8 | + FuncInitArray.main(); |
| 9 | + ComptimeInitArray.main(); |
| 10 | +} |
| 11 | +const CreateArray = struct { |
| 12 | + // #region create_array |
| 13 | + const print = @import("std").debug.print; |
| 14 | + |
| 15 | + pub fn main() void { |
| 16 | + const message = [5]u8{ 'h', 'e', 'l', 'l', 'o' }; |
| 17 | + // const message = [_]u8{ 'h', 'e', 'l', 'l', 'o' }; |
| 18 | + print("{s}\n", .{message}); // hello |
| 19 | + print("{c}\n", .{message[0]}); // h |
| 20 | + } |
| 21 | + // #endregion create_array |
| 22 | +}; |
| 23 | + |
| 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 | + |
| 46 | +const Matrix = struct { |
| 47 | + // #region matrix |
| 48 | + const print = @import("std").debug.print; |
| 49 | + |
| 50 | + pub fn main() void { |
| 51 | + const matrix_4x4 = [4][4]f32{ |
| 52 | + [_]f32{ 1.0, 0.0, 0.0, 0.0 }, |
| 53 | + [_]f32{ 0.0, 1.0, 0.0, 1.0 }, |
| 54 | + [_]f32{ 0.0, 0.0, 1.0, 0.0 }, |
| 55 | + [_]f32{ 0.0, 0.0, 0.0, 1.0 }, |
| 56 | + }; |
| 57 | + |
| 58 | + for (matrix_4x4, 0..) |arr_val, arr_index| { |
| 59 | + for (arr_val, 0..) |val, index| { |
| 60 | + print("元素{}-{}是: {}\n", .{ arr_index, index, val }); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + // #endregion matrix |
| 65 | +}; |
| 66 | + |
| 67 | +const TerminatedArray = struct { |
| 68 | + // #region terminated_array |
| 69 | + const print = @import("std").debug.print; |
| 70 | + |
| 71 | + pub fn main() void { |
| 72 | + const array = [_:0]u8{ 1, 2, 3, 4 }; |
| 73 | + print("数组长度为: {}\n", .{array.len}); // 4 |
| 74 | + print("数组最后一个元素值: {}\n", .{array[array.len - 1]}); // 4 |
| 75 | + print("哨兵值为: {}\n", .{array[array.len]}); // 0 |
| 76 | + } |
| 77 | + // #endregion terminated_array |
| 78 | +}; |
| 79 | + |
| 80 | +const Multiply = struct { |
| 81 | + // #region multiply |
| 82 | + const print = @import("std").debug.print; |
| 83 | + |
| 84 | + pub fn main() void { |
| 85 | + const small = [3]i8{ 1, 2, 3 }; |
| 86 | + const big: [9]i8 = small ** 3; |
| 87 | + print("{any}\n", .{big}); // [9]i8{ 1, 2, 3, 1, 2, 3, 1, 2, 3 } |
| 88 | + } |
| 89 | + // #endregion multiply |
| 90 | +}; |
| 91 | + |
| 92 | +const Connect = struct { |
| 93 | + // #region connect |
| 94 | + const print = @import("std").debug.print; |
| 95 | + |
| 96 | + pub fn main() void { |
| 97 | + const part_one = [_]i32{ 1, 2, 3, 4 }; |
| 98 | + const part_two = [_]i32{ 5, 6, 7, 8 }; |
| 99 | + const all_of_it = part_one ++ part_two; // [_]i32{ 1, 2, 3, 4, 5, 6, 7, 8 } |
| 100 | + |
| 101 | + _ = all_of_it; |
| 102 | + } |
| 103 | + // #endregion connect |
| 104 | +}; |
| 105 | + |
| 106 | +const FuncInitArray = struct { |
| 107 | + // #region func_init_array |
| 108 | + const print = @import("std").debug.print; |
| 109 | + |
| 110 | + pub fn main() void { |
| 111 | + const array = [_]i32{make(3)} ** 10; |
| 112 | + print("{any}\n", .{array}); |
| 113 | + } |
| 114 | + |
| 115 | + fn make(x: i32) i32 { |
| 116 | + return x + 1; |
| 117 | + } |
| 118 | + // #endregion func_init_array |
| 119 | +}; |
| 120 | + |
| 121 | +const ComptimeInitArray = struct { |
| 122 | + // #region comptime_init_array |
| 123 | + const print = @import("std").debug.print; |
| 124 | + |
| 125 | + pub fn main() void { |
| 126 | + const fancy_array = init: { |
| 127 | + var initial_value: [10]usize = undefined; |
| 128 | + for (&initial_value, 0..) |*pt, i| { |
| 129 | + pt.* = i; |
| 130 | + } |
| 131 | + break :init initial_value; |
| 132 | + }; |
| 133 | + print("{any}\n", .{fancy_array}); |
| 134 | + } |
| 135 | + // #endregion comptime_init_array |
| 136 | +}; |
0 commit comments