ziglings/04_arrays.zig

34 lines
963 B
Zig
Raw Normal View History

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