zig-snippets

Mandatory hello world

Arrays and Slices

  • Tokenizer: An exercise that started about something simple but it ended up teaching me a lot about array and pointer sizes and optionals.
  • String Literals: I wanted to understand better how string literals work and some interesting things about slices and arrays.
  • String concatenation: Some example to do string concatenation. It's probably better to use a library like https://github.com/JakubSzark/zig-string but it's good snippet to have around when I just want to do something simple

Reading, writing files and/or console:

Allocators

  • General purpose
  • Page allocator: According to some video, this and the one above are probaly the easiest but the slower to use.
  • Fixed Buffer: Don't know why yet but I cannot make a "big" allocation with this one. I could not make a big allocation with the fixed buffer because I was passing a buffer array created on the stack, not on the heap. See this conversation.
  • Arena
  • Arena with fixed buffer
  • Allocators and Syscalls An example of the syscalls made to the underlying OS Linux to request memory with the output of strace

Generics

JSON

Network

Async

Pointers

Parsing

  • Parsing strings or bytes buffers Examples of parsing number in text "1234" into int 1234 and bytes arrays into known numbers.
  • Tokenizer Small tokenizing exercise that ended up as a nice memory allocation exercise.

Data structures

C interoperability

Files that start with c-*.*

Very basic example

# messages like "Double of 4 is in C 8" should appear in stdout
make libfunction

cURL example

# a request is made and the plain HTML printed into stdout
make curl

Raylib example

# NOTE: I need to download raylib to make this work. I placed it in ${HOME}/software/raylib
# a window with a hello world message should open
make ray

Calling Zig from C

At the moment of writing this README, the documentation is outdated and the header file is not generated as stated in the docs. The workaround is to write the headers manually. Look at files c-double_me* for examples:

## Passing 8 in the c-double_me.c file
make c-double_me
16

Misc