Skip to content

Instantly share code, notes, and snippets.

@nickcernis
Last active May 3, 2024 19:48
Show Gist options
  • Save nickcernis/2f8dc6b04f19e5045686a2ad829db110 to your computer and use it in GitHub Desktop.
Save nickcernis/2f8dc6b04f19e5045686a2ad829db110 to your computer and use it in GitHub Desktop.

Revisions

  1. nickcernis revised this gist Jan 18, 2024. 2 changed files with 26 additions and 1 deletion.
    25 changes: 25 additions & 0 deletions zig-basic-types-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    From [Zigling 058](https://codeberg.org/ziglings/exercises/src/branch/main/exercises/058_quiz7.zig).

    ```
    // u8 single item
    // *u8 single-item pointer
    // []u8 slice (size known at runtime)
    // [5]u8 array of 5 u8s
    // [*]u8 many-item pointer (zero or more)
    // enum {a, b} set of unique values a and b
    // error {e, f} set of unique error values e and f
    // struct {y: u8, z: i32} group of values y and z
    // union(enum) {a: u8, b: i32} single value either u8 or i32
    //
    // Values of any of the above types can be assigned as "var" or "const"
    // to allow or disallow changes (mutability) via the assigned name:
    //
    // const a: u8 = 5; // immutable
    // var b: u8 = 5; // mutable
    //
    // We can also make error unions or optional types from any of
    // the above:
    //
    // var a: E!u8 = 5; // can be u8 or error from set E
    // var b: ?u8 = 5; // can be u8 or null
    ```
    2 changes: 1 addition & 1 deletion zig-pointer-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    From [Ziglings](https://codeberg.org/ziglings/exercises/src/commit/fa22e861d5b8b63e0f34934e9a21544ad25b4110/exercises/054_manypointers.zig#L42).
    From [Ziglings 054](https://codeberg.org/ziglings/exercises/src/commit/fa22e861d5b8b63e0f34934e9a21544ad25b4110/exercises/054_manypointers.zig#L42).

    ```
    FREE ZIG POINTER CHEATSHEET! (Using u8 as the example type.)
  2. nickcernis created this gist Jan 18, 2024.
    16 changes: 16 additions & 0 deletions zig-pointer-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    From [Ziglings](https://codeberg.org/ziglings/exercises/src/commit/fa22e861d5b8b63e0f34934e9a21544ad25b4110/exercises/054_manypointers.zig#L42).

    ```
    FREE ZIG POINTER CHEATSHEET! (Using u8 as the example type.)
    +---------------+----------------------------------------------+
    | u8 | one u8 |
    | *u8 | pointer to one u8 |
    | [2]u8 | two u8s |
    | [*]u8 | pointer to unknown number of u8s |
    | [*]const u8 | pointer to unknown number of immutable u8s |
    | *[2]u8 | pointer to an array of 2 u8s |
    | *const [2]u8 | pointer to an immutable array of 2 u8s |
    | []u8 | slice of u8s |
    | []const u8 | slice of immutable u8s |
    +---------------+----------------------------------------------+
    ```