| 1 | // Top-level declarations are order-independent: |
| 2 | const print = std.debug.print; |
| 3 | const std = @import("std"); |
| 4 | const os = std.os; |
| 5 | const assert = std.debug.assert; |
| 6 | |
| 7 | // Custom error set definition: |
| 8 | const ExampleErrorSet = error{ |
| 9 | ExampleErrorVariant, |
| 10 | }; |
| 11 | |
| 12 | pub fn main() void { |
| 13 | // integers |
| 14 | const one_plus_one: i32 = 1 + 1; |
| 15 | print("1 + 1 = {}\n", .{one_plus_one}); |
| 16 | |
| 17 | // floats |
| 18 | const seven_div_three: f32 = 7.0 / 3.0; |
| 19 | print("7.0 / 3.0 = {}\n", .{seven_div_three}); |
| 20 | |
| 21 | // boolean |
| 22 | print("{}\n{}\n{}\n", .{ |
| 23 | true and false, |
| 24 | true or false, |
| 25 | !true, |
| 26 | }); |
| 27 | |
| 28 | // optional |
| 29 | var optional_value: ?[]const u8 = null; |
| 30 | assert(optional_value == null); |
| 31 | |
| 32 | print("\noptional 1\ntype: {}\nvalue: {?s}\n", .{ |
| 33 | @TypeOf(optional_value), optional_value, |
| 34 | }); |
| 35 | |
| 36 | optional_value = "hi"; |
| 37 | assert(optional_value != null); |
| 38 | |
| 39 | print("\noptional 2\ntype: {}\nvalue: {?s}\n", .{ |
| 40 | @TypeOf(optional_value), optional_value, |
| 41 | }); |
| 42 | |
| 43 | // error union |
| 44 | var number_or_error: ExampleErrorSet!i32 = ExampleErrorSet.ExampleErrorVariant; |
| 45 | |
| 46 | print("\nerror union 1\ntype: {}\nvalue: {!}\n", .{ |
| 47 | @TypeOf(number_or_error), |
| 48 | number_or_error, |
| 49 | }); |
| 50 | |
| 51 | number_or_error = 1234; |
| 52 | |
| 53 | print("\nerror union 2\ntype: {}\nvalue: {!}\n", .{ |
| 54 | @TypeOf(number_or_error), number_or_error, |
| 55 | }); |
| 56 | } |
| 57 | |
| 58 | // exe=succeed |