Basics

Zig Running Code

Running Zig Code

Zig code runs via zig run or zig build with .zig files.

Introduction to Running Zig Code

Zig is a powerful programming language that emphasizes simplicity and performance. Before diving into Zig's syntax and features, it's essential to understand how to execute Zig code. This guide will cover the basic commands for running Zig files, using both zig run and zig build commands.

Using zig run Command

The zig run command is a convenient way to compile and execute a Zig program in one step. This command is particularly useful during the development phase when you want to test your code quickly.

To run the above Zig program, navigate to the directory containing hello.zig and execute the following command in your terminal:

This command will compile and immediately run the hello.zig program, displaying "Hello, World!" in the terminal.

Using zig build Command

The zig build command is more suited for building projects with multiple files or when you need to configure the build process with additional options. This command involves creating a build.zig file to manage the project's build process.

With the build.zig file in place, you can compile the project with:

This will generate an executable in the zig-out/bin directory, which you can run with:

The zig build command is ideal for larger projects where you need more control over the build configuration.

Conclusion

Both zig run and zig build are powerful tools in the Zig developer's toolkit. Understanding when and how to use each will streamline your development process and prepare you for more complex projects. Next, you'll learn about Zig's syntax in the following post of this series.