Learn Zig from Scratch
Hello! Welcome
Today we’re going to learn about Zig from Scratch
What is Zig?
Zig is a modern programming language that focuses on performance, safety, and simplicity.
It is designed to give you full control over hardware. With Zig, you can write fast and safe programs!
Why use Zig?
Zig is popular for its efficiency and full control of memory. Its simple language allows for writing safe and predictable code.
It is ideal for embedded systems, low-level applications, and software that requires direct memory control.
Installing Zig
To get started with Zig, you need to install Zig on your computer.
- Download the appropriate file for your system from the official Zig page.
- Unzip the file and follow the instructions.
Now you can use Zig! It’s that easy!
You’re doing great!
Now let’s talk about the syntax of Zig
Variables in Zig
Variables store data that your program needs. Common types include integers (i32
, u8
), floats (f32
), and booleans (bool
).
We declare variables with var
and use const
for constants.
var x: i32 = 42; // variable
const y: f64 = 3.14; // constant
var sum = 10 + 5;
With them, we can perform arithmetic, logical, and comparison operations.
Conditionals
With conditionals, you can make the program make decisions. We use if
and else
to execute code only if a condition is true.
For example:
if (x > 10) {
// do something
}
Loops
Loops allow us to repeat actions and iterate over ranges and collections. For example, while
and for
.
var i: i32 = 0;
while (i < 5) : (i += 1) {
// code here
}
Functions in Zig
Functions are blocks of code that perform specific tasks.
fn sum(a: i32, b: i32) i32 {
return a + b;
}
Functions help you organize your code and make it clearer.
Arrays
Arrays are structures that allow you to store multiple values. They can be declared as fixed or dynamic:
const arr: [5]i32 = [5]i32{1, 2, 3, 4, 5};
Arrays can be used for fixed-size data collections.
Structures in Zig
Structures are a way to group related data. With them, you can create custom data types for your program.
const Person = struct {
name: []const u8,
age: i32,
};
Structures help you organize and store data neatly.
You’re almost there!
We just need to see how to create a project in Zig.
Projects in Zig
To start a project in Zig:
- Create a folder and inside, run
zig init-exe
. - This will generate the initial files to start a project.
This command organizes the project and allows you to compile it easily.
Documentation and Help
Zig has good documentation and a friendly community that can help you learn and solve problems.
Write Your First Program
A good first project in Zig is to write a program that displays a message on the screen.
Create a file main.zig
and write your first program:
const std = @import("std");
pub fn main() void {
std.debug.print("Hello, Zig!\n", .{});
}
It’s a simple way to start programming in Zig.
Compile and Run
Once your code is ready, compile it with Zig and run the program to see the result.
zig build run
This command compiles and runs your program immediately.
Well done!
Now you know the basics of Zig! Keep exploring and creating with this innovative language!