Zig: A Basic Introduction

Zig: A Basic Introduction

Zig is a modern programming language designed to be robust, efficient, and easy to understand. It focuses on providing predictable performance and is often used for system programming, similar to C or Rust. It emphasizes safety, simplicity, and the absence of hidden control flow, making it a suitable choice for developers who need low-level control.

Installation

To start using Zig, you need to install it on your system. Here’s how you can do that:

  • Download Zig:
  • Extract and Install:
  • On Linux/macOS:
    bash tar xvfz zig-linux-x86_64-<version>.tar.gz cd zig-linux-x86_64-<version>/
  • On Windows:
    • Extract the zip file to your desired directory.
  • Add Zig to PATH (Optional): Add the Zig binary directory to your PATH for easier access.

Hello World in Zig

Let’s start with a simple “Hello, World!” program in Zig.

  1. Create a new file called hello.zig and open it in your favorite text editor.
  2. Write the following code:
const std = @import("std");

pub fn main() void {
    std.debug.print("Hello, world!\n", .{});
}
  1. Compile and Run:
  • To compile the program, open your terminal (or command prompt on Windows) and run:
    zig build-exe hello.zig
  • This will generate an executable named hello. Run it: ./hello You should see “Hello, World!” printed to your terminal.

Basic Syntax and Features

Variables

Zig uses const for immutable variables and var for mutable ones.

const x = 10;  // Immutable
var y = 20;    // Mutable

var message = "hello world";
const z: u8 = 255; //uint of size 8
var floats: f32 = 100.0; //float of size 32

Functions

Functions in Zig are defined using the fn keyword.

fn add(a: i32, b: i32) i32 {
    return a + b;
}

const result = add(5, 10);

Control Flow

Zig supports standard control flow structures like if, while, and for.

const n = 10;

if (n > 5) {
    std.debug.print("n is greater than 5\n", .{});
} else {
    std.debug.print("n is not greater than 5\n", .{});
}

Error Handling

Zig has a unique error-handling system using the try, catch, and error keywords.

fn openFile() !void {
    const file = try std.fs.cwd().openFile("example.txt", .{});
    file.close();
}

pub fn main() void {
    openFile() catch |err| {
        std.debug.print("Failed to open file: {}\n", .{err});
    };
}

Top Level Declarations

In zig, top level declarations such as global variables are order-independent and lazily analyzed. Here is an example on what I mean:

var y: i32 = add(10, x);
const x: i32 = add(12, 34);

test "global variables" {
    assert(x == 46);
    assert(y == 56);
}

fn add(a: i32, b: i32) i32 {
    return a + b;
}

const std = @import("std");
const assert = std.debug.assert;

Running this with zig test global_variables.zig will return “All 1 tests passed.”

Building Projects

Zig provides a powerful build system that can be used to manage more complex projects.

  1. Create a new project:
   zig init-exe

This will create a new Zig project with a src directory and a build.zig file.

  1. Build and run:
   zig build run

This will compile and run your project.

If you wish to learn more about Zig, their official website has a long list of tutorials and examples within the Learn page. Happy Coding!

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *