Skip to content

Instantly share code, notes, and snippets.

@nickcernis
Last active May 3, 2024 19:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickcernis/2f8dc6b04f19e5045686a2ad829db110 to your computer and use it in GitHub Desktop.
Save nickcernis/2f8dc6b04f19e5045686a2ad829db110 to your computer and use it in GitHub Desktop.
Zig Cheatsheets

From Zigling 058.

//                          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

From Ziglings 054.

     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                      |
   +---------------+----------------------------------------------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment