04_arrays.zig 963 B

123456789101112131415161718192021222324252627282930313233
  1. //
  2. // Let's learn some array basics. Arrays are declared with:
  3. //
  4. // const foo [size]<type> = [size]<type>{ values };
  5. //
  6. // When Zig can infer the size of the array, you can use '_' for the
  7. // size. You can also let Zig infer the type of the value so the
  8. // declaration is much less verbose.
  9. //
  10. // const foo = [_]<type>{ values };
  11. //
  12. const std = @import("std");
  13. pub fn main() void {
  14. const some_primes = [_]u8{ 1, 3, 5, 7, 11, 13, 17, 19 };
  15. // Individual values can be set with '[]' notation. Let's fix
  16. // the first prime (it should be 2!):
  17. some_primes[0] = 2;
  18. // Individual values can also be accessed with '[]' notation.
  19. const first = some_primes[0];
  20. // Looks like we need to complete this expression (like 'first'):
  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. }