constants = immutable and must have value
var = mutable
const c: i32 = 5; // signed 32bit constantconst inferred_c = @as(i32, 5); // explicit declarationconst a: i32 = undefined; // if not val is defined, use "undefined", but must have value at initdoes not allow for unused values, unused can be placed into _ var
Defer is used to execute a statement upon exiting the current block.
test "defer" { var x: i16 = 5; { defer x += 2; try expect(x == 5); } try expect(x == 7);}get length
const length = a.len //5works as an expression
All function arguments are immutable - if a copy is desired the user must explicitly make one. Unlike variables, which are snake_case, functions are camelCase. Here's an example of declaring and calling a simple function.
must have an else to capture anything outside of scope
test "switch expression" { var x: i8 = 10; x = switch (x) { -1...1 => -x, 10, 100 => @divExact(x, 10), else => x, }; try expect(x == 1);}no built in string type, strings declared with U8
const mystring: [] const u8 = "this is a string"; // mystring is a slice of u8 valuesconcat
const part1 = "Hello";const part2 = "World";const message = part1 ++ ", " ++ part2 ++ "!";equality and ordering
const str1 = "apple";const str2 = "banana";zig fetch --save=yaml https://github.com/kubkon/zig-yaml/archive/refs/tags/0.0.1.tar.gz
create a new zig project
zig initcompile and run without building executable
zig run src/main.zigcreate a new binary
zig build-exe src/main.zig