Giter VIP home page Giter VIP logo

zig's Introduction

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

zig's People

Contributors

tomiis4 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.