/zig-snippets

Useful Zig code snippets

Primary LanguageZigCreative Commons Attribution 4.0 InternationalCC-BY-4.0

Zig Snippets

Welcome to zig-snippets, a repository of useful Zig code snippets.

Zig is a "general-purpose programming language and toolchain for maintaining robust, optimal, and reusable software." You can learn more about Zig at ziglang.org.

This repository will serve as a catalog of categorized Zig code, and I encourage any contributions of useful Zig code snippets. Together, we are all more successful.

NOTE: Zig is not stable, and the compiler will implement breaking changes! If a Zig snippet does not work, this very well may be the reason.

License

This document is proudly licensed under the terms of the Creative Commons Attribution 4.0 International (CC BY 4.0) license.

All code in the snippets/ directory is proudly licensed under the terms of the Mozilla Public License v. 2.0.

For more information on the Free Culture Movement, please visit Wikipedia.

Table of Contents

  1. Introduction
  2. Input/Output
  3. Strings

Introduction

Introductory code snippets

Arrays

Here is how to write a basic array in Zig:

// Specify the length inside the square brackets, with the type after
// Use {}s as the container (same as [] in Python, Ruby, etc.)
const odd_nums = [5]i8{ 1, 3, 5, 7, 9 };

// The length can also be inferred
const even_nums = [_]i8{ 2, 4, 6, 8, 10 };

// String literals are simply pointers to array literals
const greet = "Hello!";
const greet_arr_literal = [_]u8{ 'H', 'e', 'l', 'l', '0', '!' };

Assignment

Here is how to assign to variables in Zig:

// Mutable variable
var age: i32 = 29;

// Immutable variable
const num_of_cores: i32 = 4;

Enums

Here is how to write enums in Zig:

// Simple enum
const InstructionSet = enum {
    _6502,
    x86,
    mips,
    risc_v,
}

// Enums can also be given functions
const InstructionSet = enum {
    ...
    pub fn isRiscV(self: InstructionSet) bool {
        return self == InstructionSet.risc_v;
    }
}

Functions

Here is how to write functions in Zig:

fn add(x: i32, y: i32) i32 {
    return x + y;
}

Main function

Here is how the main() function is written in Zig:

const std = @import("std");

pub fn main() void {
    std.debug.print("Hello, {}!\n", .{"World"});
}

IO

Standard Output

Here is how to write to standard output in Zig:

const std = @import("std");

// Because we are using the 'try' keyword, we have to prefix '!'
// to the return type
pub fn main() !void {
    const stdout = std.io.getStdOut().outStream();
    _ = try stdout.write("Hello, World!\n");
}

Strings

String code snippsets

Multiline strings

Here is how to write multi-line strings in Zig:

// Here is a multi-line string literal
const hello_world_in_python =
    \\def greet():
    \\    print('Hello, World!')
    \\
    \\if __name__ == '__main__':
    \\    greet()
;