|
| 1 | +## Zig Language Syntax |
| 2 | + |
| 3 | +### Comments |
| 4 | + |
| 5 | +Zig supports three types of comments: |
| 6 | + |
| 7 | +* **Normal Comments**: Start with `//` and continue to the end of the line. They are ignored by the compiler. |
| 8 | + ```zig |
| 9 | + // This is a normal comment. |
| 10 | + ``` |
| 11 | +* **Doc Comments**: Start with `///` and document the declaration that immediately follows. |
| 12 | + ```zig |
| 13 | + /// This is a documentation comment for the MyStruct struct. |
| 14 | + const MyStruct = struct { |
| 15 | + /// This is a doc comment for the field `x`. |
| 16 | + x: i32, |
| 17 | + }; |
| 18 | + ``` |
| 19 | +* **Top-Level Doc Comments**: Start with `//!` and document the current module. They must be at the beginning of a file. |
| 20 | + ```zig |
| 21 | + //! This is a top-level doc comment for the entire file. |
| 22 | + ``` |
| 23 | +
|
| 24 | +### Variable Declarations |
| 25 | +
|
| 26 | +Variables are declared using the `const` and `var` keywords. |
| 27 | +
|
| 28 | +* **`const`**: Declares a constant (immutable) variable. |
| 29 | + ```zig |
| 30 | + const x: i32 = 10; |
| 31 | + const y = 20; // Type is inferred |
| 32 | + ``` |
| 33 | +* **`var`**: Declares a mutable variable. |
| 34 | + ```zig |
| 35 | + var x: i32 = 10; |
| 36 | + var y = 20; // Type is inferred |
| 37 | + ``` |
| 38 | +* **`undefined`**: Used to initialize a variable without giving it a specific value. |
| 39 | + ```zig |
| 40 | + var x: i32 = undefined; |
| 41 | + ``` |
| 42 | +* **`threadlocal`**: Declares a thread-local variable. |
| 43 | + ```zig |
| 44 | + threadlocal var x: i32 = 0; |
| 45 | + ``` |
| 46 | +
|
| 47 | +### Data Types |
| 48 | +
|
| 49 | +#### Primitive Types |
| 50 | +
|
| 51 | +| Type | Description | |
| 52 | +| :------------- | :---------------------------------------- | |
| 53 | +| `iN`, `uN` | Signed and unsigned integers of N bits. | |
| 54 | +| `isize`, `usize` | Signed and unsigned pointer-sized integers. | |
| 55 | +| `f16`, `f32`, `f64`, `f80`, `f128` | Floating-point numbers. | |
| 56 | +| `bool` | `true` or `false`. | |
| 57 | +| `void` | Represents the absence of a value. | |
| 58 | +| `anyerror` | A generic error type. | |
| 59 | +| `anyopaque` | Used for type-erased pointers. | |
| 60 | +| `noreturn` | Indicates a function that does not return. | |
| 61 | +| `type` | The type of a type. | |
| 62 | +| `comptime_int` | The type of an integer literal. | |
| 63 | +| `comptime_float`| The type of a float literal. | |
| 64 | +
|
| 65 | +#### Literals |
| 66 | +
|
| 67 | +* **Integer Literals**: |
| 68 | + ```zig |
| 69 | + const decimal = 1234; |
| 70 | + const hex = 0xff; |
| 71 | + const octal = 0o755; |
| 72 | + const binary = 0b1100; |
| 73 | + ``` |
| 74 | +* **Float Literals**: |
| 75 | + ```zig |
| 76 | + const float1 = 1.23; |
| 77 | + const float2 = 1.23e4; |
| 78 | + ``` |
| 79 | +* **String Literals**: |
| 80 | + ```zig |
| 81 | + const str = "hello"; // Type is *const [5:0]u8 |
| 82 | + ``` |
| 83 | +* **Character Literals**: |
| 84 | + ```zig |
| 85 | + const char = 'a'; // Type is comptime_int |
| 86 | + ``` |
| 87 | +
|
| 88 | +### Control Flow |
| 89 | +
|
| 90 | +#### `if` |
| 91 | +
|
| 92 | +The `if` statement executes a block of code if a condition is true. |
| 93 | +
|
| 94 | +```zig |
| 95 | +if (x > 10) { |
| 96 | + // ... |
| 97 | +} else if (x > 5) { |
| 98 | + // ... |
| 99 | +} else { |
| 100 | + // ... |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +`if` can also capture the payload of an optional type. |
| 105 | + |
| 106 | +```zig |
| 107 | +if (optional_value) |value| { |
| 108 | + // use value |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +#### `while` |
| 113 | + |
| 114 | +The `while` loop repeatedly executes a block of code as long as a condition is true. |
| 115 | + |
| 116 | +```zig |
| 117 | +while (x < 10) { |
| 118 | + x += 1; |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +`while` can also be used with optionals and error unions. |
| 123 | + |
| 124 | +```zig |
| 125 | +while (optional_value) |value| { |
| 126 | + // use value |
| 127 | +} |
| 128 | +
|
| 129 | +while (error_union) |value| { |
| 130 | + // use value |
| 131 | +} else |err| { |
| 132 | + // handle error |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +#### `for` |
| 137 | + |
| 138 | +The `for` loop iterates over the elements of an array, slice, or tuple. |
| 139 | + |
| 140 | +```zig |
| 141 | +const items = [_]i32{ 1, 2, 3 }; |
| 142 | +for (items) |item| { |
| 143 | + // use item |
| 144 | +} |
| 145 | +
|
| 146 | +// With index |
| 147 | +for (items, 0..) |item, i| { |
| 148 | + // use item and i |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +#### `switch` |
| 153 | + |
| 154 | +The `switch` statement provides a way to execute different code blocks based on the value of an expression. |
| 155 | + |
| 156 | +```zig |
| 157 | +switch (value) { |
| 158 | + 1 => { /* ... */ }, |
| 159 | + 2 => { /* ... */ }, |
| 160 | + else => { /* ... */ }, |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +`switch` must be exhaustive. |
| 165 | + |
| 166 | +### Functions |
| 167 | + |
| 168 | +Functions are declared with the `fn` keyword. |
| 169 | + |
| 170 | +```zig |
| 171 | +fn add(a: i32, b: i32) i32 { |
| 172 | + return a + b; |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | +* **`pub`**: Makes a function visible outside the current module. |
| 177 | +* **`inline`**: Suggests to the compiler that the function should be inlined. |
| 178 | +* **`noreturn`**: Specifies that the function will not return. |
| 179 | + |
| 180 | +### Error Handling |
| 181 | + |
| 182 | +Zig has a unique approach to error handling using error sets and error unions. |
| 183 | + |
| 184 | +* **`error`**: A set of possible error values. |
| 185 | + ```zig |
| 186 | + const MyError = error{ |
| 187 | + InvalidValue, |
| 188 | + OutOfMemory, |
| 189 | + }; |
| 190 | + ``` |
| 191 | +* **Error Union**: A type that can hold either a value or an error. |
| 192 | + ```zig |
| 193 | + fn doSomething() !i32 { |
| 194 | + if (something_wrong) { |
| 195 | + return error.InvalidValue; |
| 196 | + } |
| 197 | + return 42; |
| 198 | + } |
| 199 | + ``` |
| 200 | + The `!` in `!i32` is a shorthand for `anyerror!i32`. |
| 201 | +
|
| 202 | +* **`try`**: Unwraps a value from an error union. If the value is an error, the function returns the error. |
| 203 | + ```zig |
| 204 | + const value = try doSomething(); |
| 205 | + ``` |
| 206 | +* **`catch`**: Unwraps a value from an error union, with a fallback value or block for the error case. |
| 207 | + ```zig |
| 208 | + const value = doSomething() catch 0; |
| 209 | +
|
| 210 | + const value2 = doSomething() catch |err| { |
| 211 | + // handle error |
| 212 | + return 0; |
| 213 | + }; |
| 214 | + ``` |
| 215 | +* **`errdefer`**: Executes a statement when the function returns with an error. |
| 216 | + ```zig |
| 217 | + errdefer cleanup(); |
| 218 | + ``` |
| 219 | +
|
| 220 | +### Other Core Constructs |
| 221 | +
|
| 222 | +#### `defer` |
| 223 | +
|
| 224 | +Executes a statement at the end of the current block, regardless of how the block is exited. |
| 225 | +
|
| 226 | +```zig |
| 227 | +defer file.close(); |
| 228 | +``` |
| 229 | + |
| 230 | +#### `unreachable` |
| 231 | + |
| 232 | +Asserts that a piece of code is never executed. |
| 233 | + |
| 234 | +```zig |
| 235 | +if (value > 10) { |
| 236 | + // ... |
| 237 | +} else { |
| 238 | + unreachable; |
| 239 | +} |
| 240 | +``` |
| 241 | + |
| 242 | +#### `struct` |
| 243 | + |
| 244 | +A collection of named fields. |
| 245 | + |
| 246 | +```zig |
| 247 | +const Point = struct { |
| 248 | + x: i32, |
| 249 | + y: i32, |
| 250 | +}; |
| 251 | +``` |
| 252 | + |
| 253 | +#### `enum` |
| 254 | + |
| 255 | +A type with a fixed set of named values. |
| 256 | + |
| 257 | +```zig |
| 258 | +const Color = enum { |
| 259 | + red, |
| 260 | + green, |
| 261 | + blue, |
| 262 | +}; |
| 263 | +``` |
| 264 | + |
| 265 | +#### `union` |
| 266 | + |
| 267 | +A type that can hold one of several types at a time. |
| 268 | + |
| 269 | +```zig |
| 270 | +const MyUnion = union { |
| 271 | + integer: i32, |
| 272 | + float: f32, |
| 273 | +}; |
| 274 | +``` |
| 275 | + |
| 276 | +#### `opaque` |
| 277 | + |
| 278 | +An opaque type is a type whose layout and size is unknown. |
| 279 | + |
| 280 | +```zig |
| 281 | +const MyOpaqueType = opaque {}; |
| 282 | +``` |
| 283 | + |
| 284 | +### Keywords |
| 285 | + |
| 286 | +Here is a list of keywords found in the Zig language reference: |
| 287 | + |
| 288 | +`addrspace`, `align`, `allowzero`, `and`, `anyframe`, `anyopaque`, `asm`, `async`, `await`, `break`, `callconv`, `catch`, `comptime`, `const`, `continue`, `defer`, `else`, `enum`, `errdefer`, `error`, `export`, `extern`, `false`, `fn`, `for`, `if`, `inline`, `noalias`, `nosuspend`, `null`, `opaque`, `or`, `orelse`, `packed`, `promise`, `pub`, `resume`, `return`, `linksection`, `struct`, `suspend`, `switch`, `test`, `threadlocal`, `true`, `try`, `type`, `union`, `unreachable`, `usingnamespace`, `var`, `volatile`, `while` |
0 commit comments