Dave Gauer 4 лет назад
Родитель
Сommit
87541c0c8b
3 измененных файлов с 34 добавлено и 2 удалено
  1. 31 0
      04_arrays.zig
  2. 1 1
      README.md
  3. 2 1
      ziglings

+ 31 - 0
04_arrays.zig

@@ -0,0 +1,31 @@
+//
+// Let's learn some array basics. Arrays literals are declared with:
+//
+//   [size]<type>{ values };
+//
+// When Zig can infer the size of the array, you can use '_' for the
+// size like so:
+//
+//   [_]<type>{ values };
+//
+const std = @import("std");
+
+pub fn main() void {
+    const some_primes = [_]u8{ 2, 3, 5, 7, 11, 13, 17, 19 };
+
+    // Array values are accessed using square bracket '[]' notation.
+    //
+    // (Note that when Zig can infer the type (u8 in this case) of a
+    // value, we don't have to manually specify it.)
+    //
+    const first = some_primes[0];
+
+    // Looks like we need to complete this expression:
+    const fourth = ???;
+
+    // Use '.len' to get the length of the array:
+    const length = some_primes.???;
+
+    std.debug.print("First: {}, Fourth: {}, Length: {}\n",
+        .{first, fourth, length});
+}

+ 1 - 1
README.md

@@ -60,7 +60,7 @@ Planned exercises:
 * [x] Hello world (main needs to be public)
 * [x] Importing standard library
 * [x] Assignment
-* [ ] Arrays
+* [x] Arrays
 * [ ] If
 * [ ] While
 * [ ] For

+ 2 - 1
ziglings

@@ -67,7 +67,8 @@ function check_it {
 
 check_it 01_hello.zig "Hello world" "Note the error: the source file has a hint for fixing 'main'."
 check_it 02_std.zig "Standard Library"
-check_it 03_assignment.zig "55 314159 -11"
+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."
 
 echo
 echo "    __   __          _ "