tomiis4 / Zig

My Zig projects with cheatsheet

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Zig Cheatsheet

File

Run file

zig run file.zig

Generate exe file

zig build-exe file.zig

Hello world

// import print fromm std
const print = @import("std").debug.print;

// create main function
pub fn main() void {
    // print
    print("Hello, world!\n", .{});
}

Importing packages

const <package> = @import("<package-name>");

Variables

// mutable
var <variable-name>: type = <value>;
var <variable-name>: type; // empty

// immutable
const <variable-name>: type = <value>;

// arrays
var arr: [<length>]<type> = [<value>, ...];
var arr: [<length>]<type>; // empty

/*
Type: 
	bool                     = %b   = true, false
	u8, u16, u32, u64, u128  = %d   = number in range of x bits, can't be negative
	i8, i16, i32, i64, i128  = %d   = number in range of x bits, can be negative
	f16, f32, f64            = %f   = decimal numbers
	[] u8               = %.*s = string
*/

Structs

// create struct
const <Struct-name> = struct {
    <key1>: <type>,
    <key2>: <type>,
};

// use struct
const <struct-x> = {
    <key1>: <value>,
    <key2>: <value>,
};

Map

const HashMap = @import("std").HashMap;

// create map
var <map-name> = HashMap(<type>).init(<length>);

// add item
<map-name>.put(<key>, <value>);

// read item
const <value> = <map-name>.get(<key>);

Functions

// normal function for one file
fn name() void {
	//...
}

// public function
pub fn name() void {
	//...
}

// return
fn name() <type> { return x; }
fn name() (<type>, <type>) { return (x, y); }

// parameters 
fn name(param1: <type>) {  }
fn name(param1, param2: <type>) {  } // if param1 have same type as param2

Logic Statements

If/else

if (<statement>) {
    // ...
}

Switch/case

switch (operation) {
    x => <do-x>,
    y => <do-y>,
    else => <do-else>,
}

Loop

For-I

for (var i: u32 = 0; i<10; i++) {
    // ...
}

For-In

var arr: [10]u8 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

for (arr) |elem, i| {
    // ...
}

While

while (condition) {
    // ...
}

Converting

// str -> int
const str = "123";
const num = try std.parseInt(u32, str);

// int -> str

Build-In Functions

func

Pointers

Unit Testing

// main.zig
// main_test.zig

External file

// folder structure
|- main.zig
|
|- example
  |- second.zig

// main.zig


// second.zig

Remote packages

Install packages


Import packages

Packages

pkg

// code

TODO

  • Enums
  • std.io

Project ideas

About

My Zig projects with cheatsheet


Languages

Language:Zig 100.0%