Dave Gauer 4 лет назад
Родитель
Сommit
faa49abb06
2 измененных файлов с 41 добавлено и 0 удалено
  1. 40 0
      05_arrays2.zig
  2. 1 0
      ziglings

+ 40 - 0
05_arrays2.zig

@@ -0,0 +1,40 @@
+//
+// Zig has some fun array operators.
+//
+// You can use '++' to concatenate two arrays:
+//   const a = [_]u8{ 1,2 };
+//   const b = [_]u8{ 3,4 };
+//   const c = a ++ b ++ [_]u8{ 5 }; // 1,2,3,4,5
+//
+// You can use '**' to repeat an array:
+//   const d = [_]u8{ 1,2,3 } ** 2; // 1,2,3,1,2,3
+//
+const std = @import("std");
+
+pub fn main() void {
+    const le = [_]u8{ 1, 3 };
+    const et = [_]u8{ 3, 7 };
+
+    // I want this to contain digits: 1 3 3 7
+    const leet = ???;
+
+    // I want this to contain digits: 1 0 0 1 1 0 0 1 1 0 0 1
+    const bit_pattern = [_]u8{ ??? } ** 3;
+
+
+    // We could print these arrays with leet[0], leet[1],...but let's
+    // have a little preview of Zig 'for' loops instead!
+    std.debug.print("LEET: ", .{});
+
+    for (leet) |*n| {
+        std.debug.print("{}", .{n.*});
+    }
+
+    std.debug.print(", Bits: ", .{});
+
+    for (bit_pattern) |*n| {
+        std.debug.print("{}", .{n.*});
+    }
+
+    std.debug.print("\n", .{});
+}

+ 1 - 0
ziglings

@@ -69,6 +69,7 @@ check_it 01_hello.zig "Hello world" "Note the error: the source file has a hint
 check_it 02_std.zig "Standard Library"
 check_it 03_assignment.zig "55 314159 -11" "There are three mistakes in this one!"
 check_it 04_arrays.zig "Fourth: 7, Length: 8" "There are two things to complete here."
+check_it 05_arrays2.zig "LEET: 1337, Bits: 100110011001" "Fill in the two arrays."
 
 echo
 echo "    __   __          _ "