04_arrays.zig 840 B

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