diff options
Diffstat (limited to 'examples/hello-zig/src/main.zig')
| -rw-r--r-- | examples/hello-zig/src/main.zig | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/examples/hello-zig/src/main.zig b/examples/hello-zig/src/main.zig new file mode 100644 index 0000000..74eb590 --- /dev/null +++ b/examples/hello-zig/src/main.zig | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const hello_zig = @import("hello_zig"); | ||
| 3 | |||
| 4 | pub fn main() !void { | ||
| 5 | var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||
| 6 | defer arena.deinit(); | ||
| 7 | |||
| 8 | const allocator = arena.allocator(); | ||
| 9 | const args = try std.process.argsAlloc(allocator); | ||
| 10 | const name = if (args.len > 1) args[1] else "Nova"; | ||
| 11 | |||
| 12 | var greeter = hello_zig.Greeter{ | ||
| 13 | .name = name, | ||
| 14 | .punctuation = '!', | ||
| 15 | }; | ||
| 16 | |||
| 17 | const stdout = std.fs.File.stdout().deprecatedWriter(); | ||
| 18 | try greeter.write(stdout, .verbose); | ||
| 19 | |||
| 20 | const sample_values = [_]i32{ -3, -2, 0, 1, 2, 3, 4 }; | ||
| 21 | var squares = try hello_zig.collectEvenSquares(allocator, &sample_values); | ||
| 22 | defer squares.deinit(allocator); | ||
| 23 | |||
| 24 | std.debug.print("even squares: {any}\n", .{squares.items}); | ||
| 25 | } | ||
| 26 | |||
| 27 | test "main module can compute a summary" { | ||
| 28 | const greeting = try hello_zig.describeNumber(std.testing.allocator, 42); | ||
| 29 | defer std.testing.allocator.free(greeting); | ||
| 30 | |||
| 31 | try std.testing.expect(std.mem.startsWith(u8, greeting, "positive")); | ||
| 32 | } | ||
